]> Creatis software - bbtk.git/blob - kernel/src/bbtkObject.h
#3203
[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 //EED 2018-07-11 minwg64 
109           boost::shared_ptr<U> p = s->template GetThisPointer<U>();
110           if (!lock) s->mThisPointerLocked.reset();
111           return p;
112         }                                                               
113       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
114       static_cast<Object::Deleter*>                                     
115         (p._internal_get_deleter(typeid(Object::Deleter)))              
116         ->mPointer = p;                                                 
117       s->mThisPointer = p;                                              
118       Object::InsertInObjectList(p);                                    
119       if (lock) s->LockThis();                                          
120       return p;                                                 
121     }                                                                   
122     template <class U, class D>
123      static boost::shared_ptr<U> MakePointer(U* s, 
124                                              const D& del,
125                                              bool lock = false)
126     {                                                                   
127       if (s->mThisPointer.lock())                                       
128         {                       
129 //EED 2018-07-11 minwg64 
130           boost::shared_ptr<U> p = s->template GetThisPointer<U>();
131           if (!lock) s->mThisPointerLocked.reset();
132           return p;
133         }                                                               
134       boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
135       static_cast<D*>                                   
136         (p._internal_get_deleter(typeid(D)))            
137         ->mPointer = p;                                                 
138       s->mThisPointer = p;                                              
139       Object::InsertInObjectList(p);                                    
140       if (lock) s->LockThis();                                          
141       return p;                                                 
142     }                                                                   
143      
144   private:
145     typedef std::set<boost::weak_ptr<Object> > ObjectListType;
146     static ObjectListType mgObjectList;
147     static ObjectListType mgPackageList;
148     WeakPointer mThisPointer;                                           
149     Pointer mThisPointerLocked;                                         
150
151   }; 
152   
153 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self; 
154   
155 #define BBTK_FORWARD_DECLARE_POINTER(CLASS)                     \
156   typedef boost::shared_ptr<CLASS> CLASS ## Pointer;            \
157     typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
158
159
160 #define BBTK_OBJECT_MINIMAL_INTERFACE                                   \
161   public:                                                               \
162   typedef boost::shared_ptr<Self> Pointer;                              \
163     typedef boost::weak_ptr<Self> WeakPointer;                          \
164     friend struct Object::Deleter;                                      
165   //private:                                                            
166
167   // does not work : why ?
168   //      boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt; 
169   //
170   
171 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS)  \
172   public : typedef CLASS Self;                          \
173     BBTK_OBJECT_MINIMAL_INTERFACE;              
174
175  
176 #define BBTK_OBJECT_INTERFACE(CLASS)                                \
177   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
178 public:                                                             \
179   std::string GetObjectName() const;                                \
180     std::string GetObjectInfo() const ;                             \
181     size_t GetObjectSize() const ;                                  \
182     size_t GetObjectInternalSize() const ;                          \
183     size_t GetObjectRecursiveSize() const ;                         \
184 protected:                                                          \
185     CLASS();                                                        \
186     CLASS(const CLASS&);                                                    \
187     ~CLASS();                                       
188   
189 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS)                              \
190   BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS);                   \
191 public:                                                             \
192   std::string GetObjectName() const;                                \
193     std::string GetObjectInfo() const ;                             \
194     size_t GetObjectSize() const ;                                  \
195     size_t GetObjectInternalSize() const ;                          \
196     size_t GetObjectRecursiveSize() const ;                         
197
198 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS)                           \
199   public : typedef CLASS Self;                                          \
200     BBTK_OBJECT_MINIMAL_INTERFACE;                                      \
201 protected:                                                              \
202     CLASS();                                                            \
203     CLASS(const CLASS&);                                                        \
204     virtual ~CLASS();                                       
205   
206   //=======================================================================
207   // A struct with one static instance 
208   // just to print object list info after main
209   class BBTK_EXPORT StaticInitTime
210   {
211   public:
212     StaticInitTime();
213     ~StaticInitTime();
214
215
216     static bool PrintObjectListInfo;
217   private:
218     static bbtk::Object mObject;
219   };
220   
221   
222   /*
223   template <class T, class U>
224   inline T BruteForceDownCastPointer(class U p)
225   {
226     
227   }
228   */
229
230 }// namespace bbtk
231
232 #endif
233