1 /*=========================================================================
4 Module: $RCSfile: bbtkObject.h,v $
6 Date: $Date: 2008/04/22 08:29:09 $
7 Version: $Revision: 1.2 $
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 <boost/shared_ptr.hpp>
28 #include <boost/weak_ptr.hpp>
34 // The top class of bbtk class hierarchy
38 typedef boost::shared_ptr<Object> Pointer;
39 typedef boost::weak_ptr<Object> WeakPointer;
44 virtual std::string GetObjectName() const;
45 virtual std::string GetObjectInfo() const;
46 virtual size_t GetObjectSize() const { return sizeof(Object); }
47 virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
48 virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
49 long GetUseCount() { return mThisPointer.use_count(); }
51 static void InsertInObjectList(Pointer);
52 static void RemoveFromObjectList(WeakPointer);
54 static void PrintObjectListInfo(const std::string& name);
55 // static void PrintObjectInfo(const std::string& name);
56 static void PrintObjectInfo(const Pointer& o);
57 /// Default objects deleter : removes object from list on deletion
60 Deleter() : mPointer() {}
61 virtual void operator() (Object* p);
62 virtual void Delete(Object* p) { delete p; }
67 void LockThis() { mThisPointerLocked = mThisPointer.lock(); }
68 void UnLockThis() { mThisPointerLocked = Pointer(); }
69 // Object::Pointer GetThis() const { return mThisPointer.lock(); }
71 boost::shared_ptr<U> GetThisPointer() const
73 return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
76 static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
78 if (s->mThisPointer.lock())
80 boost::shared_ptr<U> p = s->GetThisPointer<U>();
81 if (!lock) s->mThisPointerLocked.reset();
84 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
85 static_cast<Object::Deleter*>
86 (p._internal_get_deleter(typeid(Object::Deleter)))
89 Object::InsertInObjectList(p);
90 if (lock) s->LockThis();
93 template <class U, class D>
94 static boost::shared_ptr<U> MakePointer(U* s,
98 if (s->mThisPointer.lock())
100 boost::shared_ptr<U> p = s->GetThisPointer<U>();
101 if (!lock) s->mThisPointerLocked.reset();
104 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
106 (p._internal_get_deleter(typeid(D)))
109 Object::InsertInObjectList(p);
110 if (lock) s->LockThis();
116 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
117 static ObjectListType mgObjectList;
118 WeakPointer mThisPointer;
119 Pointer mThisPointerLocked;
123 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
125 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
126 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
127 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
130 #define BBTK_OBJECT_MINIMAL_INTERFACE \
132 typedef boost::shared_ptr<Self> Pointer; \
133 typedef boost::weak_ptr<Self> WeakPointer; \
134 friend struct Object::Deleter;
137 // does not work : why ?
138 // boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt;
141 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
142 public : typedef CLASS Self; \
143 BBTK_OBJECT_MINIMAL_INTERFACE;
146 #define BBTK_OBJECT_INTERFACE(CLASS) \
147 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
149 std::string GetObjectName() const; \
150 std::string GetObjectInfo() const ; \
151 size_t GetObjectSize() const ; \
152 size_t GetObjectInternalSize() const ; \
153 size_t GetObjectRecursiveSize() const ; \
156 CLASS(const CLASS&); \
159 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
160 public : typedef CLASS Self; \
161 BBTK_OBJECT_MINIMAL_INTERFACE; \
164 CLASS(const CLASS&); \
167 //=======================================================================
168 // A struct with one static instance
169 // just to print object list info after main
170 struct StaticInitTime
176 static bool PrintObjectListInfo;
178 static Object mObject;
183 template <class T, class U>
184 inline T BruteForceDownCastPointer(class U p)