Salome HOME
Base implementation of Notebook
[modules/kernel.git] / src / Utils / Utils_SINGLETON.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME Utils : general SALOME's definitions and tools
23 //  File   : Utils_SINGLETON.hxx
24 //  Author : Antoine YESSAYAN, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 # if !defined( __SINGLETON__H__ )
29 # define __SINGLETON__H__
30
31 #include "SALOME_Utils.hxx"
32
33 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
34 # include <list>
35
36 /*!\class SINGLETON_
37  *
38  * <B>Definition</B>
39  * 
40  * A singleton is a data which is created and deleted only once in the application.
41  * The C++ compiler allow the user to create static data before the first executable statement.
42  * They are deleted after the last statement.statement.
43  *
44  * The SINGLETON_ template class deals with dynamic singleton. It is useful for functor objects.
45  * For example, an object which, when created, connects the application to a system and
46  * disconnects the application at deletion.
47  *
48  *
49  * <B>Usage</B>
50  * 
51  * To create a single instance a POINT_ object :
52  * 
53  * # include "Utils_SINGLETON.hxx"
54  *      ...
55  *      ptrPoint = SINGLETON_<POINT_>::Instance() ;
56  * 
57  * 
58  * <B>Design description</B>
59  *
60  *      -# the user creates an object of class TYPE By using a class method : SINGLETON_<TYPE>::Instance() which
61  *         returns a pointer to the single object ;
62  *      -# this class method uses the default constructor to create an object ;
63  *      -# at the same time, this class method reate a destructor object which is added to the generic list
64  *         of destructors objects to be executed at the end of the application (atexit) ;
65  *      -# at the end of the application process all the deletions are performed by the Nettoyage() C function
66  *         which execute the destructions objects then deletes the destructions objects themselves ;
67  *      -# the Nettoyage() C function is recorded using atexit() C function through the creation of a static
68  *         single object ATEXIT_().
69  */
70
71
72 template <class TYPE> class SINGLETON_
73 {
74
75 public :
76
77         static TYPE *Instance( void );          //!< Singleton dynamic creation using the default builder
78         static bool IsAlreadyExisting( void );  //!< returns True if the singleton is already existing
79         static int Destruction( void );         //!< destroys the Singleton before the end of the application process
80
81 private :
82
83         TYPE _Instance ;
84         static SINGLETON_ *PtrSingleton ;
85
86         SINGLETON_( void );
87         ~SINGLETON_();
88
89 } ;     /* class SINGLETON_<TYPE> */
90
91
92
93
94 template <class TYPE> SINGLETON_<TYPE> *SINGLETON_<TYPE>::PtrSingleton=NULL ;
95
96
97
98 /*!
99  * The class method Instance :
100  *  -# creates an object of class TYPE ;
101  *  -# creates a destruction object DESTRUCTEUR_DE_<TYPE> which is appended to the list of destruction objects to be
102  *     executed ;
103  *  -# returns a pointer to the created object.
104  *
105  *  Note that the two created objects are deleted at the end of the process in the function Nettoyage().
106  */
107 template <class TYPE> TYPE *SINGLETON_<TYPE>::Instance( void )
108 {
109         if ( ! PtrSingleton )
110         {
111                 //MESSAGE("SINGLETON_<TYPE>::Instance( void )") ;
112                 PtrSingleton = new SINGLETON_<TYPE> ;
113                 new DESTRUCTEUR_DE_<TYPE>( PtrSingleton->_Instance ) ;
114         }
115         return &PtrSingleton->_Instance ;
116 }
117
118
119 template <class TYPE> bool SINGLETON_<TYPE>::IsAlreadyExisting( void )
120 {
121         return PtrSingleton ? true : false ;
122 }
123
124
125
126
127 template <class TYPE> SINGLETON_<TYPE>::SINGLETON_( void )
128 {
129         //MESSAGE("CREATION d'un SINGLETON_") ;
130 }
131
132
133
134
135 /*!
136         The method SINGLETON_<TYPE>::Destruction can be called by the user. If it is not
137         the function nettoyage() calls it atexit.
138
139         N.B. : the singleton objects are destroyed in the reverse order of there creation.
140 */
141 template <class TYPE> int SINGLETON_<TYPE>::Destruction( void )
142 {
143         int k = - 1 ;
144         //BEGIN_OF("SINGLETON_<TYPE>::Destruction( void )") ;
145         if ( PtrSingleton )
146         {
147           //MESSAGE("Destruction du SINGLETON_") ;
148
149
150                 std::list<DESTRUCTEUR_GENERIQUE_ *>::iterator k ;
151                 for( k=DESTRUCTEUR_GENERIQUE_::Destructeurs->begin() ; k!=DESTRUCTEUR_GENERIQUE_::Destructeurs->end();k++)
152                 {
153                         if ( *k == PtrSingleton->_Instance )
154                         {
155                                 DESTRUCTEUR_GENERIQUE_::Destructeurs->erase( k ) ;
156                                 break ;
157                         }
158                 }
159                 delete PtrSingleton ;
160                 PtrSingleton = NULL ;
161         }
162         //END_OF("SINGLETON_<TYPE>::Destruction( void )") ;
163         return k ;
164 }
165
166
167 template <class TYPE> SINGLETON_<TYPE>::~SINGLETON_()
168 {
169   //MESSAGE("passage dans SINGLETON_<TYPE>::~SINGLETON_( void )") ;
170 }
171
172 # endif         /* # if !defined( __SINGLETON__H__ ) */