Back to index

salome-kernel  6.5.0
Utils_SINGLETON.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Utils : general SALOME's definitions and tools
00024 //  File   : Utils_SINGLETON.hxx
00025 //  Author : Antoine YESSAYAN, EDF
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Utils/Utils_SINGLETON.hxx,v 1.9.2.1.10.2.12.1 2012-04-12 14:05:32 vsr Exp $
00028 //
00029 # if !defined( __SINGLETON__H__ )
00030 # define __SINGLETON__H__
00031 
00032 #include "SALOME_Utils.hxx"
00033 
00034 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
00035 # include <list>
00036 
00073 template <class TYPE> class SINGLETON_
00074 {
00075 
00076 public :
00077 
00078         static TYPE *Instance( void );          
00079         static bool IsAlreadyExisting( void );  
00080         static int Destruction( void );         
00081 
00082 private :
00083 
00084         TYPE _Instance ;
00085         static SINGLETON_ *PtrSingleton ;
00086 
00087         SINGLETON_( void );
00088         ~SINGLETON_();
00089 
00090 } ;     /* class SINGLETON_<TYPE> */
00091 
00092 
00093 
00094 
00095 template <class TYPE> SINGLETON_<TYPE> *SINGLETON_<TYPE>::PtrSingleton=NULL ;
00096 
00097 
00098 
00108 template <class TYPE> TYPE *SINGLETON_<TYPE>::Instance( void )
00109 {
00110         if ( ! PtrSingleton )
00111         {
00112                 //MESSAGE("SINGLETON_<TYPE>::Instance( void )") ;
00113                 PtrSingleton = new SINGLETON_<TYPE> ;
00114                 new DESTRUCTEUR_DE_<TYPE>( PtrSingleton->_Instance ) ;
00115         }
00116         return &PtrSingleton->_Instance ;
00117 }
00118 
00119 
00120 template <class TYPE> bool SINGLETON_<TYPE>::IsAlreadyExisting( void )
00121 {
00122         return PtrSingleton ? true : false ;
00123 }
00124 
00125 
00126 
00127 
00128 template <class TYPE> SINGLETON_<TYPE>::SINGLETON_( void )
00129 {
00130         //MESSAGE("CREATION d'un SINGLETON_") ;
00131 }
00132 
00133 
00134 
00135 
00142 template <class TYPE> int SINGLETON_<TYPE>::Destruction( void )
00143 {
00144         int k = - 1 ;
00145         //BEGIN_OF("SINGLETON_<TYPE>::Destruction( void )") ;
00146         if ( PtrSingleton )
00147         {
00148           //MESSAGE("Destruction du SINGLETON_") ;
00149 
00150 
00151                 std::list<DESTRUCTEUR_GENERIQUE_ *>::iterator k ;
00152                 for( k=DESTRUCTEUR_GENERIQUE_::Destructeurs->begin() ; k!=DESTRUCTEUR_GENERIQUE_::Destructeurs->end();k++)
00153                 {
00154                         if ( *k == PtrSingleton->_Instance )
00155                         {
00156                                 DESTRUCTEUR_GENERIQUE_::Destructeurs->erase( k ) ;
00157                                 break ;
00158                         }
00159                 }
00160                 delete PtrSingleton ;
00161                 PtrSingleton = NULL ;
00162         }
00163         //END_OF("SINGLETON_<TYPE>::Destruction( void )") ;
00164         return k ;
00165 }
00166 
00167 
00168 template <class TYPE> SINGLETON_<TYPE>::~SINGLETON_()
00169 {
00170   //MESSAGE("passage dans SINGLETON_<TYPE>::~SINGLETON_( void )") ;
00171 }
00172 
00173 # endif         /* # if !defined( __SINGLETON__H__ ) */