]> Creatis software - bbtk.git/blob - kernel/src/bbtkObject.h
Compil sous Windows
[bbtk.git] / kernel / src / bbtkObject.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkObject.h,v $
5   Language:  C++
6   Date:      $Date: 2008/05/06 13:45:12 $
7   Version:   $Revision: 1.4 $
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 void operator() (Object* p); 
66       virtual void Delete(Object* p) { delete p; }
67       WeakPointer mPointer;
68     };
69
70   protected:
71     void LockThis() { mThisPointerLocked = mThisPointer.lock(); }       
72     void UnLockThis() { mThisPointerLocked = Pointer(); }
73     //    Object::Pointer GetThis() const { return mThisPointer.lock(); } 
74     template <class U>
75     boost::shared_ptr<U> GetThisPointer() const 
76     {
77       return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
78     }
79     template <class U>
80     static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
81     {                                                                   
82       if (s->mThisPointer.lock())                                       
83         {                                                               
84           boost::shared_ptr<U> p = s->GetThisPointer<U>();
85           if (!lock) s->mThisPointerLocked.reset();
86           return p;
87         }                                                               
88       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
89       static_cast<Object::Deleter*>                                     
90         (p._internal_get_deleter(typeid(Object::Deleter)))              
91         ->mPointer = p;                                                 
92       s->mThisPointer = p;                                              
93       Object::InsertInObjectList(p);                                    
94       if (lock) s->LockThis();                                          
95       return p;                                                 
96     }                                                                   
97     template <class U, class D>
98      static boost::shared_ptr<U> MakePointer(U* s, 
99                                              const D& del,
100                                              bool lock = false)
101     {                                                                   
102       if (s->mThisPointer.lock())                                       
103         {                                                               
104           boost::shared_ptr<U> p = s->GetThisPointer<U>();
105           if (!lock) s->mThisPointerLocked.reset();
106           return p;
107         }                                                               
108       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
109       static_cast<D*>                                   
110         (p._internal_get_deleter(typeid(D)))            
111         ->mPointer = p;                                                 
112       s->mThisPointer = p;                                              
113       Object::InsertInObjectList(p);                                    
114       if (lock) s->LockThis();                                          
115       return p;                                                 
116     }                                                                   
117      
118
119   private:
120     typedef std::set<boost::weak_ptr<Object> > ObjectListType;
121     static ObjectListType mgObjectList;
122     WeakPointer mThisPointer;                                           
123     Pointer mThisPointerLocked;                                         
124
125   }; 
126   
127 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self; 
128   
129 #define BBTK_FORWARD_DECLARE_POINTER(CLASS)                     \
130   typedef boost::shared_ptr<CLASS> CLASS ## Pointer;            \
131     typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
132
133
134 #define BBTK_OBJECT_MINIMAL_INTERFACE                                   \
135   public:                                                               \
136   typedef boost::shared_ptr<Self> Pointer;                              \
137     typedef boost::weak_ptr<Self> WeakPointer;                          \
138     friend struct Object::Deleter;                                      
139   //private:                                                            
140
141   // does not work : why ?
142   //      boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt; 
143   //
144   
145 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS)  \
146   public : typedef CLASS Self;                          \
147     BBTK_OBJECT_MINIMAL_INTERFACE;              
148
149  
150 #define BBTK_OBJECT_INTERFACE(CLASS)                                \
151   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
152 public:                                                             \
153   std::string GetObjectName() const;                                \
154     std::string GetObjectInfo() const ;                             \
155     size_t GetObjectSize() const ;                                  \
156     size_t GetObjectInternalSize() const ;                          \
157     size_t GetObjectRecursiveSize() const ;                         \
158 protected:                                                          \
159     CLASS();                                                        \
160     CLASS(const CLASS&);                                                    \
161     ~CLASS();                                       
162   
163 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS)                              \
164   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
165 public:                                                             \
166   std::string GetObjectName() const;                                \
167     std::string GetObjectInfo() const ;                             \
168     size_t GetObjectSize() const ;                                  \
169     size_t GetObjectInternalSize() const ;                          \
170     size_t GetObjectRecursiveSize() const ;                         
171
172 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS)                           \
173   public : typedef CLASS Self;                                          \
174     BBTK_OBJECT_MINIMAL_INTERFACE;                                      \
175 protected:                                                              \
176     CLASS();                                                            \
177     CLASS(const CLASS&);                                                        \
178     virtual ~CLASS();                                       
179   
180   //=======================================================================
181   // A struct with one static instance 
182   // just to print object list info after main
183   class BBTK_EXPORT StaticInitTime
184   {
185   public:
186     StaticInitTime();
187     ~StaticInitTime();
188
189
190     static bool PrintObjectListInfo;
191   private:
192           static bbtk::Object mObject;
193   };
194   
195   
196   /*
197   template <class T, class U>
198   inline T BruteForceDownCastPointer(class U p)
199   {
200     
201   }
202   */
203
204 }// namespace bbtk
205
206 #endif
207