bbtkObject.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkObject.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2009/04/30 14:31:31 $
00006   Version:   $Revision: 1.11 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00031 
00032 #include "bbtkObject.h"
00033 #include "bbtkMessageManager.h"
00034 #include "bbtkPackage.h"
00035 
00036 namespace bbtk
00037 { 
00038   
00039 
00040   //=======================================================================
00041   Object::ObjectListType Object::mgObjectList;
00042   Object::ObjectListType Object::mgPackageList;
00043   //=======================================================================
00044 
00045   //=======================================================================
00046   Object::Object()
00047   { 
00048   }
00049   //=======================================================================
00050 
00051 
00052   //=======================================================================
00053   Object::~Object()
00054   { 
00055   }
00056   //=======================================================================
00057 
00058   //=======================================================================
00059   void Object::InsertInObjectList(Pointer p)
00060   { 
00061     bbtkDebugMessage("object",9,"##> Object::InsertInObjectList(\""
00062                      <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
00063     boost::weak_ptr<Object> w(p);
00064     mgObjectList.insert(w); 
00065   }
00066   //=======================================================================
00067 
00068   //=======================================================================
00069   void Object::InsertInPackageList(Pointer p)
00070   { 
00071     bbtkDebugMessage("object",9,"##> Object::InsertInPackageList(\""
00072                      <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
00073     boost::weak_ptr<Object> w(p);
00074     mgPackageList.insert(w); 
00075   }
00076   //=======================================================================
00077 
00078 
00079   //=======================================================================
00080   void Object::RemoveFromObjectList(WeakPointer p)
00081   { 
00082     bbtkDebugMessage("object",9,"##> Object::RemoveFromObjectList()"
00083                      <<std::endl);
00084     mgObjectList.erase(p);
00085 
00086   }
00087   //=======================================================================
00088 
00089 
00090   //=======================================================================
00091   std::string Object::GetObjectName() const
00092   {
00093     return std::string("**Unknown object**");
00094   }
00095   //=======================================================================
00096 
00097   //=======================================================================
00098   std::string Object::GetObjectInfo() const
00099   {
00100     return std::string("");
00101   }
00102   //=======================================================================
00103   
00104   //=======================================================================
00105   void Object::PrintObjectListInfo(const std::string& name)
00106   {
00107     
00108     std::cout 
00109       << "=============== Living bbtk::Object pointers ========="<<std::endl;
00110 
00111     long n = 0;
00112     long u = 0;
00113     size_t m = 0;
00114     ObjectListType::iterator i;
00115     for (i = mgObjectList.begin();
00116          i!=mgObjectList.end();
00117          ++i)
00118       {
00119         if (i->use_count() == 0) 
00120           {
00121             u++;
00122           }
00123         else 
00124           { 
00125             Object::Pointer p(i->lock());
00126             if (p->GetObjectName().find(name) != std::string::npos ) 
00127               {
00128                 std::cout << n << "/" << mgObjectList.size() << " ";
00129                 PrintObjectInfo(p);
00130                 m += p->GetObjectSize();
00131                 n++;
00132               }
00133           }
00134       }
00135     std::cout 
00136       << "------------------------------------------------------"<<std::endl; 
00137 
00138     std::cout << " Total : "<<n<<" objects - "<<m<<" b"<<std::endl;
00139     if (u==1)
00140       {
00141         std::cout<<"* Note : "<<u
00142                  <<" object in list has 0 ref count, "
00143                  <<"i.e. are no more accessible by bbtk but did not properly destroyed because another ref counting system is holding them !"<<std::endl;
00144       }
00145     else if (u>1)
00146       {
00147         std::cout<<"* Note : "<<u
00148                  <<" objects in list have 0 ref count, "
00149                  <<"i.e. are no more accessible by bbtk but did not properly destroyed because another ref counting system is holding them !"<<std::endl;
00150       }
00151     std::cout
00152       << "============ EO Living bbtk::Object pointers ========="<<std::endl;
00153         
00154   }
00155   //=======================================================================
00156 
00157   /*
00158   //=======================================================================
00159   void Object::PrintObjectInfo(const std::string& name)
00160   {
00161     long n = 0;
00162     ObjectListType::iterator i;
00163     for (i = mgObjectList.begin();
00164          i!=mgObjectList.end();
00165          ++i)
00166       {
00167         n++;
00168         if (i->use_count() == 0) continue;
00169         Object::Pointer p(i->lock());   
00170         if (p->GetObjectName().find(name) != std::string::npos ) 
00171           {
00172             std::cout << n << "/" << mgObjectList.size() << " ";
00173             PrintObjectInfo(p);
00174           }
00175       }
00176   }
00177   //=======================================================================
00178   */
00179   //=======================================================================
00180   void Object::PrintObjectInfo(const Object::Pointer& p)
00181   {
00182     std::cout << "* [" << p << "] \t" 
00183               << p.use_count()-1 << " r \t"
00184               << p->GetObjectRecursiveSize() << " ("
00185               << p->GetObjectSize() << ") b \t"
00186               << p->GetObjectName() 
00187               << std::endl;
00188     std::cout << p->GetObjectInfo();
00189   }
00190   //=======================================================================
00191 
00192   //=======================================================================
00194   void Object::Deleter::operator() (Object* p)
00195   {
00196     std::string name = p->GetObjectName();
00197 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
00198     void* adr = (void*)p;
00199     bbtkDebugMessage("object",1,"##> Object::Deleter : deleting \""
00200                      <<name<<"\" ["<<adr<<"]"<<std::endl);
00201 #endif
00202     int remaining = this->Delete(p);
00203     if (remaining == 0)
00204       {
00205         Object::RemoveFromObjectList(mPointer);
00206         bbtkDebugMessage("object",2,"<## Object::Deleter : \""<<name<<"\" ["
00207                          <<adr<<"] deleted"<<std::endl);
00208       }
00209     else
00210       {
00211         bbtkWarning("##### Object::Deleter \""<<name<<"\" failed !!! "
00212                     << remaining << " reference(s) still around..."
00213                     <<std::endl); 
00214       }
00215   }
00216   //=======================================================================
00217 
00218 
00219 
00220 
00221   //=======================================================================
00222   void Object::ReleasePackages()
00223   {
00224     bbtkDebugMessage("object",1,"##> Object::ReleasePackages()"<<std::endl);
00225     // Release package pointers
00226     ObjectListType::iterator i;
00227     for (i = mgPackageList.begin();
00228          i!= mgPackageList.end();
00229          ++i)
00230       {
00231         if (i->use_count() != 0) 
00232           { 
00233             bbtkDebugMessage("object",1,"##> Releasing package '"<<
00234                              i->lock()->GetThisPointer<Package>()->GetName()
00235                              <<"'"<<std::endl);
00236             //      Object::Pointer p(i->lock());
00237             Package::WeakPointer w(i->lock()->GetThisPointer<Package>());
00238             Package::Release(w);
00239             /*
00240             if (p->GetObjectName().find(name) != std::string::npos ) 
00241               {
00242                 std::cout << n << "/" << mgObjectList.size() << " ";
00243                 PrintObjectInfo(p);
00244                 m += p->GetObjectSize();
00245                 n++;
00246               }
00247             */
00248           }
00249       }  
00250 
00251   }
00252   //=======================================================================
00253 
00254 
00255 
00256 
00257   //=======================================================================
00258   bool StaticInitTime::PrintObjectListInfo = false;
00259   //=======================================================================
00260   //=======================================================================
00261   StaticInitTime::StaticInitTime() 
00262   {
00263   }
00264   //=======================================================================
00265   //=======================================================================
00266   StaticInitTime::~StaticInitTime()
00267   {
00268     Object::ReleasePackages();
00269 
00270     if (PrintObjectListInfo) 
00271       {
00272         std::cout << std::endl
00273                   << "************************** main ended *******************"
00274                   << std::endl << std::endl;
00275         Object::PrintObjectListInfo("");
00276       }
00277     if (Object::GetObjectsCount()>0)
00278       {
00279         std::cout << std::endl;
00280         std::cout << "**************************** WARNING *************************"
00281                   <<std::endl; 
00282         std::cout << "**** "<< Object::GetObjectsCount()
00283                   <<" bbtk objects still alive after main ended"<<std::endl;
00284         if (!PrintObjectListInfo) 
00285           {
00286             
00287             std::cout << "**** "
00288                       <<"Rerun bbi with -D option or put 'debug -D' in bbs file"
00289                   <<std::endl;
00290             std::cout << "**** "
00291                       <<"to view the objects list "
00292                       <<std::endl;
00293           }
00294         std::cout << "**************************************************************"
00295                   <<std::endl
00296                   <<std::endl;
00297 
00298      }
00299   }
00300   //=======================================================================
00301   //=======================================================================
00302   //  The static instance
00303   static StaticInitTime i;
00304   //=======================================================================
00305 
00306 }

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1