SALOME documentation central

src/Utils/Utils_SINGLETON.hxx

00001 //  Copyright (C) 2007-2008  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 //  SALOME Utils : general SALOME's definitions and tools
00023 //  File   : Utils_SINGLETON.hxx
00024 //  Author : Antoine YESSAYAN, EDF
00025 //  Module : SALOME
00026 //  $Header$
00027 //
00028 # if !defined( __SINGLETON__H__ )
00029 # define __SINGLETON__H__
00030 
00031 #include "SALOME_Utils.hxx"
00032 
00033 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
00034 # include <list>
00035 
00072 template <class TYPE> class SINGLETON_
00073 {
00074 
00075 public :
00076 
00077      static TYPE *Instance( void );          
00078      static bool IsAlreadyExisting( void );  
00079      static int Destruction( void );         
00080 
00081 private :
00082 
00083      TYPE _Instance ;
00084      static SINGLETON_ *PtrSingleton ;
00085 
00086      SINGLETON_( void );
00087      ~SINGLETON_();
00088 
00089 } ;  /* class SINGLETON_<TYPE> */
00090 
00091 
00092 
00093 
00094 template <class TYPE> SINGLETON_<TYPE> *SINGLETON_<TYPE>::PtrSingleton=NULL ;
00095 
00096 
00097 
00107 template <class TYPE> TYPE *SINGLETON_<TYPE>::Instance( void )
00108 {
00109      if ( ! PtrSingleton )
00110      {
00111           //MESSAGE("SINGLETON_<TYPE>::Instance( void )") ;
00112           PtrSingleton = new SINGLETON_<TYPE> ;
00113           new DESTRUCTEUR_DE_<TYPE>( PtrSingleton->_Instance ) ;
00114      }
00115      return &PtrSingleton->_Instance ;
00116 }
00117 
00118 
00119 template <class TYPE> bool SINGLETON_<TYPE>::IsAlreadyExisting( void )
00120 {
00121      return PtrSingleton ? true : false ;
00122 }
00123 
00124 
00125 
00126 
00127 template <class TYPE> SINGLETON_<TYPE>::SINGLETON_( void )
00128 {
00129      //MESSAGE("CREATION d'un SINGLETON_") ;
00130 }
00131 
00132 
00133 
00134 
00141 template <class TYPE> int SINGLETON_<TYPE>::Destruction( void )
00142 {
00143      int k = - 1 ;
00144      //BEGIN_OF("SINGLETON_<TYPE>::Destruction( void )") ;
00145      if ( PtrSingleton )
00146      {
00147        //MESSAGE("Destruction du SINGLETON_") ;
00148 
00149 
00150           std::list<DESTRUCTEUR_GENERIQUE_ *>::iterator k ;
00151           for( k=DESTRUCTEUR_GENERIQUE_::Destructeurs->begin() ; k!=DESTRUCTEUR_GENERIQUE_::Destructeurs->end();k++)
00152           {
00153                if ( *k == PtrSingleton->_Instance )
00154                {
00155                     DESTRUCTEUR_GENERIQUE_::Destructeurs->erase( k ) ;
00156                     break ;
00157                }
00158           }
00159           delete PtrSingleton ;
00160           PtrSingleton = NULL ;
00161      }
00162      //END_OF("SINGLETON_<TYPE>::Destruction( void )") ;
00163      return k ;
00164 }
00165 
00166 
00167 template <class TYPE> SINGLETON_<TYPE>::~SINGLETON_()
00168 {
00169   //MESSAGE("passage dans SINGLETON_<TYPE>::~SINGLETON_( void )") ;
00170 }
00171 
00172 # endif        /* # if !defined( __SINGLETON__H__ ) */