library of assembled shared sources |
http://lass.cocamware.com |
#include <singleton.h>
Public Types | |
enum | { destructionPriority = destructPriority } |
typedef Singleton< T, destructPriority > | TSelf |
typedef T | TInstance |
Public Member Functions | |
Singleton () | |
virtual | ~Singleton () |
int | destructionPriority () const |
Static Public Member Functions | |
static TInstance * | instance () |
Return pointer to singleton instance. | |
Protected Member Functions | |
void | subscribeInstance (int iDestructionPriority) |
Subscribe to the singleton guard. | |
Static Private Member Functions | |
static bool | deadReference (bool iSetReferenceToDead=false) |
return true if singleton is destructed. | |
Private Attributes | |
ScopedPtr< TInstance > | instance_ |
Related Functions | |
(Note that these are not member functions.) | |
enum | DestructionPriorities { destructionPriorityDefault = 1000, destructionPriorityBeginUser = 500, destructionPriorityInternalProxyMan = 0, destructionPriorityInternalPythonMutex = 50, destructionPriorityInternalTlsDestructors = 100, destructionPriorityInternalAllocators = 200, destructionPriorityInternalTlsLocalsMain = 400 } |
Destruction priority constants. More... |
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);
2. 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() { return lass::util::Singleton<Foo>::instance(); } foo()->bar(fun);
Both ways only differ in how to access the instance. All other properties are common:
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.
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.
Foo:
Since it has to be constructed on the heap without any parameters in the neighbourhoud, it has to have a default constructor.Reference:
Definition at line 140 of file singleton.h.
typedef Singleton<T, destructPriority> lass::util::Singleton< T, destructPriority >::TSelf |
Definition at line 144 of file singleton.h.
typedef T lass::util::Singleton< T, destructPriority >::TInstance |
Definition at line 146 of file singleton.h.
anonymous enum |
lass::util::Singleton< T, DP >::Singleton | ( | ) | [inline] |
Definition at line 58 of file singleton.inl.
lass::util::Singleton< T, DP >::~Singleton | ( | ) | [inline, virtual] |
Definition at line 67 of file singleton.inl.
References lass::util::Singleton< T, destructPriority >::deadReference().
Singleton< T, DP >::TInstance * lass::util::Singleton< T, DP >::instance | ( | ) | [inline, 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).
Definition at line 80 of file singleton.inl.
References lass::io::cerr(), lass::util::Singleton< T, destructPriority >::deadReference(), lass::util::impl::SingletonBase::destructionPriority(), lass::util::Singleton< T, destructPriority >::instance_, LASS_LOCK, and lass::util::impl::SingletonBase::subscribeInstance().
bool lass::util::Singleton< T, DP >::deadReference | ( | bool | iSetReferenceToDead = false |
) | [inline, static, private] |
return true if singleton is destructed.
iSetReferenceToDead | - call this method with true on destruction of singleton
|
Definition at line 115 of file singleton.inl.
Referenced by lass::util::Singleton< T, destructPriority >::instance(), and lass::util::Singleton< T, destructPriority >::~Singleton().
int lass::util::impl::SingletonBase::destructionPriority | ( | ) | const [inherited] |
Definition at line 167 of file singleton_impl.cpp.
References lass::util::impl::SingletonBase::destructionPriority_.
Referenced by lass::util::Singleton< T, destructPriority >::instance(), and lass::util::impl::CompareDestructionPriority::operator()().
void lass::util::impl::SingletonBase::subscribeInstance | ( | int | iDestructionPriority | ) | [protected, inherited] |
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 :)
Definition at line 181 of file singleton_impl.cpp.
References lass::util::impl::SingletonBase::destructionPriority_, and lass::util::impl::SingletonGuard::instance().
Referenced by lass::util::Singleton< T, destructPriority >::instance().
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 125 of file singleton.h.
ScopedPtr<TInstance> lass::util::Singleton< T, destructPriority >::instance_ [private] |
Definition at line 158 of file singleton.h.
Referenced by lass::util::Singleton< T, destructPriority >::instance().
Generated on Mon Nov 10 14:22:17 2008 for Library of Assembled Shared Sources by 1.5.7.1 |