2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkObject.h,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.9 $
34 =========================================================================*/
40 *\brief Object : the top class of bbtk class hierarchy
44 #ifndef __bbtkObject_h__
45 #define __bbtkObject_h__
47 #include <bbtkSystem.h>
48 #include <boost/shared_ptr.hpp>
49 #include <boost/weak_ptr.hpp>
55 // The top class of bbtk class hierarchy
56 class BBTK_EXPORT Object
59 typedef boost::shared_ptr<Object> Pointer;
60 typedef boost::weak_ptr<Object> WeakPointer;
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(); }
72 static void InsertInObjectList(Pointer);
73 static void RemoveFromObjectList(WeakPointer);
75 static void InsertInPackageList(Pointer);
76 static void ReleasePackages();
78 static void PrintObjectListInfo(const std::string& name);
79 // static void PrintObjectInfo(const std::string& name);
80 static void PrintObjectInfo(const Pointer& o);
82 static long GetObjectsCount() { return mgObjectList.size(); }
84 /// Default objects deleter : removes object from list on deletion
85 struct BBTK_EXPORT Deleter
87 Deleter() : mPointer() {}
89 virtual void operator() (Object* p);
90 virtual int Delete(Object* p) { delete p; return 0; }
95 void LockThis() { mThisPointerLocked = mThisPointer.lock(); }
96 void UnLockThis() { mThisPointerLocked = Pointer(); }
97 // Object::Pointer GetThis() const { return mThisPointer.lock(); }
99 boost::shared_ptr<U> GetThisPointer() const
101 return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
104 static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
106 if (s->mThisPointer.lock())
108 //EED 2018-07-11 minwg64
109 boost::shared_ptr<U> p = s->template GetThisPointer<U>();
110 if (!lock) s->mThisPointerLocked.reset();
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)))
118 Object::InsertInObjectList(p);
119 if (lock) s->LockThis();
122 template <class U, class D>
123 static boost::shared_ptr<U> MakePointer(U* s,
127 if (s->mThisPointer.lock())
129 //EED 2018-07-11 minwg64
130 boost::shared_ptr<U> p = s->template GetThisPointer<U>();
131 if (!lock) s->mThisPointerLocked.reset();
134 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
136 (p._internal_get_deleter(typeid(D)))
139 Object::InsertInObjectList(p);
140 if (lock) s->LockThis();
145 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
146 static ObjectListType mgObjectList;
147 static ObjectListType mgPackageList;
148 WeakPointer mThisPointer;
149 Pointer mThisPointerLocked;
153 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
155 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
156 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
157 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
160 #define BBTK_OBJECT_MINIMAL_INTERFACE \
162 typedef boost::shared_ptr<Self> Pointer; \
163 typedef boost::weak_ptr<Self> WeakPointer; \
164 friend struct Object::Deleter;
167 // does not work : why ?
168 // boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt;
171 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
172 public : typedef CLASS Self; \
173 BBTK_OBJECT_MINIMAL_INTERFACE;
176 #define BBTK_OBJECT_INTERFACE(CLASS) \
177 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
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 ; \
186 CLASS(const CLASS&); \
189 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS) \
190 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
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 ;
198 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
199 public : typedef CLASS Self; \
200 BBTK_OBJECT_MINIMAL_INTERFACE; \
203 CLASS(const CLASS&); \
206 //=======================================================================
207 // A struct with one static instance
208 // just to print object list info after main
209 class BBTK_EXPORT StaticInitTime
216 static bool PrintObjectListInfo;
218 static bbtk::Object mObject;
223 template <class T, class U>
224 inline T BruteForceDownCastPointer(class U p)