1 // SALOME Basics : general SALOME definitions and tools (C++ part - no CORBA)
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : BasicGenericDestructor.hxx
25 // Author : Antoine YESSAYAN, Paul RASCLE, EDF
29 #ifndef _BASICGENERICDESTRUCTOR_HXX_
30 #define _BASICGENERICDESTRUCTOR_HXX_
40 #if defined BASICS_EXPORTS
42 #define BASICS_EXPORT __declspec( dllexport )
48 #define BASICS_EXPORT __declspec( dllimport )
57 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
58 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
64 // ============================================================================
66 * The PROTECTED_DELETE base class provides a protected destructor.
67 * The only way to use PROTECTED_DELETE is inheritance:
68 * example: class LocalTraceBufferPool : public PROTECTED_DELETE
69 * Herited class destructor must stay protected.
71 * - create an instance of herited class on the heap (new),
72 * - use addObj(instance) to store the instance on the static list _objList,
73 * - delete instance with deleteInstance(instance)
75 * This class is utilised with GENERIC_DESTRUCTOR and DESTRUCTOR_OF,
76 * to program automatic deletion of objects at the end of the process, while
77 * keeping the possibility of an early destruction, if required. This is used
78 * for unit testing and trace mecanism.
80 // ============================================================================
82 class PROTECTED_DELETE
85 static void deleteInstance(PROTECTED_DELETE *anObject);
86 static void addObj(PROTECTED_DELETE *anObject);
89 virtual ~PROTECTED_DELETE();
90 static std::list<PROTECTED_DELETE*> _objList;
93 static pthread_mutex_t _listMutex;
96 // ============================================================================
98 * The GENERIC_DESTRUCTOR abstract class describes the comportement of any
99 * destruction object. This type is used to create a list of miscellaneous
100 * destruction objects.
102 * The only way to use the GENERIC_DESTRUCTOR class is inheritance:
103 * class SPECIFIC_DESTRUCTOR : public GENERIC_DESTRUCTOR
105 * A generic destructor provides two methods:
106 * -# a static method to add a destruction (object) to be performed:
107 * GENERIC_DESTRUCTOR::Add(GENERIC_DESTRUCTOR &anObject);
108 * The Destruction object is stored in a list of pointer to
109 * GENERIC_DESTRUCTOR objects.
110 * -# an object method to execute the destruction : operator()().
112 // ============================================================================
114 class GENERIC_DESTRUCTOR
117 BASICS_EXPORT static std::list<GENERIC_DESTRUCTOR*> *Destructors;
119 virtual ~GENERIC_DESTRUCTOR() {};
120 BASICS_EXPORT static const int Add(GENERIC_DESTRUCTOR &anObject);
121 BASICS_EXPORT virtual void operator()(void) = 0;
124 // ============================================================================
126 * The DESTRUCTOR_OF class allows the user to program - at any moment - the
127 * destruction of an object at the end of the process.
129 * Example: the POINT ptrPoint will be destroyed at the end of the process
132 * POINT *ptrPoint = new POINT ;
134 * DESTRUCTOR_OF<POINT> *ptrDestruct = new DESTRUCTOR_OF<POINT>(*ptrPoint);
136 * Note that neither ptrPoint, nor ptrDestruct should be destroyed by the user.
138 * The destruction object must be created dynamically because it suscribes
139 * itself in the list of destruction to be performed at the end of the process.
141 // ============================================================================
143 template <class TYPE> class DESTRUCTOR_OF : public GENERIC_DESTRUCTOR
148 Programs the destruction at the end of the process, of the object anObject.
149 This method records in _objectPtr the address of an object to be destroyed
150 at the end of the process
152 DESTRUCTOR_OF(TYPE &anObject):
153 _objectPtr(&anObject)
155 DEVTRACE(" DESTRUCTOR_OF " << typeid(anObject).name()
156 << " " << _objectPtr << " " << this );
157 PROTECTED_DELETE::addObj(_objectPtr);
158 assert(GENERIC_DESTRUCTOR::Add(*this) >= 0);
162 Performs the destruction of the object.
163 This method really destroys the object pointed by _objectPtr.
164 It should be called at the end of the process (i.e. at exit).
166 virtual void operator()(void)
170 DEVTRACE("DESTRUCTOR_OF<>::operator() " << _objectPtr);
171 if (_objectPtr) PROTECTED_DELETE::deleteInstance(_objectPtr);
176 virtual ~DESTRUCTOR_OF()
178 DEVTRACE("~DESTRUCTOR_OF() " << this);