Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-03-26 15:50:30


Rob Stewart wrote:

>I contend that you can provide a mechanism whereby clients can
>ensure their type can only be instantiated by the Singleton
>framework, but don't require that.
>
I already do :) You can use a type deriving from the singleton if you
want true singleton behavior, or you can use a typedef if you want one
globally managed resource and any number of local resources.

class A : public singleton < A > { /*...*/ }; // only the singleton
instance can exist
class B { /*...*/ }; // can create any number of B instances
typedef singleton < B > global_b; // but global_b manages the lifetime
of one global B instance.

There is a multiton for cases when you need multiple global instances
that uses key based identification, and alternately you can derive a
different type from B for each global instance you want managed.

class Printer { /*...*/ };

class PrinterA : public Printer, public singleton < PrinterA > { };
class PrinterB : public Printer, public singleton < PrinterB > { };

-Jason


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk