]> Creatis software - bbtk.git/blob - kernel/src/bbtkObject.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkObject.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkObject.h,v $
4   Language:  C++
5   Date:      $Date: 2008/10/17 08:18:13 $
6   Version:   $Revision: 1.7 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *\file
33  *\brief  Object : the top class of bbtk class hierarchy 
34  * 
35  */
36
37 #ifndef __bbtkObject_h__
38 #define __bbtkObject_h__
39
40 #include <bbtkSystem.h>
41 #include <boost/shared_ptr.hpp>
42 #include <boost/weak_ptr.hpp>
43 #include <set>
44
45 namespace bbtk
46 {
47
48   // The top class of bbtk class hierarchy 
49   class BBTK_EXPORT Object 
50   {
51   public:
52     typedef boost::shared_ptr<Object> Pointer;
53     typedef boost::weak_ptr<Object> WeakPointer;
54
55     Object();
56     virtual ~Object(); 
57     
58     virtual std::string GetObjectName() const;
59     virtual std::string GetObjectInfo() const;
60     virtual size_t GetObjectSize() const { return sizeof(Object); }
61     virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
62     virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
63     long GetUseCount() { return mThisPointer.use_count(); }
64     
65     static void InsertInObjectList(Pointer);
66     static void RemoveFromObjectList(WeakPointer);
67
68     static void InsertInPackageList(Pointer);
69     static void ReleasePackages();
70
71     static void PrintObjectListInfo(const std::string& name);
72     //    static void PrintObjectInfo(const std::string& name);
73     static void PrintObjectInfo(const Pointer& o); 
74
75     static long GetObjectsCount() { return mgObjectList.size(); }
76
77     /// Default objects deleter : removes object from list on deletion
78     struct BBTK_EXPORT Deleter 
79     { 
80       Deleter() : mPointer() {}
81       virtual ~Deleter() {}
82       virtual void operator() (Object* p); 
83       virtual void Delete(Object* p) { delete p; }
84       WeakPointer mPointer;
85     };
86
87   protected:
88     void LockThis() { mThisPointerLocked = mThisPointer.lock(); }       
89     void UnLockThis() { mThisPointerLocked = Pointer(); }
90     //    Object::Pointer GetThis() const { return mThisPointer.lock(); } 
91     template <class U>
92     boost::shared_ptr<U> GetThisPointer() const 
93     {
94       return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
95     }
96     template <class U>
97     static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
98     {                                                                   
99       if (s->mThisPointer.lock())                                       
100         {                                                               
101           boost::shared_ptr<U> p = s->GetThisPointer<U>();
102           if (!lock) s->mThisPointerLocked.reset();
103           return p;
104         }                                                               
105       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
106       static_cast<Object::Deleter*>                                     
107         (p._internal_get_deleter(typeid(Object::Deleter)))              
108         ->mPointer = p;                                                 
109       s->mThisPointer = p;                                              
110       Object::InsertInObjectList(p);                                    
111       if (lock) s->LockThis();                                          
112       return p;                                                 
113     }                                                                   
114     template <class U, class D>
115      static boost::shared_ptr<U> MakePointer(U* s, 
116                                              const D& del,
117                                              bool lock = false)
118     {                                                                   
119       if (s->mThisPointer.lock())                                       
120         {                                                               
121           boost::shared_ptr<U> p = s->GetThisPointer<U>();
122           if (!lock) s->mThisPointerLocked.reset();
123           return p;
124         }                                                               
125       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
126       static_cast<D*>                                   
127         (p._internal_get_deleter(typeid(D)))            
128         ->mPointer = p;                                                 
129       s->mThisPointer = p;                                              
130       Object::InsertInObjectList(p);                                    
131       if (lock) s->LockThis();                                          
132       return p;                                                 
133     }                                                                   
134      
135
136   private:
137     typedef std::set<boost::weak_ptr<Object> > ObjectListType;
138     static ObjectListType mgObjectList;
139     static ObjectListType mgPackageList;
140     WeakPointer mThisPointer;                                           
141     Pointer mThisPointerLocked;                                         
142
143   }; 
144   
145 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self; 
146   
147 #define BBTK_FORWARD_DECLARE_POINTER(CLASS)                     \
148   typedef boost::shared_ptr<CLASS> CLASS ## Pointer;            \
149     typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
150
151
152 #define BBTK_OBJECT_MINIMAL_INTERFACE                                   \
153   public:                                                               \
154   typedef boost::shared_ptr<Self> Pointer;                              \
155     typedef boost::weak_ptr<Self> WeakPointer;                          \
156     friend struct Object::Deleter;                                      
157   //private:                                                            
158
159   // does not work : why ?
160   //      boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt; 
161   //
162   
163 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS)  \
164   public : typedef CLASS Self;                          \
165     BBTK_OBJECT_MINIMAL_INTERFACE;              
166
167  
168 #define BBTK_OBJECT_INTERFACE(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 protected:                                                          \
177     CLASS();                                                        \
178     CLASS(const CLASS&);                                                    \
179     ~CLASS();                                       
180   
181 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS)                              \
182   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
183 public:                                                             \
184   std::string GetObjectName() const;                                \
185     std::string GetObjectInfo() const ;                             \
186     size_t GetObjectSize() const ;                                  \
187     size_t GetObjectInternalSize() const ;                          \
188     size_t GetObjectRecursiveSize() const ;                         
189
190 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS)                           \
191   public : typedef CLASS Self;                                          \
192     BBTK_OBJECT_MINIMAL_INTERFACE;                                      \
193 protected:                                                              \
194     CLASS();                                                            \
195     CLASS(const CLASS&);                                                        \
196     virtual ~CLASS();                                       
197   
198   //=======================================================================
199   // A struct with one static instance 
200   // just to print object list info after main
201   class BBTK_EXPORT StaticInitTime
202   {
203   public:
204     StaticInitTime();
205     ~StaticInitTime();
206
207
208     static bool PrintObjectListInfo;
209   private:
210     static bbtk::Object mObject;
211   };
212   
213   
214   /*
215   template <class T, class U>
216   inline T BruteForceDownCastPointer(class U p)
217   {
218     
219   }
220   */
221
222 }// namespace bbtk
223
224 #endif
225