Back to index

salome-kernel  6.5.0
BasicsGenericDestructor.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 Basics : general SALOME definitions and tools (C++ part - no CORBA)
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.5.2.1.10.2.12.1 2012-04-12 14:05:02 vsr Exp $
00028 //
00029 #ifndef _BASICGENERICDESTRUCTOR_HXX_
00030 #define _BASICGENERICDESTRUCTOR_HXX_
00031 
00032 #include "SALOME_Basics.hxx"
00033 
00034 #include <list>
00035 #include <algorithm>
00036 #include <cassert>
00037 #include <typeinfo>
00038 #include <iostream>
00039 #include <cstdlib>
00040 #include <pthread.h>
00041 
00042 //#define _DEVDEBUG_
00043 
00044 #ifdef _DEVDEBUG_
00045 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
00046 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
00047 #else
00048 #define MYDEVTRACE
00049 #define DEVTRACE(msg)
00050 #endif
00051 
00052 #ifdef WNT
00053 #pragma warning(disable:4251) // Warning DLL Interface ...
00054 #endif
00055 
00056 // ============================================================================
00072 // ============================================================================
00073 
00074 class BASICS_EXPORT PROTECTED_DELETE
00075 {
00076 public:
00077   static void deleteInstance(PROTECTED_DELETE *anObject);
00078   static void addObj(PROTECTED_DELETE *anObject);
00079 
00080 protected:
00081   virtual ~PROTECTED_DELETE();
00082   static std::list<PROTECTED_DELETE*> _objList;
00083 
00084 private:
00085   static pthread_mutex_t _listMutex;
00086 };
00087 
00088 // ============================================================================
00104 // ============================================================================
00105 
00106 class BASICS_EXPORT GENERIC_DESTRUCTOR
00107 {
00108 public :
00109   static std::list<GENERIC_DESTRUCTOR*> *Destructors;
00110 
00111   virtual ~GENERIC_DESTRUCTOR() {};
00112   static const int Add(GENERIC_DESTRUCTOR &anObject);
00113   virtual void operator()(void) = 0;
00114 };
00115 
00116 // ============================================================================
00133 // ============================================================================
00134 
00135 template <class TYPE> class DESTRUCTOR_OF : public GENERIC_DESTRUCTOR
00136 {
00137 public:
00143   DESTRUCTOR_OF(TYPE &anObject):
00144     _objectPtr(&anObject)
00145   {
00146     DEVTRACE(" DESTRUCTOR_OF " << typeid(anObject).name() 
00147              << " " << _objectPtr << " " << this );
00148     PROTECTED_DELETE::addObj(_objectPtr);
00149     assert(GENERIC_DESTRUCTOR::Add(*this) >= 0);
00150   }
00151 
00157   virtual void operator()(void)
00158   {
00159     if (_objectPtr)
00160       {
00161         DEVTRACE("DESTRUCTOR_OF<>::operator() " << _objectPtr);
00162         if (_objectPtr) PROTECTED_DELETE::deleteInstance(_objectPtr);
00163         _objectPtr = NULL;
00164       }
00165   }
00166 
00167   virtual ~DESTRUCTOR_OF()
00168   {
00169     DEVTRACE("~DESTRUCTOR_OF() " << this);
00170     assert(!_objectPtr);
00171   }
00172 
00173 private:
00174   TYPE *_objectPtr;
00175 };
00176 
00177 # endif