1 /*=========================================================================
4 Module: $RCSfile: bbtkObject.h,v $
6 Date: $Date: 2008/05/06 13:45:12 $
7 Version: $Revision: 1.4 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See doc/license.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 *\brief Object : the top class of bbtk class hierarchy
24 #ifndef __bbtkObject_h__
25 #define __bbtkObject_h__
27 #include <bbtkSystem.h>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/weak_ptr.hpp>
35 // The top class of bbtk class hierarchy
36 class BBTK_EXPORT Object
39 typedef boost::shared_ptr<Object> Pointer;
40 typedef boost::weak_ptr<Object> WeakPointer;
45 virtual std::string GetObjectName() const;
46 virtual std::string GetObjectInfo() const;
47 virtual size_t GetObjectSize() const { return sizeof(Object); }
48 virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
49 virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
50 long GetUseCount() { return mThisPointer.use_count(); }
52 static void InsertInObjectList(Pointer);
53 static void RemoveFromObjectList(WeakPointer);
55 static void PrintObjectListInfo(const std::string& name);
56 // static void PrintObjectInfo(const std::string& name);
57 static void PrintObjectInfo(const Pointer& o);
59 static long GetObjectsCount() { return mgObjectList.size(); }
61 /// Default objects deleter : removes object from list on deletion
62 struct BBTK_EXPORT Deleter
64 Deleter() : mPointer() {}
65 virtual void operator() (Object* p);
66 virtual void Delete(Object* p) { delete p; }
71 void LockThis() { mThisPointerLocked = mThisPointer.lock(); }
72 void UnLockThis() { mThisPointerLocked = Pointer(); }
73 // Object::Pointer GetThis() const { return mThisPointer.lock(); }
75 boost::shared_ptr<U> GetThisPointer() const
77 return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
80 static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
82 if (s->mThisPointer.lock())
84 boost::shared_ptr<U> p = s->GetThisPointer<U>();
85 if (!lock) s->mThisPointerLocked.reset();
88 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
89 static_cast<Object::Deleter*>
90 (p._internal_get_deleter(typeid(Object::Deleter)))
93 Object::InsertInObjectList(p);
94 if (lock) s->LockThis();
97 template <class U, class D>
98 static boost::shared_ptr<U> MakePointer(U* s,
102 if (s->mThisPointer.lock())
104 boost::shared_ptr<U> p = s->GetThisPointer<U>();
105 if (!lock) s->mThisPointerLocked.reset();
108 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
110 (p._internal_get_deleter(typeid(D)))
113 Object::InsertInObjectList(p);
114 if (lock) s->LockThis();
120 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
121 static ObjectListType mgObjectList;
122 WeakPointer mThisPointer;
123 Pointer mThisPointerLocked;
127 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
129 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
130 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
131 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
134 #define BBTK_OBJECT_MINIMAL_INTERFACE \
136 typedef boost::shared_ptr<Self> Pointer; \
137 typedef boost::weak_ptr<Self> WeakPointer; \
138 friend struct Object::Deleter;
141 // does not work : why ?
142 // boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt;
145 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
146 public : typedef CLASS Self; \
147 BBTK_OBJECT_MINIMAL_INTERFACE;
150 #define BBTK_OBJECT_INTERFACE(CLASS) \
151 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
153 std::string GetObjectName() const; \
154 std::string GetObjectInfo() const ; \
155 size_t GetObjectSize() const ; \
156 size_t GetObjectInternalSize() const ; \
157 size_t GetObjectRecursiveSize() const ; \
160 CLASS(const CLASS&); \
163 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS) \
164 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
166 std::string GetObjectName() const; \
167 std::string GetObjectInfo() const ; \
168 size_t GetObjectSize() const ; \
169 size_t GetObjectInternalSize() const ; \
170 size_t GetObjectRecursiveSize() const ;
172 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
173 public : typedef CLASS Self; \
174 BBTK_OBJECT_MINIMAL_INTERFACE; \
177 CLASS(const CLASS&); \
180 //=======================================================================
181 // A struct with one static instance
182 // just to print object list info after main
183 class BBTK_EXPORT StaticInitTime
190 static bool PrintObjectListInfo;
192 static bbtk::Object mObject;
197 template <class T, class U>
198 inline T BruteForceDownCastPointer(class U p)