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