1 // SALOME Utils : general SALOME's definitions and tools
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : Utils_DESTRUCTEUR_GENERIQUE.cxx
25 // Author : Antoine YESSAYAN, EDF
37 # include "Utils_DESTRUCTEUR_GENERIQUE.hxx"
38 # include "utilities.h"
44 static int MYDEBUG = 0;
46 static int MYDEBUG = 0;
49 static list<DESTRUCTEUR_GENERIQUE_*> *Destructeurs=0 ;
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_.
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.
63 static bool ATEXIT_Done = false ;
70 * Allocation dynamique de Destructeurs, une liste chaînée de DESTRUCTEUR_GENERIQUE_* et enregistrement
71 * de la fonction Nettoyage() par atexit().
73 * La liste chaînée Destructeurs est détruite dans la fonction Nettoyage.
75 //CCRT ATEXIT_( void )
76 ATEXIT_( bool Make_ATEXIT )
79 if ( Make_ATEXIT && !ATEXIT_Done ) {
81 ASSERT (Destructeurs==0);
82 if(MYDEBUG) MESSAGE("Construction ATEXIT"); // message necessaire pour utiliser logger dans Nettoyage (cf.BUG KERNEL4561)
83 Destructeurs = new list<DESTRUCTEUR_GENERIQUE_*> ; // Destructeurs alloué dynamiquement (cf. ci-dessous) ,
84 // il est utilisé puis détruit par la fonction Nettoyage
85 int cr = atexit( Nettoyage ); // exécute Nettoyage lors de exit, après la destruction des données statiques !
93 if(MYDEBUG) MESSAGE("Destruction ATEXIT") ;
100 static ATEXIT_ nettoyage = ATEXIT_( false ); /* singleton statique */
104 * traitement effectué :
105 * -# exécution de tous les objets de type DESTRUCTEUR_DE_ stockés dans la liste Destructeurs (ce qui détruit les
106 * singletons correspondant) ;
107 * -# puis destruction de tous les objets de type DESTRUCTEUR_DE_ stockés dans la liste Destructeurs;
108 * -# destruction de la liste Destructeurs.
111 void Nettoyage( void )
113 if(MYDEBUG) BEGIN_OF("Nettoyage( void )") ;
114 ASSERT(Destructeurs) ;
115 if(MYDEBUG) SCRUTE( Destructeurs->size() ) ;
116 if( Destructeurs->size() )
118 list<DESTRUCTEUR_GENERIQUE_*>::iterator it = Destructeurs->end() ;
122 if(MYDEBUG) MESSAGE( "DESTRUCTION d'un SINGLETON");
124 DESTRUCTEUR_GENERIQUE_* ptr = *it ;
125 //Destructeurs->remove( *it ) ;
128 }while( it!= Destructeurs->begin() ) ;
130 Destructeurs->clear() ;
131 if(MYDEBUG) SCRUTE( Destructeurs->size() ) ;
132 ASSERT( Destructeurs->size()==0 ) ;
133 ASSERT( Destructeurs->empty() ) ;
138 if(MYDEBUG) END_OF("Nettoyage( void )") ;
144 * Adds a destruction object to the list of actions to be performed at the end
147 const int DESTRUCTEUR_GENERIQUE_::Ajout( DESTRUCTEUR_GENERIQUE_ &objet )
149 // N.B. : l'ordre de creation des SINGLETON etant important
150 // on n'utilise pas deux fois la meme position pour
151 // les stocker dans la pile des objets.
154 if ( !ATEXIT_Done ) {
155 nettoyage = ATEXIT_( true ) ;
158 ASSERT(Destructeurs) ;
159 Destructeurs->push_back( &objet ) ;
160 return Destructeurs->size() ;