General KERNEL Documentation     End User KERNEL Services  


src/Basics/BasicsGenericDestructor.hxx

Go to the documentation of this file.
00001 //  SALOME Basics : general SALOME definitions and tools (C++ part - no CORBA)
00002 //
00003 //  Copyright (C) 2003  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 //
00024 //  File   : BasicGenericDestructor.hxx
00025 //  Author : Antoine YESSAYAN, Paul RASCLE, EDF
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Basics/BasicsGenericDestructor.hxx,v 1.3 2006/06/01 11:15:15 jfa Exp $
00028 
00029 #ifndef _BASICGENERICDESTRUCTOR_HXX_
00030 #define _BASICGENERICDESTRUCTOR_HXX_
00031 
00032 #include <list>
00033 #include <algorithm>
00034 #include <cassert>
00035 #include <typeinfo>
00036 #include <iostream>
00037 #include <cstdlib>
00038 #include <pthread.h>
00039 
00040 #if defined BASICS_EXPORTS
00041 #if defined WIN32
00042 #define BASICS_EXPORT __declspec( dllexport )
00043 #else
00044 #define BASICS_EXPORT
00045 #endif
00046 #else
00047 #if defined WNT
00048 #define BASICS_EXPORT __declspec( dllimport )
00049 #else
00050 #define BASICS_EXPORT
00051 #endif
00052 #endif
00053 
00054 //#define _DEVDEBUG_
00055 
00056 #ifdef _DEVDEBUG_
00057 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
00058 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
00059 #else
00060 #define MYDEVTRACE
00061 #define DEVTRACE(msg)
00062 #endif
00063 
00064 // ============================================================================
00080 // ============================================================================
00081 
00082 class PROTECTED_DELETE
00083 {
00084 public:
00085   static void deleteInstance(PROTECTED_DELETE *anObject);
00086   static void addObj(PROTECTED_DELETE *anObject);
00087 
00088 protected:
00089   virtual ~PROTECTED_DELETE();
00090   static std::list<PROTECTED_DELETE*> _objList;
00091 
00092 private:
00093   static pthread_mutex_t _listMutex;
00094 };
00095 
00096 // ============================================================================
00112 // ============================================================================
00113 
00114 class GENERIC_DESTRUCTOR
00115 {
00116 public :
00117   BASICS_EXPORT static std::list<GENERIC_DESTRUCTOR*> *Destructors;
00118 
00119   virtual ~GENERIC_DESTRUCTOR() {};
00120   BASICS_EXPORT static const int Add(GENERIC_DESTRUCTOR &anObject);
00121   BASICS_EXPORT virtual void operator()(void) = 0;
00122 };
00123 
00124 // ============================================================================
00141 // ============================================================================
00142 
00143 template <class TYPE> class DESTRUCTOR_OF : public GENERIC_DESTRUCTOR
00144 {
00145 
00146 public:
00152   DESTRUCTOR_OF(TYPE &anObject):
00153     _objectPtr(&anObject)
00154   {
00155     DEVTRACE(" DESTRUCTOR_OF " << typeid(anObject).name() 
00156           << " " << _objectPtr << " " << this );
00157     PROTECTED_DELETE::addObj(_objectPtr);
00158     assert(GENERIC_DESTRUCTOR::Add(*this) >= 0);
00159   }
00160 
00166   virtual void operator()(void)
00167   {
00168     if (_objectPtr)
00169       {
00170      DEVTRACE("DESTRUCTOR_OF<>::operator() " << _objectPtr);
00171      if (_objectPtr) PROTECTED_DELETE::deleteInstance(_objectPtr);
00172      _objectPtr = NULL;
00173       }
00174   }
00175 
00176   virtual ~DESTRUCTOR_OF()
00177   {
00178     DEVTRACE("~DESTRUCTOR_OF() " << this);
00179     assert(!_objectPtr);
00180   }
00181 
00182 private:
00183   TYPE *_objectPtr;
00184 };
00185 
00186 # endif