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