1 /*=========================================================================
3 Module: $RCSfile: bbtkObject.cxx,v $
5 Date: $Date: 2009/04/30 14:31:31 $
6 Version: $Revision: 1.11 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
32 #include "bbtkObject.h"
33 #include "bbtkMessageManager.h"
34 #include "bbtkPackage.h"
40 //=======================================================================
41 Object::ObjectListType Object::mgObjectList;
42 Object::ObjectListType Object::mgPackageList;
43 //=======================================================================
45 //=======================================================================
49 //=======================================================================
52 //=======================================================================
56 //=======================================================================
58 //=======================================================================
59 void Object::InsertInObjectList(Pointer p)
61 bbtkDebugMessage("object",9,"##> Object::InsertInObjectList(\""
62 <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
63 boost::weak_ptr<Object> w(p);
64 mgObjectList.insert(w);
66 //=======================================================================
68 //=======================================================================
69 void Object::InsertInPackageList(Pointer p)
71 bbtkDebugMessage("object",9,"##> Object::InsertInPackageList(\""
72 <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
73 boost::weak_ptr<Object> w(p);
74 mgPackageList.insert(w);
76 //=======================================================================
79 //=======================================================================
80 void Object::RemoveFromObjectList(WeakPointer p)
82 bbtkDebugMessage("object",9,"##> Object::RemoveFromObjectList()"
84 mgObjectList.erase(p);
87 //=======================================================================
90 //=======================================================================
91 std::string Object::GetObjectName() const
93 return std::string("**Unknown object**");
95 //=======================================================================
97 //=======================================================================
98 std::string Object::GetObjectInfo() const
100 return std::string("");
102 //=======================================================================
104 //=======================================================================
105 void Object::PrintObjectListInfo(const std::string& name)
109 << "=============== Living bbtk::Object pointers ========="<<std::endl;
114 ObjectListType::iterator i;
115 for (i = mgObjectList.begin();
116 i!=mgObjectList.end();
119 if (i->use_count() == 0)
125 Object::Pointer p(i->lock());
126 if (p->GetObjectName().find(name) != std::string::npos )
128 std::cout << n << "/" << mgObjectList.size() << " ";
130 m += p->GetObjectSize();
136 << "------------------------------------------------------"<<std::endl;
138 std::cout << " Total : "<<n<<" objects - "<<m<<" b"<<std::endl;
141 std::cout<<"* Note : "<<u
142 <<" object in list has 0 ref count, "
143 <<"i.e. are no more accessible by bbtk but did not properly destroyed because another ref counting system is holding them !"<<std::endl;
147 std::cout<<"* Note : "<<u
148 <<" objects in list have 0 ref count, "
149 <<"i.e. are no more accessible by bbtk but did not properly destroyed because another ref counting system is holding them !"<<std::endl;
152 << "============ EO Living bbtk::Object pointers ========="<<std::endl;
155 //=======================================================================
158 //=======================================================================
159 void Object::PrintObjectInfo(const std::string& name)
162 ObjectListType::iterator i;
163 for (i = mgObjectList.begin();
164 i!=mgObjectList.end();
168 if (i->use_count() == 0) continue;
169 Object::Pointer p(i->lock());
170 if (p->GetObjectName().find(name) != std::string::npos )
172 std::cout << n << "/" << mgObjectList.size() << " ";
177 //=======================================================================
179 //=======================================================================
180 void Object::PrintObjectInfo(const Object::Pointer& p)
182 std::cout << "* [" << p << "] \t"
183 << p.use_count()-1 << " r \t"
184 << p->GetObjectRecursiveSize() << " ("
185 << p->GetObjectSize() << ") b \t"
186 << p->GetObjectName()
188 std::cout << p->GetObjectInfo();
190 //=======================================================================
192 //=======================================================================
194 void Object::Deleter::operator() (Object* p)
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);
202 int remaining = this->Delete(p);
205 Object::RemoveFromObjectList(mPointer);
206 bbtkDebugMessage("object",2,"<## Object::Deleter : \""<<name<<"\" ["
207 <<adr<<"] deleted"<<std::endl);
211 bbtkWarning("##### Object::Deleter \""<<name<<"\" failed !!! "
212 << remaining << " reference(s) still around..."
216 //=======================================================================
221 //=======================================================================
222 void Object::ReleasePackages()
224 bbtkDebugMessage("object",1,"##> Object::ReleasePackages()"<<std::endl);
225 // Release package pointers
226 ObjectListType::iterator i;
227 for (i = mgPackageList.begin();
228 i!= mgPackageList.end();
231 if (i->use_count() != 0)
233 bbtkDebugMessage("object",1,"##> Releasing package '"<<
234 i->lock()->GetThisPointer<Package>()->GetName()
236 // Object::Pointer p(i->lock());
237 Package::WeakPointer w(i->lock()->GetThisPointer<Package>());
240 if (p->GetObjectName().find(name) != std::string::npos )
242 std::cout << n << "/" << mgObjectList.size() << " ";
244 m += p->GetObjectSize();
252 //=======================================================================
257 //=======================================================================
258 bool StaticInitTime::PrintObjectListInfo = false;
259 //=======================================================================
260 //=======================================================================
261 StaticInitTime::StaticInitTime()
264 //=======================================================================
265 //=======================================================================
266 StaticInitTime::~StaticInitTime()
268 Object::ReleasePackages();
270 if (PrintObjectListInfo)
272 std::cout << std::endl
273 << "************************** main ended *******************"
274 << std::endl << std::endl;
275 Object::PrintObjectListInfo("");
277 if (Object::GetObjectsCount()>0)
279 std::cout << std::endl;
280 std::cout << "**************************** WARNING *************************"
282 std::cout << "**** "<< Object::GetObjectsCount()
283 <<" bbtk objects still alive after main ended"<<std::endl;
284 if (!PrintObjectListInfo)
288 <<"Rerun bbi with -D option or put 'debug -D' in bbs file"
291 <<"to view the objects list "
294 std::cout << "**************************************************************"
300 //=======================================================================
301 //=======================================================================
302 // The static instance
303 static StaticInitTime i;
304 //=======================================================================