Salome HOME
Initialisation de la base KERNEL avec la version operationnelle de KERNEL_SRC issue...
[modules/kernel.git] / src / Utils / Utils_DESTRUCTEUR_GENERIQUE.hxx
1 //=============================================================================
2 // File      : Utils_DESTRUCTEUR_GENERIQUE.hxx
3 // Created   : Mon Nov  5 17:01:47 CET 2001
4 // Author    : Antoine YESSAYAN, EDF
5 // Project   : SALOME
6 // Copyright : EDF 2001
7 // $Header$
8 //=============================================================================
9
10 # if !defined( __DESTRUCTEUR_GENERIQUE__H__ )
11 # define __DESTRUCTEUR_GENERIQUE__H__
12
13 # include "utilities.h"
14
15 /*!\class DESTRUCTEUR_GENERIQUE_
16  *
17  * <B>Definition</B>
18  * 
19  * The DESTRUCTEUR_GENERIQUE_ abstract class describes the comportement of any destruction object.
20  * Tis type is used to create a list of miscellaneous destruction object.
21  *
22  * <B>Usage</B>
23  * 
24  * The only way to use the DESTRUCTEUR_GENERIQUE_ class is inheritance :
25  *      class DESTRUCTEUR_SPECIFIQUE_ : public DESTRUCTEUR_GENERIQUE_
26  * 
27  * <B>Design description</B>
28  * 
29  *      A generic destructor supply two functionalities :
30  *      -# a static method to add a destruction (objetct) to be performed DESTRUCTEUR_GENERIQUE_::Ajout(
31  *      DESTRUCTEUR_GENERIQUE_ &objet) ;
32  *         The Destruction object is stored in a list of pointer to DESTRUCTEUR_GENERIQUE_ objects.
33  *      -# an object method to execute the destruction : operator()().
34  */
35
36 class DESTRUCTEUR_GENERIQUE_
37 {
38 public :
39         virtual ~DESTRUCTEUR_GENERIQUE_() {}//!< virtual destructor
40         static const int Ajout( DESTRUCTEUR_GENERIQUE_ &objet );//!< adds a destruction object to the list of destructions
41         virtual void operator()( void )=0 ;//!< performs the destruction
42 } ;
43
44
45
46
47
48
49 /*!\class DESTRUCTEUR_DE_
50  *
51  * <B>Purpose</B>
52  * 
53  * The DESTRUCTEUR_DE_ class allows the user to program - at any moment - the destruction of an object
54  * at the end of the process.
55  *
56  * <B>Usage</B>
57  * 
58  *      In this example the POINT_ ptrPoint will be destroyed at the end of the process (atexit).
59  *
60  *      POINT_ *ptrPoint = new POINT_ ;<BR>
61  *      DESTRUCTEUR_DE_<POINT_> *ptrDestruction = new DESTRUCTEUR_DE_<POINT_>( *ptrPoint ) ;
62  * 
63  *      Note that neither ptrPoint, nor ptrDestruction should be destroyed by the user.
64  * 
65  * <B>Design description</B>
66  * 
67  *      The destruction object must be created dynamicaly because it suscribes himself in the list of
68  *      destruction to be performed at the end of the process.
69  * 
70  */
71
72 template <class TYPE> class DESTRUCTEUR_DE_ : public DESTRUCTEUR_GENERIQUE_
73 {
74 public :
75
76         inline DESTRUCTEUR_DE_( TYPE &objet ); //! programs the destruction at the end of the process, of the object objet
77         virtual void operator()( void ) ; //!< performs the destruction of the object
78         virtual ~DESTRUCTEUR_DE_() ;
79 private :
80         const TYPE *_PtrObjet ;
81 } ;
82
83
84
85
86
87
88 /*!
89 This method records in _PtrObjet the address of an object to be destroyed at the end of the process
90 */
91 template <class TYPE> DESTRUCTEUR_DE_<TYPE>::DESTRUCTEUR_DE_( TYPE &objet ): _PtrObjet( &objet )
92 {
93         int k = DESTRUCTEUR_GENERIQUE_::Ajout( *this ) ;        
94         ASSERT(k>=0) ;
95 }
96
97 template <class TYPE> DESTRUCTEUR_DE_<TYPE>::~DESTRUCTEUR_DE_()
98 {
99         ASSERT(_PtrObjet==NULL) ;
100 }
101
102 /*!
103 This method really destroys the object pointed by _PtrObjet. It should be called at the end of the process
104 (i.e. at exit).
105 */
106 template <class TYPE> void DESTRUCTEUR_DE_<TYPE>::operator()( void )
107 {
108         if ( _PtrObjet )
109         {
110                   MESSAGE("deleting _PtrObjet") ;
111                 delete (TYPE*)_PtrObjet ;
112                 _PtrObjet = NULL ;
113         }
114 }
115
116 # endif         /* # if !defined( __SINGLETON__H__ ) */