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