]> 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: 2009/04/30 14:31:31 $
6   Version:   $Revision: 1.8 $
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 int Delete(Object* p) { delete p; return 0; }
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   private:
136     typedef std::set<boost::weak_ptr<Object> > ObjectListType;
137     static ObjectListType mgObjectList;
138     static ObjectListType mgPackageList;
139     WeakPointer mThisPointer;                                           
140     Pointer mThisPointerLocked;                                         
141
142   }; 
143   
144 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self; 
145   
146 #define BBTK_FORWARD_DECLARE_POINTER(CLASS)                     \
147   typedef boost::shared_ptr<CLASS> CLASS ## Pointer;            \
148     typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
149
150
151 #define BBTK_OBJECT_MINIMAL_INTERFACE                                   \
152   public:                                                               \
153   typedef boost::shared_ptr<Self> Pointer;                              \
154     typedef boost::weak_ptr<Self> WeakPointer;                          \
155     friend struct Object::Deleter;                                      
156   //private:                                                            
157
158   // does not work : why ?
159   //      boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt; 
160   //
161   
162 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS)  \
163   public : typedef CLASS Self;                          \
164     BBTK_OBJECT_MINIMAL_INTERFACE;              
165
166  
167 #define BBTK_OBJECT_INTERFACE(CLASS)                                \
168   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
169 public:                                                             \
170   std::string GetObjectName() const;                                \
171     std::string GetObjectInfo() const ;                             \
172     size_t GetObjectSize() const ;                                  \
173     size_t GetObjectInternalSize() const ;                          \
174     size_t GetObjectRecursiveSize() const ;                         \
175 protected:                                                          \
176     CLASS();                                                        \
177     CLASS(const CLASS&);                                                    \
178     ~CLASS();                                       
179   
180 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS)                              \
181   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
182 public:                                                             \
183   std::string GetObjectName() const;                                \
184     std::string GetObjectInfo() const ;                             \
185     size_t GetObjectSize() const ;                                  \
186     size_t GetObjectInternalSize() const ;                          \
187     size_t GetObjectRecursiveSize() const ;                         
188
189 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS)                           \
190   public : typedef CLASS Self;                                          \
191     BBTK_OBJECT_MINIMAL_INTERFACE;                                      \
192 protected:                                                              \
193     CLASS();                                                            \
194     CLASS(const CLASS&);                                                        \
195     virtual ~CLASS();                                       
196   
197   //=======================================================================
198   // A struct with one static instance 
199   // just to print object list info after main
200   class BBTK_EXPORT StaticInitTime
201   {
202   public:
203     StaticInitTime();
204     ~StaticInitTime();
205
206
207     static bool PrintObjectListInfo;
208   private:
209     static bbtk::Object mObject;
210   };
211   
212   
213   /*
214   template <class T, class U>
215   inline T BruteForceDownCastPointer(class U p)
216   {
217     
218   }
219   */
220
221 }// namespace bbtk
222
223 #endif
224