1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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
23 // SALOME Basics : general SALOME definitions and tools (C++ part - no CORBA)
24 // File : BasicGenericDestructor.hxx
25 // Author : Antoine YESSAYAN, Paul RASCLE, EDF
29 #ifndef _BASICGENERICDESTRUCTOR_HXX_
30 #define _BASICGENERICDESTRUCTOR_HXX_
32 #include "SALOME_Basics.hxx"
45 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
46 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
53 #pragma warning(disable:4251) // Warning DLL Interface ...
56 // ============================================================================
58 * The PROTECTED_DELETE base class provides a protected destructor.
59 * The only way to use PROTECTED_DELETE is inheritance:
60 * example: class LocalTraceBufferPool : public PROTECTED_DELETE
61 * Herited class destructor must stay protected.
63 * - create an instance of herited class on the heap (new),
64 * - use addObj(instance) to store the instance on the static list _objList,
65 * - delete instance with deleteInstance(instance)
67 * This class is utilised with GENERIC_DESTRUCTOR and DESTRUCTOR_OF,
68 * to program automatic deletion of objects at the end of the process, while
69 * keeping the possibility of an early destruction, if required. This is used
70 * for unit testing and trace mechanism.
72 // ============================================================================
74 class BASICS_EXPORT PROTECTED_DELETE
77 static void deleteInstance(PROTECTED_DELETE *anObject);
78 static void addObj(PROTECTED_DELETE *anObject);
81 virtual ~PROTECTED_DELETE();
82 static std::list<PROTECTED_DELETE*> _objList;
85 static pthread_mutex_t _listMutex;
88 // ============================================================================
90 * The GENERIC_DESTRUCTOR abstract class describes the comportement of any
91 * destruction object. This type is used to create a list of miscellaneous
92 * destruction objects.
94 * The only way to use the GENERIC_DESTRUCTOR class is inheritance:
95 * class SPECIFIC_DESTRUCTOR : public GENERIC_DESTRUCTOR
97 * A generic destructor provides two methods:
98 * -# a static method to add a destruction (object) to be performed:
99 * GENERIC_DESTRUCTOR::Add(GENERIC_DESTRUCTOR &anObject);
100 * The Destruction object is stored in a list of pointer to
101 * GENERIC_DESTRUCTOR objects.
102 * -# an object method to execute the destruction : operator()().
104 // ============================================================================
106 class BASICS_EXPORT GENERIC_DESTRUCTOR
109 static std::list<GENERIC_DESTRUCTOR*> *Destructors;
111 virtual ~GENERIC_DESTRUCTOR() {};
112 static int Add(GENERIC_DESTRUCTOR &anObject);
113 virtual void operator()(void) = 0;
116 // ============================================================================
118 * The DESTRUCTOR_OF class allows the user to program - at any moment - the
119 * destruction of an object at the end of the process.
121 * Example: the POINT ptrPoint will be destroyed at the end of the process
124 * POINT *ptrPoint = new POINT ;
126 * DESTRUCTOR_OF<POINT> *ptrDestruct = new DESTRUCTOR_OF<POINT>(*ptrPoint);
128 * Note that neither ptrPoint, nor ptrDestruct should be destroyed by the user.
130 * The destruction object must be created dynamically because it subscribes
131 * itself in the list of destruction to be performed at the end of the process.
133 // ============================================================================
135 template <class TYPE> class DESTRUCTOR_OF : public GENERIC_DESTRUCTOR
139 Programs the destruction at the end of the process, of the object anObject.
140 This method records in _objectPtr the address of an object to be destroyed
141 at the end of the process
143 DESTRUCTOR_OF(TYPE &anObject):
144 _objectPtr(&anObject)
146 DEVTRACE(" DESTRUCTOR_OF " << typeid(anObject).name()
147 << " " << _objectPtr << " " << this );
148 PROTECTED_DELETE::addObj(_objectPtr);
149 assert(GENERIC_DESTRUCTOR::Add(*this) >= 0);
153 Performs the destruction of the object.
154 This method really destroys the object pointed by _objectPtr.
155 It should be called at the end of the process (i.e. at exit).
157 virtual void operator()(void)
161 DEVTRACE("DESTRUCTOR_OF<>::operator() " << _objectPtr);
162 if (_objectPtr) PROTECTED_DELETE::deleteInstance(_objectPtr);
167 virtual ~DESTRUCTOR_OF()
169 DEVTRACE("~DESTRUCTOR_OF() " << this);