]> Creatis software - bbtk.git/blob - kernel/src/bbtkObject.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkObject.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkObject.h,v $
5   Language:  C++
6   Date:      $Date: 2008/04/22 08:29:09 $
7   Version:   $Revision: 1.2 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See doc/license.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  *\file
20  *\brief  Object : the top class of bbtk class hierarchy 
21  * 
22  */
23
24 #ifndef __bbtkObject_h__
25 #define __bbtkObject_h__
26
27 #include <boost/shared_ptr.hpp>
28 #include <boost/weak_ptr.hpp>
29 #include <set>
30
31 namespace bbtk
32 {
33
34   // The top class of bbtk class hierarchy 
35   class Object 
36   {
37   public:
38     typedef boost::shared_ptr<Object> Pointer;
39     typedef boost::weak_ptr<Object> WeakPointer;
40
41     Object();
42     virtual ~Object(); 
43     
44     virtual std::string GetObjectName() const;
45     virtual std::string GetObjectInfo() const;
46     virtual size_t GetObjectSize() const { return sizeof(Object); }
47     virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
48     virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
49     long GetUseCount() { return mThisPointer.use_count(); }
50     
51     static void InsertInObjectList(Pointer);
52     static void RemoveFromObjectList(WeakPointer);
53
54     static void PrintObjectListInfo(const std::string& name);
55     //    static void PrintObjectInfo(const std::string& name);
56     static void PrintObjectInfo(const Pointer& o); 
57     /// Default objects deleter : removes object from list on deletion
58     struct Deleter 
59     { 
60       Deleter() : mPointer() {}
61       virtual void operator() (Object* p); 
62       virtual void Delete(Object* p) { delete p; }
63       WeakPointer mPointer;
64     };
65
66   protected:
67     void LockThis() { mThisPointerLocked = mThisPointer.lock(); }       
68     void UnLockThis() { mThisPointerLocked = Pointer(); }
69     //    Object::Pointer GetThis() const { return mThisPointer.lock(); } 
70     template <class U>
71     boost::shared_ptr<U> GetThisPointer() const 
72     {
73       return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
74     }
75     template <class U>
76     static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
77     {                                                                   
78       if (s->mThisPointer.lock())                                       
79         {                                                               
80           boost::shared_ptr<U> p = s->GetThisPointer<U>();
81           if (!lock) s->mThisPointerLocked.reset();
82           return p;
83         }                                                               
84       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
85       static_cast<Object::Deleter*>                                     
86         (p._internal_get_deleter(typeid(Object::Deleter)))              
87         ->mPointer = p;                                                 
88       s->mThisPointer = p;                                              
89       Object::InsertInObjectList(p);                                    
90       if (lock) s->LockThis();                                          
91       return p;                                                 
92     }                                                                   
93     template <class U, class D>
94      static boost::shared_ptr<U> MakePointer(U* s, 
95                                              const D& del,
96                                              bool lock = false)
97     {                                                                   
98       if (s->mThisPointer.lock())                                       
99         {                                                               
100           boost::shared_ptr<U> p = s->GetThisPointer<U>();
101           if (!lock) s->mThisPointerLocked.reset();
102           return p;
103         }                                                               
104       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
105       static_cast<D*>                                   
106         (p._internal_get_deleter(typeid(D)))            
107         ->mPointer = p;                                                 
108       s->mThisPointer = p;                                              
109       Object::InsertInObjectList(p);                                    
110       if (lock) s->LockThis();                                          
111       return p;                                                 
112     }                                                                   
113      
114
115   private:
116     typedef std::set<boost::weak_ptr<Object> > ObjectListType;
117     static ObjectListType mgObjectList;
118     WeakPointer mThisPointer;                                           
119     Pointer mThisPointerLocked;                                         
120
121   }; 
122   
123 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self; 
124   
125 #define BBTK_FORWARD_DECLARE_POINTER(CLASS)                     \
126   typedef boost::shared_ptr<CLASS> CLASS ## Pointer;            \
127     typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
128
129
130 #define BBTK_OBJECT_MINIMAL_INTERFACE                                   \
131   public:                                                               \
132   typedef boost::shared_ptr<Self> Pointer;                              \
133     typedef boost::weak_ptr<Self> WeakPointer;                          \
134     friend struct Object::Deleter;                                      
135   //private:                                                            
136
137   // does not work : why ?
138   //      boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt; 
139   //
140   
141 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS)  \
142   public : typedef CLASS Self;                          \
143     BBTK_OBJECT_MINIMAL_INTERFACE;              
144
145  
146 #define BBTK_OBJECT_INTERFACE(CLASS)                                \
147   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
148 public:                                                             \
149   std::string GetObjectName() const;                                \
150     std::string GetObjectInfo() const ;                             \
151     size_t GetObjectSize() const ;                                  \
152     size_t GetObjectInternalSize() const ;                          \
153     size_t GetObjectRecursiveSize() const ;                         \
154 protected:                                                          \
155     CLASS();                                                        \
156     CLASS(const CLASS&);                                                    \
157     ~CLASS();                                       
158   
159 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS)                           \
160   public : typedef CLASS Self;                                          \
161     BBTK_OBJECT_MINIMAL_INTERFACE;                                      \
162 protected:                                                              \
163     CLASS();                                                            \
164     CLASS(const CLASS&);                                                        \
165     virtual ~CLASS();                                       
166   
167   //=======================================================================
168   // A struct with one static instance 
169   // just to print object list info after main
170   struct StaticInitTime
171   {
172     StaticInitTime();
173     ~StaticInitTime();
174
175
176     static bool PrintObjectListInfo;
177   private:
178     static Object mObject;
179   };
180   
181   
182   /*
183   template <class T, class U>
184   inline T BruteForceDownCastPointer(class U p)
185   {
186     
187   }
188   */
189
190 }// namespace bbtk
191
192 #endif
193