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