]> 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/07/23 11:46:11 $
7   Version:   $Revision: 1.5 $
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 <bbtkSystem.h>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/weak_ptr.hpp>
30 #include <set>
31
32 namespace bbtk
33 {
34
35   // The top class of bbtk class hierarchy 
36   class BBTK_EXPORT Object 
37   {
38   public:
39     typedef boost::shared_ptr<Object> Pointer;
40     typedef boost::weak_ptr<Object> WeakPointer;
41
42     Object();
43     virtual ~Object(); 
44     
45     virtual std::string GetObjectName() const;
46     virtual std::string GetObjectInfo() const;
47     virtual size_t GetObjectSize() const { return sizeof(Object); }
48     virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
49     virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
50     long GetUseCount() { return mThisPointer.use_count(); }
51     
52     static void InsertInObjectList(Pointer);
53     static void RemoveFromObjectList(WeakPointer);
54
55     static void PrintObjectListInfo(const std::string& name);
56     //    static void PrintObjectInfo(const std::string& name);
57     static void PrintObjectInfo(const Pointer& o); 
58
59     static long GetObjectsCount() { return mgObjectList.size(); }
60
61     /// Default objects deleter : removes object from list on deletion
62     struct BBTK_EXPORT Deleter 
63     { 
64       Deleter() : mPointer() {}
65       virtual ~Deleter() {}
66       virtual void operator() (Object* p); 
67       virtual void Delete(Object* p) { delete p; }
68       WeakPointer mPointer;
69     };
70
71   protected:
72     void LockThis() { mThisPointerLocked = mThisPointer.lock(); }       
73     void UnLockThis() { mThisPointerLocked = Pointer(); }
74     //    Object::Pointer GetThis() const { return mThisPointer.lock(); } 
75     template <class U>
76     boost::shared_ptr<U> GetThisPointer() const 
77     {
78       return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
79     }
80     template <class U>
81     static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
82     {                                                                   
83       if (s->mThisPointer.lock())                                       
84         {                                                               
85           boost::shared_ptr<U> p = s->GetThisPointer<U>();
86           if (!lock) s->mThisPointerLocked.reset();
87           return p;
88         }                                                               
89       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
90       static_cast<Object::Deleter*>                                     
91         (p._internal_get_deleter(typeid(Object::Deleter)))              
92         ->mPointer = p;                                                 
93       s->mThisPointer = p;                                              
94       Object::InsertInObjectList(p);                                    
95       if (lock) s->LockThis();                                          
96       return p;                                                 
97     }                                                                   
98     template <class U, class D>
99      static boost::shared_ptr<U> MakePointer(U* s, 
100                                              const D& del,
101                                              bool lock = false)
102     {                                                                   
103       if (s->mThisPointer.lock())                                       
104         {                                                               
105           boost::shared_ptr<U> p = s->GetThisPointer<U>();
106           if (!lock) s->mThisPointerLocked.reset();
107           return p;
108         }                                                               
109       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
110       static_cast<D*>                                   
111         (p._internal_get_deleter(typeid(D)))            
112         ->mPointer = p;                                                 
113       s->mThisPointer = p;                                              
114       Object::InsertInObjectList(p);                                    
115       if (lock) s->LockThis();                                          
116       return p;                                                 
117     }                                                                   
118      
119
120   private:
121     typedef std::set<boost::weak_ptr<Object> > ObjectListType;
122     static ObjectListType mgObjectList;
123     WeakPointer mThisPointer;                                           
124     Pointer mThisPointerLocked;                                         
125
126   }; 
127   
128 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self; 
129   
130 #define BBTK_FORWARD_DECLARE_POINTER(CLASS)                     \
131   typedef boost::shared_ptr<CLASS> CLASS ## Pointer;            \
132     typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
133
134
135 #define BBTK_OBJECT_MINIMAL_INTERFACE                                   \
136   public:                                                               \
137   typedef boost::shared_ptr<Self> Pointer;                              \
138     typedef boost::weak_ptr<Self> WeakPointer;                          \
139     friend struct Object::Deleter;                                      
140   //private:                                                            
141
142   // does not work : why ?
143   //      boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt; 
144   //
145   
146 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS)  \
147   public : typedef CLASS Self;                          \
148     BBTK_OBJECT_MINIMAL_INTERFACE;              
149
150  
151 #define BBTK_OBJECT_INTERFACE(CLASS)                                \
152   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
153 public:                                                             \
154   std::string GetObjectName() const;                                \
155     std::string GetObjectInfo() const ;                             \
156     size_t GetObjectSize() const ;                                  \
157     size_t GetObjectInternalSize() const ;                          \
158     size_t GetObjectRecursiveSize() const ;                         \
159 protected:                                                          \
160     CLASS();                                                        \
161     CLASS(const CLASS&);                                                    \
162     ~CLASS();                                       
163   
164 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS)                              \
165   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
166 public:                                                             \
167   std::string GetObjectName() const;                                \
168     std::string GetObjectInfo() const ;                             \
169     size_t GetObjectSize() const ;                                  \
170     size_t GetObjectInternalSize() const ;                          \
171     size_t GetObjectRecursiveSize() const ;                         
172
173 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS)                           \
174   public : typedef CLASS Self;                                          \
175     BBTK_OBJECT_MINIMAL_INTERFACE;                                      \
176 protected:                                                              \
177     CLASS();                                                            \
178     CLASS(const CLASS&);                                                        \
179     virtual ~CLASS();                                       
180   
181   //=======================================================================
182   // A struct with one static instance 
183   // just to print object list info after main
184   class BBTK_EXPORT StaticInitTime
185   {
186   public:
187     StaticInitTime();
188     ~StaticInitTime();
189
190
191     static bool PrintObjectListInfo;
192   private:
193           static bbtk::Object mObject;
194   };
195   
196   
197   /*
198   template <class T, class U>
199   inline T BruteForceDownCastPointer(class U p)
200   {
201     
202   }
203   */
204
205 }// namespace bbtk
206
207 #endif
208