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