bbtkObject.h

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkObject.h,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:13 $
00006   Version:   $Revision: 1.7 $
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 
00037 #ifndef __bbtkObject_h__
00038 #define __bbtkObject_h__
00039 
00040 #include <bbtkSystem.h>
00041 #include <boost/shared_ptr.hpp>
00042 #include <boost/weak_ptr.hpp>
00043 #include <set>
00044 
00045 namespace bbtk
00046 {
00047 
00048   // The top class of bbtk class hierarchy 
00049   class BBTK_EXPORT Object 
00050   {
00051   public:
00052     typedef boost::shared_ptr<Object> Pointer;
00053     typedef boost::weak_ptr<Object> WeakPointer;
00054 
00055     Object();
00056     virtual ~Object(); 
00057     
00058     virtual std::string GetObjectName() const;
00059     virtual std::string GetObjectInfo() const;
00060     virtual size_t GetObjectSize() const { return sizeof(Object); }
00061     virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
00062     virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
00063     long GetUseCount() { return mThisPointer.use_count(); }
00064     
00065     static void InsertInObjectList(Pointer);
00066     static void RemoveFromObjectList(WeakPointer);
00067 
00068     static void InsertInPackageList(Pointer);
00069     static void ReleasePackages();
00070 
00071     static void PrintObjectListInfo(const std::string& name);
00072     //    static void PrintObjectInfo(const std::string& name);
00073     static void PrintObjectInfo(const Pointer& o); 
00074 
00075     static long GetObjectsCount() { return mgObjectList.size(); }
00076 
00078     struct BBTK_EXPORT Deleter 
00079     { 
00080       Deleter() : mPointer() {}
00081       virtual ~Deleter() {}
00082       virtual void operator() (Object* p); 
00083       virtual void Delete(Object* p) { delete p; }
00084       WeakPointer mPointer;
00085     };
00086 
00087   protected:
00088     void LockThis() { mThisPointerLocked = mThisPointer.lock(); }       
00089     void UnLockThis() { mThisPointerLocked = Pointer(); }
00090     //    Object::Pointer GetThis() const { return mThisPointer.lock(); } 
00091     template <class U>
00092     boost::shared_ptr<U> GetThisPointer() const 
00093     {
00094       return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
00095     }
00096     template <class U>
00097     static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
00098     {                                                                   
00099       if (s->mThisPointer.lock())                                       
00100         {                                                               
00101           boost::shared_ptr<U> p = s->GetThisPointer<U>();
00102           if (!lock) s->mThisPointerLocked.reset();
00103           return p;
00104         }                                                               
00105       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
00106       static_cast<Object::Deleter*>                                     
00107         (p._internal_get_deleter(typeid(Object::Deleter)))              
00108         ->mPointer = p;                                                 
00109       s->mThisPointer = p;                                              
00110       Object::InsertInObjectList(p);                                    
00111       if (lock) s->LockThis();                                          
00112       return p;                                                 
00113     }                                                                   
00114     template <class U, class D>
00115      static boost::shared_ptr<U> MakePointer(U* s, 
00116                                              const D& del,
00117                                              bool lock = false)
00118     {                                                                   
00119       if (s->mThisPointer.lock())                                       
00120         {                                                               
00121           boost::shared_ptr<U> p = s->GetThisPointer<U>();
00122           if (!lock) s->mThisPointerLocked.reset();
00123           return p;
00124         }                                                               
00125       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
00126       static_cast<D*>                                   
00127         (p._internal_get_deleter(typeid(D)))            
00128         ->mPointer = p;                                                 
00129       s->mThisPointer = p;                                              
00130       Object::InsertInObjectList(p);                                    
00131       if (lock) s->LockThis();                                          
00132       return p;                                                 
00133     }                                                                   
00134      
00135 
00136   private:
00137     typedef std::set<boost::weak_ptr<Object> > ObjectListType;
00138     static ObjectListType mgObjectList;
00139     static ObjectListType mgPackageList;
00140     WeakPointer mThisPointer;                                           
00141     Pointer mThisPointerLocked;                                         
00142 
00143   }; 
00144   
00145 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self; 
00146   
00147 #define BBTK_FORWARD_DECLARE_POINTER(CLASS)                     \
00148   typedef boost::shared_ptr<CLASS> CLASS ## Pointer;            \
00149     typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
00150 
00151 
00152 #define BBTK_OBJECT_MINIMAL_INTERFACE                                   \
00153   public:                                                               \
00154   typedef boost::shared_ptr<Self> Pointer;                              \
00155     typedef boost::weak_ptr<Self> WeakPointer;                          \
00156     friend struct Object::Deleter;                                      
00157   //private:                                                            
00158 
00159   // does not work : why ?
00160   //      boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt; 
00161   //
00162   
00163 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS)  \
00164   public : typedef CLASS Self;                          \
00165     BBTK_OBJECT_MINIMAL_INTERFACE;              
00166 
00167  
00168 #define BBTK_OBJECT_INTERFACE(CLASS)                                \
00169   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
00170 public:                                                             \
00171   std::string GetObjectName() const;                                \
00172     std::string GetObjectInfo() const ;                             \
00173     size_t GetObjectSize() const ;                                  \
00174     size_t GetObjectInternalSize() const ;                          \
00175     size_t GetObjectRecursiveSize() const ;                         \
00176 protected:                                                          \
00177     CLASS();                                                        \
00178     CLASS(const CLASS&);                                                    \
00179     ~CLASS();                                       
00180   
00181 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS)                              \
00182   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
00183 public:                                                             \
00184   std::string GetObjectName() const;                                \
00185     std::string GetObjectInfo() const ;                             \
00186     size_t GetObjectSize() const ;                                  \
00187     size_t GetObjectInternalSize() const ;                          \
00188     size_t GetObjectRecursiveSize() const ;                         
00189 
00190 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS)                           \
00191   public : typedef CLASS Self;                                          \
00192     BBTK_OBJECT_MINIMAL_INTERFACE;                                      \
00193 protected:                                                              \
00194     CLASS();                                                            \
00195     CLASS(const CLASS&);                                                        \
00196     virtual ~CLASS();                                       
00197   
00198   //=======================================================================
00199   // A struct with one static instance 
00200   // just to print object list info after main
00201   class BBTK_EXPORT StaticInitTime
00202   {
00203   public:
00204     StaticInitTime();
00205     ~StaticInitTime();
00206 
00207 
00208     static bool PrintObjectListInfo;
00209   private:
00210     static bbtk::Object mObject;
00211   };
00212   
00213   
00214   /*
00215   template <class T, class U>
00216   inline T BruteForceDownCastPointer(class U p)
00217   {
00218     
00219   }
00220   */
00221 
00222 }// namespace bbtk
00223 
00224 #endif
00225 

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