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