www.digitalmars.com         C & C++   DMDScript  

D - Is it possible to great a singleton object in D?

reply Terry Bayne <gnome hiwaay.net> writes:
Just to avoid confusion, by "singleton" I mean an object of which only one 
instance can be instanciated, and any subsequent attempts to create an 
instance of the object return a reference to the first one created.

They work great for handling things like configuration information and the 
like.

So anyone have any idea how to create on in D?  

Thanks
Terry
Jul 24 2003
next sibling parent reply Bill Cox <bill viasic.com> writes:
Terry Bayne wrote:
 Just to avoid confusion, by "singleton" I mean an object of which only one 
 instance can be instanciated, and any subsequent attempts to create an 
 instance of the object return a reference to the first one created.
 
 They work great for handling things like configuration information and the 
 like.
 
 So anyone have any idea how to create on in D?  
 
 Thanks
 Terry
 
How about just creating one up front, and save it in a global variable? Bill
Jul 24 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
In D you can make the ctors private yet the module it's defined in can still
access them.  So yeah, that should work.

Sean

"Bill Cox" <bill viasic.com> wrote in message
news:3F204B87.3080201 viasic.com...
 Terry Bayne wrote:
 Just to avoid confusion, by "singleton" I mean an object of which only
one
 instance can be instanciated, and any subsequent attempts to create an
 instance of the object return a reference to the first one created.

 They work great for handling things like configuration information and
the
 like.

 So anyone have any idea how to create on in D?

 Thanks
 Terry
How about just creating one up front, and save it in a global variable? Bill
Jul 25 2003
prev sibling next sibling parent "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
"Terry Bayne" <gnome hiwaay.net> escreveu na mensagem
news:Xns93C28B1B4AEAEtbaynehiwaaynet 63.105.9.61...
 Just to avoid confusion, by "singleton" I mean an object of which only one
 instance can be instanciated, and any subsequent attempts to create an
 instance of the object return a reference to the first one created.

 They work great for handling things like configuration information and the
 like.

 So anyone have any idea how to create on in D?

 Thanks
 Terry
Hi, You can write your singletonas you would do in C++ or Java. Something like this works correctly: module singleton; class Singleton { private static Singleton shared; private static this() { shared = new Singleton(); } private this() { } public static Singleton getInstance() { return shared; } public void print(char[] name) { printf("Hello %.*s! I'm %d.\r\n", name, cast(int*) this); } } int main() { Singleton a = Singleton.getInstance(); a.print("a"); Singleton b = Singleton.getInstance(); b.print("b"); a.print("a"); return 0; } In D you can also define variables in the module scope, "static this" is the module constructor, will run once. Best regards, Daniel Yokomiso. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.502 / Virus Database: 300 - Release Date: 18/7/2003
Jul 24 2003
prev sibling next sibling parent Burton Radons <loth users.sourceforge.net> writes:
Terry Bayne wrote:
 Just to avoid confusion, by "singleton" I mean an object of which only one 
 instance can be instanciated, and any subsequent attempts to create an 
 instance of the object return a reference to the first one created.
Not through direct syntax. You can synthesize one using a static member and function. You can also force memory recycling: class Singleton { static Singleton singleton; new (uint size) { if (singleton === null) { singleton = (Singleton) new void * [(size + 3) / 4]; singleton.init (); } return singleton; } /* This is used instead of a constructor to avoid having the constructor called multiple times. */ void init () { /* Prepare the singleton. */ } } Singleton a = new Singleton (); /* Create the new singleton. */ Singleton b = new Singleton (); /* Recycle the old singleton. */ Unfortunately one can't create a Singleton base class that would have the inherited property, because "new" isn't passed the ClassInfo. One might ask what I'm doing using "new void * [size / 4]" instead of "new ubyte [size]". Once the GC is type-aware, casting between an array of values and an object reference will result in any pointers in the object being collected. I think I've advocated putting in casting limitations to prevent the problem before, but in any case I am now; you should not be able to cast between a value array and a pointer.
Jul 24 2003
prev sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
A singleton is nearly indistinguishable from a module, in concept.

Sean

"Terry Bayne" <gnome hiwaay.net> wrote in message
news:Xns93C28B1B4AEAEtbaynehiwaaynet 63.105.9.61...
 Just to avoid confusion, by "singleton" I mean an object of which only one
 instance can be instanciated, and any subsequent attempts to create an
 instance of the object return a reference to the first one created.

 They work great for handling things like configuration information and the
 like.

 So anyone have any idea how to create on in D?

 Thanks
 Terry
Jul 25 2003