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 boost::shared_ptr<U> p = s->GetThisPointer<U>();
109 if (!lock) s->mThisPointerLocked.reset();
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)))
117 Object::InsertInObjectList(p);
118 if (lock) s->LockThis();
121 template <class U, class D>
122 static boost::shared_ptr<U> MakePointer(U* s,
126 if (s->mThisPointer.lock())
128 boost::shared_ptr<U> p = s->GetThisPointer<U>();
129 if (!lock) s->mThisPointerLocked.reset();
132 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
134 (p._internal_get_deleter(typeid(D)))
137 Object::InsertInObjectList(p);
138 if (lock) s->LockThis();
143 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
144 static ObjectListType mgObjectList;
145 static ObjectListType mgPackageList;
146 WeakPointer mThisPointer;
147 Pointer mThisPointerLocked;
151 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
153 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
154 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
155 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
158 #define BBTK_OBJECT_MINIMAL_INTERFACE \
160 typedef boost::shared_ptr<Self> Pointer; \
161 typedef boost::weak_ptr<Self> WeakPointer; \
162 friend struct Object::Deleter;
165 // does not work : why ?
166 // boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt;
169 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
170 public : typedef CLASS Self; \
171 BBTK_OBJECT_MINIMAL_INTERFACE;
174 #define BBTK_OBJECT_INTERFACE(CLASS) \
175 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
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 ; \
184 CLASS(const CLASS&); \
187 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS) \
188 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
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 ;
196 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
197 public : typedef CLASS Self; \
198 BBTK_OBJECT_MINIMAL_INTERFACE; \
201 CLASS(const CLASS&); \
204 //=======================================================================
205 // A struct with one static instance
206 // just to print object list info after main
207 class BBTK_EXPORT StaticInitTime
214 static bool PrintObjectListInfo;
216 static bbtk::Object mObject;
221 template <class T, class U>
222 inline T BruteForceDownCastPointer(class U p)