Salome HOME
First stable version after merging with V3_2_2
[modules/kernel.git] / src / Utils / Utils_DESTRUCTEUR_GENERIQUE.cxx
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.cxx
25 //  Author : Antoine YESSAYAN, EDF
26 //  Module : SALOME
27 //  $Header$
28
29
30 # include <iostream>
31 # include <list>
32 extern "C"
33 {
34 # include <stdlib.h>
35 }
36
37 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
38 //# include "utilities.h"
39 void Nettoyage();
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 using namespace std;
48
49 std::list<DESTRUCTEUR_GENERIQUE_*> *DESTRUCTEUR_GENERIQUE_::Destructeurs=0 ;
50
51 /*! \class ATEXIT_
52  *
53  * Mecanisme pour faire executer une seule fois DESTRUCTEUR_GENERIQUE_::Nettoyage
54  * a la fin du traitement : creation d'un singleton statique de l'objet
55  * tres specialise ATEXIT_.
56  *
57  * La creation d'un objet de type ATEXIT_ entraine l'inscription de la fonction
58  * Nettoyage() par atexit(). Il suffit donc de creer un singleton statique du type ATEXIT_
59  * pour effectuer cet enregistrement une seule fois independament de l'utilisateur.
60  */
61
62 //CCRT
63 static bool ATEXIT_Done = false ;
64 //CCRT
65
66 class ATEXIT_
67 {
68 public :
69         /*!
70          * Allocation dynamique de Destructeurs, une liste chainee de DESTRUCTEUR_GENERIQUE_* et enregistrement
71          * de la fonction Nettoyage() par atexit().
72          *
73          * La liste chainee Destructeurs est detruite dans la fonction Nettoyage.
74          */
75         //CCRT  ATEXIT_( void )
76         ATEXIT_( bool Make_ATEXIT )
77         {
78           //CCRT
79           if ( Make_ATEXIT && !ATEXIT_Done ) {
80             //CCRT
81                 assert (DESTRUCTEUR_GENERIQUE_::Destructeurs==0);
82                 //cerr << "ATEXIT_::ATEXIT_ Construction ATEXIT" << endl;// message necessaire pour utiliser logger dans Nettoyage (cf.BUG KERNEL4561)
83                 DESTRUCTEUR_GENERIQUE_::Destructeurs = 
84                       new std::list<DESTRUCTEUR_GENERIQUE_*> ; // Destructeur alloue dynamiquement (cf. ci-dessous) ,
85                                                                    // il est utilise puis detruit par la fonction Nettoyage
86                 int cr = atexit( Nettoyage );                      // execute Nettoyage lors de exit, aprs la destruction des donnees statiques !
87                 assert(cr==0) ;
88                 ATEXIT_Done = true ;
89           }
90         }
91
92         ~ATEXIT_( )
93         {
94           //cerr << "ATEXIT_::~ATEXIT_ Destruction ATEXIT" << endl;
95         }
96 };
97
98
99
100
101 static ATEXIT_ nettoyage = ATEXIT_( false );    /* singleton statique */
102
103
104 /*!
105  * traitement effectue :
106  * -# execution de tous les objets de type DESTRUCTEUR_DE_ stockes dans la liste Destructeurs (ce qui detruit les
107  *    singletons correspondant) ;
108  * -# puis destruction de tous les objets de type DESTRUCTEUR_DE_ stockes dans la liste Destructeurs;
109  * -# destruction de la liste Destructeurs.
110  */
111
112 void Nettoyage( void )
113 {
114   //cerr << "Nettoyage()" << endl;
115   //if(MYDEBUG) BEGIN_OF("Nettoyage( void )") ;
116         assert(DESTRUCTEUR_GENERIQUE_::Destructeurs) ;
117         //if(MYDEBUG) SCRUTE( DESTRUCTEUR_GENERIQUE_::Destructeurs->size() ) ;
118         if( DESTRUCTEUR_GENERIQUE_::Destructeurs->size() )
119         {
120                 std::list<DESTRUCTEUR_GENERIQUE_*>::iterator it = DESTRUCTEUR_GENERIQUE_::Destructeurs->end() ;
121
122                 do
123                 {
124                   //if(MYDEBUG) MESSAGE( "DESTRUCTION d'un SINGLETON");
125                         it-- ;
126                         DESTRUCTEUR_GENERIQUE_* ptr = *it ;
127                         //DESTRUCTEUR_GENERIQUE_::Destructeurs->remove( *it ) ;
128                         (*ptr)() ;
129                         delete ptr ;
130                 }while( it!=  DESTRUCTEUR_GENERIQUE_::Destructeurs->begin() ) ;
131
132                 DESTRUCTEUR_GENERIQUE_::Destructeurs->clear() ;
133                 //if(MYDEBUG) SCRUTE( DESTRUCTEUR_GENERIQUE_::Destructeurs->size() ) ;
134                 assert( DESTRUCTEUR_GENERIQUE_::Destructeurs->size()==0 ) ;
135                 assert( DESTRUCTEUR_GENERIQUE_::Destructeurs->empty() ) ;
136         }
137
138         delete DESTRUCTEUR_GENERIQUE_::Destructeurs;
139         DESTRUCTEUR_GENERIQUE_::Destructeurs=0;
140         //if(MYDEBUG) END_OF("Nettoyage( void )") ;
141         return ;
142 }
143
144
145 /*!
146  * Adds a destruction object to the list of actions to be performed at the end
147  * of the process
148  */
149 const int DESTRUCTEUR_GENERIQUE_::Ajout( DESTRUCTEUR_GENERIQUE_ &objet )
150 {
151         // N.B. : l'ordre de creation des SINGLETON etant important
152         //        on n'utilise pas deux fois la meme position pour
153         //        les stocker dans la pile des objets.
154
155         //CCRT
156         if ( !ATEXIT_Done ) {
157           nettoyage = ATEXIT_( true ) ;
158         }
159         //CCRT
160         assert(Destructeurs) ;
161         Destructeurs->push_back( &objet ) ;
162         return Destructeurs->size() ;
163 }