Salome HOME
PR: force the transformation of message argument into string
[modules/kernel.git] / src / Utils / Utils_SINGLETON.hxx
1 //=============================================================================
2 // File      : Utils_SINGLETON.hxx
3 // Created   : lun nov  5 16:04:47 CET 2001
4 // Author    : Antoine YESSAYAN, EDF
5 // Project   : SALOME
6 // Copyright : EDF 2001
7 // $Header$
8 //=============================================================================
9
10
11
12
13 # if !defined( __SINGLETON__H__ )
14 # define __SINGLETON__H__
15
16 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
17 # include <list>
18
19 /*!\class SINGLETON_
20  *
21  * <B>Definition</B>
22  * 
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.
26  *
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.
30  *
31  *
32  * <B>Usage</B>
33  * 
34  * To create a single instance a POINT_ object :
35  * 
36  * # include "Utils_SINGLETON.hxx"
37  *      ...
38  *      ptrPoint = SINGLETON_<POINT_>::Instance() ;
39  * 
40  * 
41  * <B>Design description</B>
42  *
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_().
52  */
53
54
55 template <class TYPE> class SINGLETON_
56 {
57
58 public :
59
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
63
64 private :
65
66         TYPE _Instance ;
67         static SINGLETON_ *PtrSingleton ;
68
69         SINGLETON_( void );
70         ~SINGLETON_();
71
72 } ;     /* class SINGLETON_<TYPE> */
73
74
75
76
77 template <class TYPE> SINGLETON_<TYPE> *SINGLETON_<TYPE>::PtrSingleton=NULL ;
78
79
80
81 /*!
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
85  *     executed ;
86  *  -# returns a pointer to the created object.
87  *
88  *  Note that the two created objects are deleted at the end of the process in the function Nettoyage().
89  */
90 template <class TYPE> TYPE *SINGLETON_<TYPE>::Instance( void )
91 {
92         if ( ! PtrSingleton )
93         {
94                 MESSAGE("SINGLETON_<TYPE>::Instance( void )") ;
95                 PtrSingleton = new SINGLETON_<TYPE> ;
96                 new DESTRUCTEUR_DE_<TYPE>( PtrSingleton->_Instance ) ;
97         }
98         return &PtrSingleton->_Instance ;
99 }
100
101
102 template <class TYPE> bool SINGLETON_<TYPE>::IsAlreadyExisting( void )
103 {
104         return PtrSingleton ? true : false ;
105 }
106
107
108
109
110 template <class TYPE> SINGLETON_<TYPE>::SINGLETON_( void )
111 {
112         MESSAGE("CREATION d'un SINGLETON_") ;
113 }
114
115
116
117
118 /*!
119         The method SINGLETON_<TYPE>::Destruction can be called by the user. If it is not
120         the function nettoyage() calls it atexit.
121
122         N.B. : the singleton objects are destroyed in the reverse order of there creation.
123 */
124 template <class TYPE> int SINGLETON_<TYPE>::Destruction( void )
125 {
126         int k = - 1 ;
127         BEGIN_OF("SINGLETON_<TYPE>::Destruction( void )") ;
128         if ( PtrSingleton )
129         {
130                 MESSAGE("Destruction du SINGLETON_") ;
131
132
133                 list<DESTRUCTEUR_GENERIQUE_ *>::iterator k ;
134                 for( k=DESTRUCTEUR_GENERIQUE_::Destructeurs.begin() ; k!=DESTRUCTEUR_GENERIQUE_::Destructeurs.end();k++)
135                 {
136                         if ( *k == PtrSingleton->_Instance )
137                         {
138                                 DESTRUCTEUR_GENERIQUE_::Destructeurs.erase( k ) ;
139                                 break ;
140                         }
141                 }
142                 delete PtrSingleton ;
143                 PtrSingleton = NULL ;
144         }
145         END_OF("SINGLETON_<TYPE>::Destruction( void )") ;
146         return k ;
147 }
148
149
150 template <class TYPE> SINGLETON_<TYPE>::~SINGLETON_()
151 {
152         MESSAGE("passage dans SINGLETON_<TYPE>::~SINGLETON_( void )") ;
153 }
154
155 # endif         /* # if !defined( __SINGLETON__H__ ) */