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