]> Creatis software - bbtk.git/blob - kernel/src/bbtkObject.cxx
Feature #1774
[bbtk.git] / kernel / src / bbtkObject.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkObject.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.12 $
34 =========================================================================*/
35
36
37
38
39 #include "bbtkObject.h"
40 #include "bbtkMessageManager.h"
41 #include "bbtkPackage.h"
42
43 namespace bbtk
44
45   
46
47   //=======================================================================
48   Object::ObjectListType Object::mgObjectList;
49   Object::ObjectListType Object::mgPackageList;
50   //=======================================================================
51
52   //=======================================================================
53   Object::Object()
54   { 
55   }
56   //=======================================================================
57
58
59   //=======================================================================
60   Object::~Object()
61   { 
62   }
63   //=======================================================================
64
65   //=======================================================================
66   void Object::InsertInObjectList(Pointer p)
67   { 
68     bbtkDebugMessage("object",9,"##> Object::InsertInObjectList(\""
69                      <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
70     boost::weak_ptr<Object> w(p);
71     mgObjectList.insert(w); 
72   }
73   //=======================================================================
74
75   //=======================================================================
76   void Object::InsertInPackageList(Pointer p)
77   { 
78     bbtkDebugMessage("object",9,"##> Object::InsertInPackageList(\""
79                      <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
80     boost::weak_ptr<Object> w(p);
81     mgPackageList.insert(w); 
82   }
83   //=======================================================================
84
85
86   //=======================================================================
87   void Object::RemoveFromObjectList(WeakPointer p)
88   { 
89     bbtkDebugMessage("object",9,"##> Object::RemoveFromObjectList()"
90                      <<std::endl);
91     mgObjectList.erase(p);
92
93   }
94   //=======================================================================
95
96
97   //=======================================================================
98   std::string Object::GetObjectName() const
99   {
100     return std::string("**Unknown object**");
101   }
102   //=======================================================================
103
104   //=======================================================================
105   std::string Object::GetObjectInfo() const
106   {
107     return std::string("");
108   }
109   //=======================================================================
110   
111   //=======================================================================
112   void Object::PrintObjectListInfo(const std::string& name)
113   {
114     
115     std::cout 
116       << "=============== Living bbtk::Object pointers ========="<<std::endl;
117
118     long n = 0;
119     long u = 0;
120     size_t m = 0;
121     ObjectListType::iterator i;
122     for (i = mgObjectList.begin();
123          i!=mgObjectList.end();
124          ++i)
125       {
126         if (i->use_count() == 0) 
127           {
128             u++;
129           }
130         else 
131           { 
132             Object::Pointer p(i->lock());
133             if (p->GetObjectName().find(name) != std::string::npos ) 
134               {
135                 std::cout << n << "/" << mgObjectList.size() << " ";
136                 PrintObjectInfo(p);
137                 m += p->GetObjectSize();
138                 n++;
139               }
140           }
141       }
142     std::cout 
143       << "------------------------------------------------------"<<std::endl; 
144
145     std::cout << " Total : "<<n<<" objects - "<<m<<" b"<<std::endl;
146     if (u==1)
147       {
148         std::cout<<"* Note : "<<u
149                  <<" object in list has 0 ref count, "
150                  <<"i.e. are no more accessible by bbtk but did not properly destroyed because another ref counting system is holding them !"<<std::endl;
151       }
152     else if (u>1)
153       {
154         std::cout<<"* Note : "<<u
155                  <<" objects in list have 0 ref count, "
156                  <<"i.e. are no more accessible by bbtk but did not properly destroyed because another ref counting system is holding them !"<<std::endl;
157       }
158     std::cout
159       << "============ EO Living bbtk::Object pointers ========="<<std::endl;
160         
161   }
162   //=======================================================================
163
164   /*
165   //=======================================================================
166   void Object::PrintObjectInfo(const std::string& name)
167   {
168     long n = 0;
169     ObjectListType::iterator i;
170     for (i = mgObjectList.begin();
171          i!=mgObjectList.end();
172          ++i)
173       {
174         n++;
175         if (i->use_count() == 0) continue;
176         Object::Pointer p(i->lock());   
177         if (p->GetObjectName().find(name) != std::string::npos ) 
178           {
179             std::cout << n << "/" << mgObjectList.size() << " ";
180             PrintObjectInfo(p);
181           }
182       }
183   }
184   //=======================================================================
185   */
186   //=======================================================================
187   void Object::PrintObjectInfo(const Object::Pointer& p)
188   {
189     std::cout << "* [" << p << "] \t" 
190               << p.use_count()-1 << " r \t"
191               << p->GetObjectRecursiveSize() << " ("
192               << p->GetObjectSize() << ") b \t"
193               << p->GetObjectName() 
194               << std::endl;
195     std::cout << p->GetObjectInfo();
196   }
197   //=======================================================================
198
199   //=======================================================================
200   /// Objects deleter
201   void Object::Deleter::operator() (Object* p)
202   {
203     std::string name = p->GetObjectName();
204 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
205     void* adr = (void*)p;
206     bbtkDebugMessage("object",1,"##> Object::Deleter : deleting \""
207                      <<name<<"\" ["<<adr<<"]"<<std::endl);
208 #endif
209     int remaining = this->Delete(p);
210     if (remaining == 0)
211       {
212         Object::RemoveFromObjectList(mPointer);
213         bbtkDebugMessage("object",2,"<## Object::Deleter : \""<<name<<"\" ["
214                          <<adr<<"] deleted"<<std::endl);
215       }
216     else
217       {
218         bbtkWarning("##### Object::Deleter \""<<name<<"\" failed !!! "
219                     << remaining << " reference(s) still around..."
220                     <<std::endl); 
221       }
222   }
223   //=======================================================================
224
225
226
227
228   //=======================================================================
229   void Object::ReleasePackages()
230   {
231     bbtkDebugMessage("object",1,"##> Object::ReleasePackages()"<<std::endl);
232     // Release package pointers
233     ObjectListType::iterator i;
234     for (i = mgPackageList.begin();
235          i!= mgPackageList.end();
236          ++i)
237       {
238         if (i->use_count() != 0) 
239           { 
240             bbtkDebugMessage("object",1,"##> Releasing package '"<<
241                              i->lock()->GetThisPointer<Package>()->GetName()
242                              <<"'"<<std::endl);
243             //      Object::Pointer p(i->lock());
244             Package::WeakPointer w(i->lock()->GetThisPointer<Package>());
245             Package::Release(w);
246             /*
247             if (p->GetObjectName().find(name) != std::string::npos ) 
248               {
249                 std::cout << n << "/" << mgObjectList.size() << " ";
250                 PrintObjectInfo(p);
251                 m += p->GetObjectSize();
252                 n++;
253               }
254             */
255           }
256       }  
257
258   }
259   //=======================================================================
260
261
262
263
264   //=======================================================================
265   bool StaticInitTime::PrintObjectListInfo = false;
266   //=======================================================================
267   //=======================================================================
268   StaticInitTime::StaticInitTime() 
269   {
270   }
271   //=======================================================================
272   //=======================================================================
273   StaticInitTime::~StaticInitTime()
274   {
275     Object::ReleasePackages();
276
277     if (PrintObjectListInfo) 
278       {
279         std::cout << std::endl
280                   << "************************** main ended *******************"
281                   << std::endl << std::endl;
282         Object::PrintObjectListInfo("");
283       }
284     if (Object::GetObjectsCount()>0)
285       {
286         std::cout << std::endl;
287         std::cout << "**************************** WARNING *************************"
288                   <<std::endl; 
289         std::cout << "**** "<< Object::GetObjectsCount()
290                   <<" bbtk objects still alive after main ended"<<std::endl;
291         if (!PrintObjectListInfo) 
292           {
293             
294             std::cout << "**** "
295                       <<"Rerun bbi with -D option or put 'debug -D' in bbs file"
296                   <<std::endl;
297             std::cout << "**** "
298                       <<"to view the objects list "
299                       <<std::endl;
300           }
301         std::cout << "**************************************************************"
302                   <<std::endl
303                   <<std::endl;
304
305      }
306   }
307   //=======================================================================
308   //=======================================================================
309   //  The static instance
310   static StaticInitTime i;
311   //=======================================================================
312
313 }