library of assembled shared sources

http://lass.cocamware.com

lass::util::Singleton< T, destructPriority > Class Template Reference

non-intrusive singleton holder. More...

#include <singleton.h>

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

Inheritance graph
[legend]
Collaboration diagram for lass::util::Singleton< T, destructPriority >:

Collaboration graph
[legend]

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 TInstanceinstance ()
 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< TInstanceinstance_

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...

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);

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:

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.

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 Typedef Documentation

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

Definition at line 144 of file singleton.h.

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

Definition at line 146 of file singleton.h.


Member Enumeration Documentation

template<class T , int destructPriority = destructionPriorityDefault>
anonymous enum

Enumerator:
destructionPriority 

Definition at line 147 of file singleton.h.


Constructor & Destructor Documentation

template<class T , int DP>
lass::util::Singleton< T, DP >::Singleton (  )  [inline]

Definition at line 58 of file singleton.inl.

template<class T , int DP>
lass::util::Singleton< T, DP >::~Singleton (  )  [inline, virtual]


Member Function Documentation

template<class T , int DP>
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).

Note:
this should be thread safe over all 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().

template<class T , int DP>
bool lass::util::Singleton< T, DP >::deadReference ( bool  iSetReferenceToDead = false  )  [inline, static, private]

return true if singleton is destructed.

Parameters:
iSetReferenceToDead - call this method with true on destruction of singleton
  • call this method with false to check it.

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]

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 :)

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

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().


Friends And Related Function Documentation

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 ;)

Enumerator:
destructionPriorityDefault  default priority level =)
destructionPriorityBeginUser  lowest legal level for user code
destructionPriorityInternalProxyMan 
destructionPriorityInternalPythonMutex 
destructionPriorityInternalTlsDestructors 
destructionPriorityInternalAllocators 
destructionPriorityInternalTlsLocalsMain 

Definition at line 125 of file singleton.h.


Field Documentation

template<class T , int destructPriority = destructionPriorityDefault>
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().


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

Generated on Mon Nov 10 14:22:17 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo