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