On Tue, 24 Nov 2009, Leandro Damasio wrote: Hi,
> > I know that you want to integrate it at low level in the Harbour Object > > engine, but how about the following sample: > > > > #xcommand SINGLETON CLASS <clsName> [ FROM <fromCls> ] => ; > > FUNCTION <clsName>() ;; > > STATIC obj ;; > > RETURN iif( obj = NIL, obj := S_<clsName>(), obj ) ;; > > CLASS S_<clsName> [ FROM <fromCls> ] > > Hi Teo, > Yes this is a smart solution, although it is limited to one singleton > class per module, but my intention is to implement a unified solution > to singleton classes in shared libraries. I do not see any reason for such limitation. The above #xcommand is universal rule you can define once in some global .ch file and then you can use it as many times as you want in one or many modules. I would only modify it a little bit adding protection against 1-st concurrent call in MT environment and making real class function static so it will not be accessible from external code. #xcommand SINGLETON CLASS <!clsName!> ; [ FROM <!fromCls1!> [,<!fromClsN!>] ] => ; FUNCTION <clsName>() ;; STATIC obj, once ;; IF obj == NIL ;; hb_threadOnce( @once, {|| obj := __S_<clsName>() } ) ;; END ;; RETURN obj;; CLASS <clsName> [ FROM <fromCls1> ] [, <fromClsN> ] STATIC ; FUNCTION __S_<clsName> It can be extended yet but I do not want to make it less readable using some more complicated PP rules. > My proposal is that it be implemented in such a way that one developer > can declare a singleton class, program its composition and behavior, > box it in a library and share it to other programmers knowing for sure > that nobody will can double instantiate it and cause error, because he > declared to the machine that this can't be done. So the above #xcommand is sth what you are looking for. It passes all conditions you've defined so far. Anyhow really important parts of such feature are not defined at all and it's not trivial to create such definition and then implement it keeping basic OOP rules so I would expect that users may want to adopt this command for his own needs. best regards, Przemek _______________________________________________ Harbour mailing list (attachment size limit: 40KB) Harbour@harbour-project.org http://lists.harbour-project.org/mailman/listinfo/harbour