2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkObject.cxx,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.12 $
34 =========================================================================*/
39 #include "bbtkObject.h"
40 #include "bbtkMessageManager.h"
41 #include "bbtkPackage.h"
47 //=======================================================================
48 Object::ObjectListType Object::mgObjectList;
49 Object::ObjectListType Object::mgPackageList;
50 //=======================================================================
52 //=======================================================================
56 //=======================================================================
59 //=======================================================================
63 //=======================================================================
65 //=======================================================================
66 void Object::InsertInObjectList(Pointer p)
68 bbtkDebugMessage("object",9,"##> Object::InsertInObjectList(\""
69 <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
70 boost::weak_ptr<Object> w(p);
71 mgObjectList.insert(w);
73 //=======================================================================
75 //=======================================================================
76 void Object::InsertInPackageList(Pointer p)
78 bbtkDebugMessage("object",9,"##> Object::InsertInPackageList(\""
79 <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
80 boost::weak_ptr<Object> w(p);
81 mgPackageList.insert(w);
83 //=======================================================================
86 //=======================================================================
87 void Object::RemoveFromObjectList(WeakPointer p)
89 bbtkDebugMessage("object",9,"##> Object::RemoveFromObjectList()"
91 mgObjectList.erase(p);
94 //=======================================================================
97 //=======================================================================
98 std::string Object::GetObjectName() const
100 return std::string("**Unknown object**");
102 //=======================================================================
104 //=======================================================================
105 std::string Object::GetObjectInfo() const
107 return std::string("");
109 //=======================================================================
111 //=======================================================================
112 void Object::PrintObjectListInfo(const std::string& name)
116 << "=============== Living bbtk::Object pointers ========="<<std::endl;
121 ObjectListType::iterator i;
122 for (i = mgObjectList.begin();
123 i!=mgObjectList.end();
126 if (i->use_count() == 0)
132 Object::Pointer p(i->lock());
133 if (p->GetObjectName().find(name) != std::string::npos )
135 std::cout << n << "/" << mgObjectList.size() << " ";
137 m += p->GetObjectSize();
143 << "------------------------------------------------------"<<std::endl;
145 std::cout << " Total : "<<n<<" objects - "<<m<<" b"<<std::endl;
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;
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;
159 << "============ EO Living bbtk::Object pointers ========="<<std::endl;
162 //=======================================================================
165 //=======================================================================
166 void Object::PrintObjectInfo(const std::string& name)
169 ObjectListType::iterator i;
170 for (i = mgObjectList.begin();
171 i!=mgObjectList.end();
175 if (i->use_count() == 0) continue;
176 Object::Pointer p(i->lock());
177 if (p->GetObjectName().find(name) != std::string::npos )
179 std::cout << n << "/" << mgObjectList.size() << " ";
184 //=======================================================================
186 //=======================================================================
187 void Object::PrintObjectInfo(const Object::Pointer& p)
189 std::cout << "* [" << p << "] \t"
190 << p.use_count()-1 << " r \t"
191 << p->GetObjectRecursiveSize() << " ("
192 << p->GetObjectSize() << ") b \t"
193 << p->GetObjectName()
195 std::cout << p->GetObjectInfo();
197 //=======================================================================
199 //=======================================================================
201 void Object::Deleter::operator() (Object* p)
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);
209 int remaining = this->Delete(p);
212 Object::RemoveFromObjectList(mPointer);
213 bbtkDebugMessage("object",2,"<## Object::Deleter : \""<<name<<"\" ["
214 <<adr<<"] deleted"<<std::endl);
218 bbtkWarning("##### Object::Deleter \""<<name<<"\" failed !!! "
219 << remaining << " reference(s) still around..."
223 //=======================================================================
228 //=======================================================================
229 void Object::ReleasePackages()
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();
238 if (i->use_count() != 0)
240 bbtkDebugMessage("object",1,"##> Releasing package '"<<
241 i->lock()->GetThisPointer<Package>()->GetName()
243 // Object::Pointer p(i->lock());
244 Package::WeakPointer w(i->lock()->GetThisPointer<Package>());
247 if (p->GetObjectName().find(name) != std::string::npos )
249 std::cout << n << "/" << mgObjectList.size() << " ";
251 m += p->GetObjectSize();
259 //=======================================================================
264 //=======================================================================
265 bool StaticInitTime::PrintObjectListInfo = false;
266 //=======================================================================
267 //=======================================================================
268 StaticInitTime::StaticInitTime()
271 //=======================================================================
272 //=======================================================================
273 StaticInitTime::~StaticInitTime()
275 Object::ReleasePackages();
277 if (PrintObjectListInfo)
279 std::cout << std::endl
280 << "************************** main ended *******************"
281 << std::endl << std::endl;
282 Object::PrintObjectListInfo("");
284 if (Object::GetObjectsCount()>0)
286 std::cout << std::endl;
287 std::cout << "**************************** WARNING *************************"
289 std::cout << "**** "<< Object::GetObjectsCount()
290 <<" bbtk objects still alive after main ended"<<std::endl;
291 if (!PrintObjectListInfo)
295 <<"Rerun bbi with -D option or put 'debug -D' in bbs file"
298 <<"to view the objects list "
301 std::cout << "**************************************************************"
307 //=======================================================================
308 //=======================================================================
309 // The static instance
310 static StaticInitTime i;
311 //=======================================================================