1 //=============================================================================
2 // File : Utils_SINGLETON.hxx
3 // Created : lun nov 5 16:04:47 CET 2001
4 // Author : Antoine YESSAYAN, EDF
6 // Copyright : EDF 2001
8 //=============================================================================
13 # if !defined( __SINGLETON__H__ )
14 # define __SINGLETON__H__
16 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
23 * A singleton is a data which is created and deleted only once in the application.
24 * The C++ compiler allow the user to create static data before the first executable statement.
25 * They are deleted after the last statement.statement.
27 * The SINGLETON_ template class deals with dynamic singleton. It is useful for functor objects.
28 * For example, an object which, when created, connects the application to a system and
29 * disconnects the application at deletion.
34 * To create a single instance a POINT_ object :
36 * # include "Utils_SINGLETON.hxx"
38 * ptrPoint = SINGLETON_<POINT_>::Instance() ;
41 * <B>Design description</B>
43 * -# the user creates an object of class TYPE By using a class method : SINGLETON_<TYPE>::Instance() which
44 * returns a pointer to the single object ;
45 * -# this class method uses the default constructor to create an object ;
46 * -# at the same time, this class method reate a destructor object which is added to the generic list
47 * of destructors objects to be executed at the end of the application (atexit) ;
48 * -# at the end of the application process all the deletions are performed by the Nettoyage() C function
49 * which execute the destructions objects then deletes the destructions objects themselves ;
50 * -# the Nettoyage() C function is recorded using atexit() C function through the creation of a static
51 * single object ATEXIT_().
55 template <class TYPE> class SINGLETON_
60 static TYPE *Instance( void ); //!< Singleton dynamic creation using the default builder
61 static bool IsAlreadyExisting( void ); //!< returns True if the singleton is already existing
62 static int Destruction( void ); //!< destroys the Singleton before the end of the application process
67 static SINGLETON_ *PtrSingleton ;
72 } ; /* class SINGLETON_<TYPE> */
77 template <class TYPE> SINGLETON_<TYPE> *SINGLETON_<TYPE>::PtrSingleton=NULL ;
82 * The class method Instance :
83 * -# creates an object of class TYPE ;
84 * -# creates a destruction object DESTRUCTEUR_DE_<TYPE> which is appended to the list of destruction objects to be
86 * -# returns a pointer to the created object.
88 * Note that the two created objects are deleted at the end of the process in the function Nettoyage().
90 template <class TYPE> TYPE *SINGLETON_<TYPE>::Instance( void )
94 MESSAGE("SINGLETON_<TYPE>::Instance( void )") ;
95 PtrSingleton = new SINGLETON_<TYPE> ;
96 new DESTRUCTEUR_DE_<TYPE>( PtrSingleton->_Instance ) ;
98 return &PtrSingleton->_Instance ;
102 template <class TYPE> bool SINGLETON_<TYPE>::IsAlreadyExisting( void )
104 return PtrSingleton ? true : false ;
110 template <class TYPE> SINGLETON_<TYPE>::SINGLETON_( void )
112 MESSAGE("CREATION d'un SINGLETON_") ;
119 The method SINGLETON_<TYPE>::Destruction can be called by the user. If it is not
120 the function nettoyage() calls it atexit.
122 N.B. : the singleton objects are destroyed in the reverse order of there creation.
124 template <class TYPE> int SINGLETON_<TYPE>::Destruction( void )
127 BEGIN_OF("SINGLETON_<TYPE>::Destruction( void )") ;
130 MESSAGE("Destruction du SINGLETON_") ;
133 list<DESTRUCTEUR_GENERIQUE_ *>::iterator k ;
134 for( k=DESTRUCTEUR_GENERIQUE_::Destructeurs.begin() ; k!=DESTRUCTEUR_GENERIQUE_::Destructeurs.end();k++)
136 if ( *k == PtrSingleton->_Instance )
138 DESTRUCTEUR_GENERIQUE_::Destructeurs.erase( k ) ;
142 delete PtrSingleton ;
143 PtrSingleton = NULL ;
145 END_OF("SINGLETON_<TYPE>::Destruction( void )") ;
150 template <class TYPE> SINGLETON_<TYPE>::~SINGLETON_()
152 MESSAGE("passage dans SINGLETON_<TYPE>::~SINGLETON_( void )") ;
155 # endif /* # if !defined( __SINGLETON__H__ ) */