Library of Assembled Shared Sources
lass::util::Singleton< T, destructPriority > Class Template Reference

non-intrusive singleton holder. More...

#include <singleton.h>

Inheritance diagram for lass::util::Singleton< T, destructPriority >:
Collaboration diagram for lass::util::Singleton< T, destructPriority >:

Static Public Member Functions

static TInstance * instance ()
 Return pointer to singleton instance.
 

Protected Member Functions

void subscribeInstance (int iDestructionPriority)
 Subscribe to the singleton guard.
 

Related Symbols

(Note that these are not member symbols.)

enum  DestructionPriorities
 Destruction priority constants. More...
 

Detailed Description

template<class T, int destructPriority = destructionPriorityDefault>
class lass::util::Singleton< T, destructPriority >

non-intrusive singleton holder.

Author
Bram de Greve
Date
2003

With this class, it's possible to create a singleton of a class Foo. It's a non-instrusive singleton because it's not required to derive Foo from this Singleton class. Instead, this class will be a holder to a singleton object of Foo. To use class Foo as a singleton is very straight forward, but can be done in a couple of ways:

  1. The typedef way: With a typedef, you create a new type that will act as singleton holder for class Foo. To access the singleton, you must now use the instance() method on that new type:
typedef lass::util::Singleton<Foo> TFooSingleton;
TFooSingleton()::instance()->bar(fun);
non-intrusive singleton holder.
Definition singleton.h:141
static TInstance * instance()
Return pointer to singleton instance.
Definition singleton.inl:82
  1. The function way (prefered/b>): you write a nice little function that returns a pointer to the singleton. To access the singleton, you simple call that function:
Foo* foo()
{
}
foo()->bar(fun);

Both ways only differ in how to access the instance. All other properties are common:

  • The instance of the singleton is constructed on the heap on first access on the heap. i.e. the first time in the program TFooSingleton::instance() or foo() is called, a new object of Foo is allocated on the heap, and a pointer to it is returned. All the following calls will return that same pointer. This is the way how to control the order of construction: on first access.
  • The order of destruction can be set by the DestructionPriority. Singletons with a smaller destruction priority will be destructed later than those with a higher destruction priority. It's not specified in which order singletons with identical destruction priorities will be destructed (i.e. its not guaranteed for such singletons that the last one constructed will be the first to be destroyed). You can set the destruction priority by the second template argument of Singleton: lass::util::Singleton<Foo,2000> will specify the singleton to have a destruction priority of 2000. The default destruction priority is set to be 1000. Priorities below 500 are reserved for the Lass implementation (and for you who know what you're doing ;)

    warning: lass::io::proxyMan() is a proxy man that is needed for the implementation of all logging activities etc. Its destruction priority is set to 0 and there should be no other singleton have a destruction priority lower or equal than this.

  • Requirements for the class Foo: Since it has to be constructed on the heap without any parameters in the neighbourhoud, it has to have a default constructor.

Reference:

  1. ALEXANDRESCU A. (2001), Modern C++ Design: Generic Programming and Design Patterns applied, C++ in depth series, Addison-Wesley, pages 129-156
  2. GAMMA E., HELM R., JOHNSON R. & VLISSIDES J. (1995), Design Patters: Elements of Reusable Object-Oriented Software, Addison-Wesley.

Definition at line 140 of file singleton.h.

Member Function Documentation

◆ instance()

template<class T, int DP>
Singleton< T, DP >::TInstance * lass::util::Singleton< T, DP >::instance ( )
static

Return pointer to singleton instance.

An instance is created on first call. Return NULL if instance is dead already (after first destruction of singletons).

Note
this should be thread safe over all singletons.

Definition at line 82 of file singleton.inl.

References instance(), and lass::util::impl::SingletonBase::subscribeInstance().

Referenced by instance(), lass::util::processPriorityDictionary(), and lass::io::proxyMan().

◆ subscribeInstance()

void lass::util::impl::SingletonBase::subscribeInstance ( int destructionPriority)
protectedinherited

Subscribe to the singleton guard.

Do this only once for each singleton, and in fact, don't do it yourself at all since Singleton<> already does it :)

Warning
this isn't thread safe as it is, but that's ok because its only caller Singleton::instance() is already locking.

Definition at line 207 of file singleton_impl.cpp.

References lass::util::destructionPriorityNever.

Referenced by lass::util::Singleton< T, destructPriority >::instance().

Friends And Related Symbol Documentation

◆ DestructionPriorities

template<class T, int destructPriority = destructionPriorityDefault>
enum DestructionPriorities
related

Destruction priority constants.

Priorities levels below singletonDestructionPriorityBeginUserRange (=500) are reserved for the implementation of Lass, and should not be used by user code (unless you know what you're doing of course ;)

Definition at line 124 of file singleton.h.


The documentation for this class was generated from the following files: