Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / Utils / Utils_DESTRUCTEUR_GENERIQUE.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME Utils : general SALOME's definitions and tools
23 //  File   : Utils_DESTRUCTEUR_GENERIQUE.hxx
24 //  Author : Antoine YESSAYAN, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 # if !defined( __DESTRUCTEUR_GENERIQUE__H__ )
29 # define __DESTRUCTEUR_GENERIQUE__H__
30
31 #include <SALOMEconfig.h>
32
33 #include "SALOME_Utils.hxx"
34
35 #include <list>
36 #include <cassert>
37 #include <omniORB4/CORBA.h>
38 #include <iostream>
39 //# include "utilities.h"
40
41 /*!\class DESTRUCTEUR_GENERIQUE_
42  *
43  * <B>Definition</B>
44  * 
45  * The DESTRUCTEUR_GENERIQUE_ abstract class describes the comportement of any destruction object.
46  * Tis type is used to create a list of miscellaneous destruction object.
47  *
48  * <B>Usage</B>
49  * 
50  * The only way to use the DESTRUCTEUR_GENERIQUE_ class is inheritance :
51  *      class DESTRUCTEUR_SPECIFIQUE_ : public DESTRUCTEUR_GENERIQUE_
52  * 
53  * <B>Design description</B>
54  * 
55  *      A generic destructor supply two functionalities :
56  *      -# a static method to add a destruction (objetct) to be performed DESTRUCTEUR_GENERIQUE_::Ajout(
57  *      DESTRUCTEUR_GENERIQUE_ &objet) ;
58  *         The Destruction object is stored in a list of pointer to DESTRUCTEUR_GENERIQUE_ objects.
59  *      -# an object method to execute the destruction : operator()().
60  */
61
62 class UTILS_EXPORT DESTRUCTEUR_GENERIQUE_
63 {
64 public :
65   static std::list<DESTRUCTEUR_GENERIQUE_*> *Destructeurs;
66
67   virtual ~DESTRUCTEUR_GENERIQUE_() {}//!< virtual destructor
68   static const int Ajout( DESTRUCTEUR_GENERIQUE_ &objet );//!< adds a destruction object to the list of destructions
69   virtual void operator()( void )=0 ;//!< performs the destruction
70 };
71
72
73 /*!\class DESTRUCTEUR_DE_
74  *
75  * <B>Purpose</B>
76  * 
77  * The DESTRUCTEUR_DE_ class allows the user to program - at any moment - the destruction of an object
78  * at the end of the process.
79  *
80  * <B>Usage</B>
81  * 
82  *      In this example the POINT_ ptrPoint will be destroyed at the end of the process (atexit).
83  *
84  *      POINT_ *ptrPoint = new POINT_ ;<BR>
85  *      DESTRUCTEUR_DE_<POINT_> *ptrDestruction = new DESTRUCTEUR_DE_<POINT_>( *ptrPoint ) ;
86  * 
87  *      Note that neither ptrPoint, nor ptrDestruction should be destroyed by the user.
88  * 
89  * <B>Design description</B>
90  * 
91  *      The destruction object must be created dynamicaly because it suscribes himself in the list of
92  *      destruction to be performed at the end of the process.
93  * 
94  */
95 template <class TYPE> class DESTRUCTEUR_DE_ : public DESTRUCTEUR_GENERIQUE_
96 {
97 public :
98   /* Programs the destruction at the end of the process, of the object objet.
99      This method records in _PtrObjet the address of an object to be destroyed 
100      at the end of the process
101   */
102   DESTRUCTEUR_DE_(TYPE &objet):
103     _PtrObjet( &objet )
104   {
105     assert(DESTRUCTEUR_GENERIQUE_::Ajout( *this ) >= 0) ;
106   }
107
108   /* Performs the destruction of the object.
109      This method really destroys the object pointed by _PtrObjet. 
110      It should be called at the end of the process (i.e. at exit).
111   */
112   virtual void operator()(void){
113     typedef PortableServer::ServantBase TServant;
114     if(_PtrObjet){
115       if(dynamic_cast<TServant*>(_PtrObjet)){
116        // std::cerr << "WARNING: automatic destruction for servant is no more used. It's too late in exit. Use explicit call" << std::endl;
117   /*
118       if(TServant* aServant = dynamic_cast<TServant*>(_PtrObjet)){
119         PortableServer::POA_var aPOA = aServant->_default_POA();
120         PortableServer::ObjectId_var anObjectId = aPOA->servant_to_id(aServant);
121         aPOA->deactivate_object(anObjectId.in());
122         aServant->_remove_ref();
123   */
124       }else{
125         //cerr << "DESTRUCTEUR_GENERIQUE_::operator() deleting _PtrObjet" << endl;
126         TYPE* aPtr = static_cast<TYPE*>(_PtrObjet);
127         delete aPtr;
128       }
129       _PtrObjet = NULL ;
130     }
131   } 
132
133   virtual ~DESTRUCTEUR_DE_(){
134     assert(!_PtrObjet) ;
135   }
136
137 private :
138   TYPE *_PtrObjet ;
139 };
140
141
142 # endif         /* # if !defined( __DESTRUCTEUR_GENERIQUE__H__ ) */