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: 2008/10/17 08:18:13 $
00006   Version:   $Revision: 1.10 $
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. destroyed without removing itself from the living objects list ! (this is just an implementation error not a memory leak)"<<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. destroyed without removing themselves from the living objects list ! (this is just an implementation error not a memory leak)"<<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     Object::RemoveFromObjectList(mPointer);
00203     this->Delete(p);
00204     bbtkDebugMessage("object",2,"<## Object::Deleter : \""<<name<<"\" ["
00205                      <<adr<<"] deleted"<<std::endl);
00206   }
00207   //=======================================================================
00208 
00209 
00210 
00211 
00212   //=======================================================================
00213   void Object::ReleasePackages()
00214   {
00215     bbtkDebugMessage("object",1,"##> Object::ReleasePackages()"<<std::endl);
00216     // Release package pointers
00217     ObjectListType::iterator i;
00218     for (i = mgPackageList.begin();
00219          i!= mgPackageList.end();
00220          ++i)
00221       {
00222         if (i->use_count() != 0) 
00223           { 
00224             bbtkDebugMessage("object",1,"##> Releasing package '"<<
00225                              i->lock()->GetThisPointer<Package>()->GetName()
00226                              <<"'"<<std::endl);
00227             //      Object::Pointer p(i->lock());
00228             Package::WeakPointer w(i->lock()->GetThisPointer<Package>());
00229             Package::Release(w);
00230             /*
00231             if (p->GetObjectName().find(name) != std::string::npos ) 
00232               {
00233                 std::cout << n << "/" << mgObjectList.size() << " ";
00234                 PrintObjectInfo(p);
00235                 m += p->GetObjectSize();
00236                 n++;
00237               }
00238             */
00239           }
00240       }  
00241 
00242   }
00243   //=======================================================================
00244 
00245 
00246 
00247 
00248   //=======================================================================
00249   bool StaticInitTime::PrintObjectListInfo = false;
00250   //=======================================================================
00251   //=======================================================================
00252   StaticInitTime::StaticInitTime() 
00253   {
00254   }
00255   //=======================================================================
00256   //=======================================================================
00257   StaticInitTime::~StaticInitTime()
00258   {
00259     Object::ReleasePackages();
00260 
00261     if (PrintObjectListInfo) 
00262       {
00263         std::cout << std::endl
00264                   << "************************** main ended *******************"
00265                   << std::endl << std::endl;
00266         Object::PrintObjectListInfo("");
00267       }
00268     if (Object::GetObjectsCount()>0)
00269       {
00270         std::cout << std::endl;
00271         std::cout << "**************************** WARNING *************************"
00272                   <<std::endl; 
00273         std::cout << "**** "<< Object::GetObjectsCount()
00274                   <<" bbtk objects still alive after main ended"<<std::endl;
00275         if (!PrintObjectListInfo) 
00276           {
00277             
00278             std::cout << "**** "
00279                       <<"Rerun bbi with -D option or put 'debug -D' in bbs file"
00280                   <<std::endl;
00281             std::cout << "**** "
00282                       <<"to view the objects list "
00283                       <<std::endl;
00284           }
00285         std::cout << "**************************************************************"
00286                   <<std::endl
00287                   <<std::endl;
00288 
00289      }
00290   }
00291   //=======================================================================
00292   //=======================================================================
00293   //  The static instance
00294   static StaticInitTime i;
00295   //=======================================================================
00296 
00297 }

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6