Salome HOME
Porting to Mandrake 10.1 and new products:
[modules/kernel.git] / src / Utils / Utils_SINGLETON.hxx
1 //  SALOME Utils : general SALOME's definitions and tools
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Utils_SINGLETON.hxx
25 //  Author : Antoine YESSAYAN, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 # if !defined( __SINGLETON__H__ )
30 # define __SINGLETON__H__
31
32 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
33 # include <list>
34
35 /*!\class SINGLETON_
36  *
37  * <B>Definition</B>
38  * 
39  * A singleton is a data which is created and deleted only once in the application.
40  * The C++ compiler allow the user to create static data before the first executable statement.
41  * They are deleted after the last statement.statement.
42  *
43  * The SINGLETON_ template class deals with dynamic singleton. It is useful for functor objects.
44  * For example, an object which, when created, connects the application to a system and
45  * disconnects the application at deletion.
46  *
47  *
48  * <B>Usage</B>
49  * 
50  * To create a single instance a POINT_ object :
51  * 
52  * # include "Utils_SINGLETON.hxx"
53  *      ...
54  *      ptrPoint = SINGLETON_<POINT_>::Instance() ;
55  * 
56  * 
57  * <B>Design description</B>
58  *
59  *      -# the user creates an object of class TYPE By using a class method : SINGLETON_<TYPE>::Instance() which
60  *         returns a pointer to the single object ;
61  *      -# this class method uses the default constructor to create an object ;
62  *      -# at the same time, this class method reate a destructor object which is added to the generic list
63  *         of destructors objects to be executed at the end of the application (atexit) ;
64  *      -# at the end of the application process all the deletions are performed by the Nettoyage() C function
65  *         which execute the destructions objects then deletes the destructions objects themselves ;
66  *      -# the Nettoyage() C function is recorded using atexit() C function through the creation of a static
67  *         single object ATEXIT_().
68  */
69
70
71 template <class TYPE> class SINGLETON_
72 {
73
74 public :
75
76         static TYPE *Instance( void );          //!< Singleton dynamic creation using the default builder
77         static bool IsAlreadyExisting( void );  //!< returns True if the singleton is already existing
78         static int Destruction( void );         //!< destroys the Singleton before the end of the application process
79
80 private :
81
82         TYPE _Instance ;
83         static SINGLETON_ *PtrSingleton ;
84
85         SINGLETON_( void );
86         ~SINGLETON_();
87
88 } ;     /* class SINGLETON_<TYPE> */
89
90
91
92
93 template <class TYPE> SINGLETON_<TYPE> *SINGLETON_<TYPE>::PtrSingleton=NULL ;
94
95
96
97 /*!
98  * The class method Instance :
99  *  -# creates an object of class TYPE ;
100  *  -# creates a destruction object DESTRUCTEUR_DE_<TYPE> which is appended to the list of destruction objects to be
101  *     executed ;
102  *  -# returns a pointer to the created object.
103  *
104  *  Note that the two created objects are deleted at the end of the process in the function Nettoyage().
105  */
106 template <class TYPE> TYPE *SINGLETON_<TYPE>::Instance( void )
107 {
108         if ( ! PtrSingleton )
109         {
110                 //MESSAGE("SINGLETON_<TYPE>::Instance( void )") ;
111                 PtrSingleton = new SINGLETON_<TYPE> ;
112                 new DESTRUCTEUR_DE_<TYPE>( PtrSingleton->_Instance ) ;
113         }
114         return &PtrSingleton->_Instance ;
115 }
116
117
118 template <class TYPE> bool SINGLETON_<TYPE>::IsAlreadyExisting( void )
119 {
120         return PtrSingleton ? true : false ;
121 }
122
123
124
125
126 template <class TYPE> SINGLETON_<TYPE>::SINGLETON_( void )
127 {
128         //MESSAGE("CREATION d'un SINGLETON_") ;
129 }
130
131
132
133
134 /*!
135         The method SINGLETON_<TYPE>::Destruction can be called by the user. If it is not
136         the function nettoyage() calls it atexit.
137
138         N.B. : the singleton objects are destroyed in the reverse order of there creation.
139 */
140 template <class TYPE> int SINGLETON_<TYPE>::Destruction( void )
141 {
142         int k = - 1 ;
143         BEGIN_OF("SINGLETON_<TYPE>::Destruction( void )") ;
144         if ( PtrSingleton )
145         {
146                 MESSAGE("Destruction du SINGLETON_") ;
147
148
149                 std::list<DESTRUCTEUR_GENERIQUE_ *>::iterator k ;
150                 for( k=DESTRUCTEUR_GENERIQUE_::Destructeurs.begin() ; k!=DESTRUCTEUR_GENERIQUE_::Destructeurs.end();k++)
151                 {
152                         if ( *k == PtrSingleton->_Instance )
153                         {
154                                 DESTRUCTEUR_GENERIQUE_::Destructeurs.erase( k ) ;
155                                 break ;
156                         }
157                 }
158                 delete PtrSingleton ;
159                 PtrSingleton = NULL ;
160         }
161         END_OF("SINGLETON_<TYPE>::Destruction( void )") ;
162         return k ;
163 }
164
165
166 template <class TYPE> SINGLETON_<TYPE>::~SINGLETON_()
167 {
168         MESSAGE("passage dans SINGLETON_<TYPE>::~SINGLETON_( void )") ;
169 }
170
171 # endif         /* # if !defined( __SINGLETON__H__ ) */