Salome HOME
PR: mergefrom_BR_CCRT_11Nov04
[modules/yacs.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 using namespace std;
40
41 void Nettoyage();
42
43 #ifdef _DEBUG_
44 static int MYDEBUG = 0;
45 #else
46 static int MYDEBUG = 0;
47 #endif
48
49 static list<DESTRUCTEUR_GENERIQUE_*> *Destructeurs=0 ;
50
51 /*! \class ATEXIT_
52  *
53  * Mécanisme pour faire exécuter une seule fois DESTRUCTEUR_GENERIQUE_::Nettoyage
54  * à la fin du traitement : creation d'un singleton statique de l'objet
55  * tres specialise ATEXIT_.
56  *
57  * La création d'un objet de type ATEXIT_ entraîne l'inscription de la fonction
58  * Nettoyage() par atexit(). Il suffit donc de créer un singleton statique du type ATEXIT_
59  * pour effectuer cet enregistrement une seule fois indépendament de l'utilisateur.
60  */
61
62 class ATEXIT_
63 {
64 public :
65         /*!
66          * Allocation dynamique de Destructeurs, une liste chaînée de DESTRUCTEUR_GENERIQUE_* et enregistrement
67          * de la fonction Nettoyage() par atexit().
68          *
69          * La liste chaînée Destructeurs est détruite dans la fonction Nettoyage.
70          */
71         ATEXIT_( void )
72         {
73                 ASSERT (Destructeurs==0);
74                 if(MYDEBUG) MESSAGE("Construction ATEXIT"); // message necessaire pour utiliser logger dans Nettoyage (cf.BUG KERNEL4561)
75                 Destructeurs = new list<DESTRUCTEUR_GENERIQUE_*> ; // Destructeurs alloué dynamiquement (cf. ci-dessous) ,
76                                                                    // il est utilisé puis détruit par la fonction Nettoyage
77                 int cr = atexit( Nettoyage );                      // exécute Nettoyage lors de exit, après la destruction des données statiques !
78                 ASSERT(cr==0) ;
79         }
80
81         ~ATEXIT_( )
82         {
83                 if(MYDEBUG) MESSAGE("Destruction ATEXIT") ;
84         }
85 };
86
87
88
89
90 static ATEXIT_ nettoyage ;      /* singleton statique */
91
92
93 /*!
94  * traitement effectué :
95  * -# exécution de tous les objets de type DESTRUCTEUR_DE_ stockés dans la liste Destructeurs (ce qui détruit les
96  *    singletons correspondant) ;
97  * -# puis destruction de tous les objets de type DESTRUCTEUR_DE_ stockés dans la liste Destructeurs;
98  * -# destruction de la liste Destructeurs.
99  */
100
101 void Nettoyage( void )
102 {
103         if(MYDEBUG) BEGIN_OF("Nettoyage( void )") ;
104         ASSERT(Destructeurs) ;
105         if(MYDEBUG) SCRUTE( Destructeurs->size() ) ;
106         if( Destructeurs->size() )
107         {
108                 list<DESTRUCTEUR_GENERIQUE_*>::iterator it = Destructeurs->end() ;
109
110                 do
111                 {
112                         if(MYDEBUG) MESSAGE( "DESTRUCTION d'un SINGLETON");
113                         it-- ;
114                         DESTRUCTEUR_GENERIQUE_* ptr = *it ;
115                         //Destructeurs->remove( *it ) ;
116                         (*ptr)() ;
117                         delete ptr ;
118                 }while( it!=  Destructeurs->begin() ) ;
119
120                 Destructeurs->clear() ;
121                 if(MYDEBUG) SCRUTE( Destructeurs->size() ) ;
122                 ASSERT( Destructeurs->size()==0 ) ;
123                 ASSERT( Destructeurs->empty() ) ;
124         }
125
126         delete Destructeurs;
127         Destructeurs=0;
128         if(MYDEBUG) END_OF("Nettoyage( void )") ;
129         return ;
130 }
131
132
133 /*!
134  * Adds a destruction object to the list of actions to be performed at the end
135  * of the process
136  */
137 const int DESTRUCTEUR_GENERIQUE_::Ajout( DESTRUCTEUR_GENERIQUE_ &objet )
138 {
139         // N.B. : l'ordre de creation des SINGLETON etant important
140         //        on n'utilise pas deux fois la meme position pour
141         //        les stocker dans la pile des objets.
142         ASSERT(Destructeurs) ;
143         Destructeurs->push_back( &objet ) ;
144         return Destructeurs->size() ;
145 }