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