OBT::Singleton< T > Class Template Reference

#include <OBTSingleton.h>

Collaboration diagram for OBT::Singleton< T >:

Collaboration graph
[legend]

List of all members.

Static Public Member Functions

static T & getInstance ()

Private Member Functions

 Singleton ()
 constructor
virtual ~Singleton ()
 destructor
 Singleton (const Singleton &)
 copy constructor
Singletonoperator= (const Singleton &)
 assignment operator

Static Private Member Functions

static void create ()
 creates a new instance of T and store a pointer to it in instance_
static void onDeadReference ()
 called on a dead reference detection, re-creates an instance and throws an exception
static void destroy ()
 flag the Singleton as destroyed

Static Private Attributes

static T * _instance = 0
 pointer to the unique instance of T in the application
static bool _destroyed = false
 indicates if the OBTSingleton.has been destroyed
static bool _explicitDestruction = false
 indicates if the OBTSingleton.has been destroyed once


Detailed Description

template<class T>
class OBT::Singleton< T >

Singleton template class
Author:
Michaël Rouillé <michael.rouille@gmail.com> In order to be a singleton, a class constructor should be private and give friendship to Singleton. The unique instance of the class is obtained by a call to getInstance.
This Singleton implementation :

Definition at line 25 of file OBTSingleton.h.


Constructor & Destructor Documentation

template<class T>
OBT::Singleton< T >::Singleton (  )  [private]

constructor

template<class T>
OBT::Singleton< T >::~Singleton (  )  [inline, private, virtual]

destructor

Definition at line 85 of file OBTSingleton.h.

00086         {
00087         }

template<class T>
OBT::Singleton< T >::Singleton ( const Singleton< T > &   )  [private]

copy constructor


Member Function Documentation

template<class T>
T & OBT::Singleton< T >::getInstance (  )  [inline, static]

access to the unique instance of T getInstance purposely does not catch the exception thrown when a dead reference is detected, in order to help you resolve potential design problems which lead to this situation. If you really need to access the destroyed singleton, just catch the exception thrown and call getInstance again.

Returns:
a reference to the unique instance of T in the application

Definition at line 94 of file OBTSingleton.h.

References OBT::Singleton< T >::_destroyed, OBT::Singleton< T >::_instance, OBT::Singleton< T >::create(), OBT::Singleton< T >::destroy(), and OBT::Singleton< T >::onDeadReference().

00095         {
00096                 if ( _instance == NULL )
00097                 {
00098                         // checks for dead reference
00099                         if ( _destroyed == true )
00100                         {
00101                                 onDeadReference() ;
00102                         }
00103 
00104                         // create the instance
00105                         create() ;
00106 
00107                         // the destroy method will be called at the application exit 
00108                         std::atexit( destroy ) ;
00109                 }
00110                 return *_instance ;
00111         }

template<class T>
void OBT::Singleton< T >::create (  )  [inline, static, private]

creates a new instance of T and store a pointer to it in instance_

Definition at line 118 of file OBTSingleton.h.

References OBT::Singleton< T >::_instance.

Referenced by OBT::Singleton< T >::getInstance(), and OBT::Singleton< T >::onDeadReference().

00119         {
00120                 // initialise _instance
00121                 static T theInstance ;
00122                 _instance = &theInstance ;
00123         }

template<class T>
void OBT::Singleton< T >::onDeadReference (  )  [inline, static, private]

called on a dead reference detection, re-creates an instance and throws an exception

Definition at line 130 of file OBTSingleton.h.

References OBT::Singleton< T >::_destroyed, OBT::Singleton< T >::_explicitDestruction, OBT::Singleton< T >::_instance, OBT::Singleton< T >::create(), and OBT::Singleton< T >::destroy().

Referenced by OBT::Singleton< T >::getInstance().

00131         {
00132                 // obtains the shell of the destroyed singleton
00133                 create() ;
00134                 // now _instance points to the "ashes" of the singleton
00135                 // - the raw memory that the singleton was seated in.
00136                 // creates a new singleton at that address
00137                 new( _instance ) T ;
00138                 // reset _destroyed because we're back in business
00139                 _destroyed = false ;
00140                 // set _explicitDestruction because the business must be destroyed
00141                 // at the application exit
00142                 _explicitDestruction = true ;
00143                 // the destroy method will be called at the application exit 
00144                 std::atexit( destroy ) ;
00145 
00146                 throw std::runtime_error( "Detection of a Singleton Dead Reference" ) ;
00147         }

template<class T>
void OBT::Singleton< T >::destroy (  )  [inline, static, private]

flag the Singleton as destroyed

Definition at line 154 of file OBTSingleton.h.

References OBT::Singleton< T >::_destroyed, OBT::Singleton< T >::_explicitDestruction, and OBT::Singleton< T >::_instance.

Referenced by OBT::Singleton< T >::getInstance(), and OBT::Singleton< T >::onDeadReference().

00155         {
00156                 if ( _explicitDestruction )
00157                 {
00158                         _instance->~T() ;
00159                 }
00160                 _instance = NULL ;
00161                 _destroyed = true ;
00162         }

template<class T>
Singleton& OBT::Singleton< T >::operator= ( const Singleton< T > &   )  [private]

assignment operator


Member Data Documentation

template<class T>
T * OBT::Singleton< T >::_instance = 0 [inline, static, private]

pointer to the unique instance of T in the application

Definition at line 53 of file OBTSingleton.h.

Referenced by OBT::Singleton< T >::create(), OBT::Singleton< T >::destroy(), OBT::Singleton< T >::getInstance(), and OBT::Singleton< T >::onDeadReference().

template<class T>
bool OBT::Singleton< T >::_destroyed = false [inline, static, private]

indicates if the OBTSingleton.has been destroyed

Definition at line 56 of file OBTSingleton.h.

Referenced by OBT::Singleton< T >::destroy(), OBT::Singleton< T >::getInstance(), and OBT::Singleton< T >::onDeadReference().

template<class T>
bool OBT::Singleton< T >::_explicitDestruction = false [inline, static, private]

indicates if the OBTSingleton.has been destroyed once

Definition at line 59 of file OBTSingleton.h.

Referenced by OBT::Singleton< T >::destroy(), and OBT::Singleton< T >::onDeadReference().


Generated on Wed Oct 1 11:34:19 2008 for OBT by  doxygen 1.5.3