1 /*=========================================================================
4 Module: $RCSfile: bbtkObject.h,v $
6 Date: $Date: 2008/04/24 11:49:59 $
7 Version: $Revision: 1.3 $
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);
58 static long GetObjectsCount() { return mgObjectList.size(); }
60 /// Default objects deleter : removes object from list on deletion
63 Deleter() : mPointer() {}
64 virtual void operator() (Object* p);
65 virtual void Delete(Object* p) { delete p; }
70 void LockThis() { mThisPointerLocked = mThisPointer.lock(); }
71 void UnLockThis() { mThisPointerLocked = Pointer(); }
72 // Object::Pointer GetThis() const { return mThisPointer.lock(); }
74 boost::shared_ptr<U> GetThisPointer() const
76 return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
79 static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
81 if (s->mThisPointer.lock())
83 boost::shared_ptr<U> p = s->GetThisPointer<U>();
84 if (!lock) s->mThisPointerLocked.reset();
87 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
88 static_cast<Object::Deleter*>
89 (p._internal_get_deleter(typeid(Object::Deleter)))
92 Object::InsertInObjectList(p);
93 if (lock) s->LockThis();
96 template <class U, class D>
97 static boost::shared_ptr<U> MakePointer(U* s,
101 if (s->mThisPointer.lock())
103 boost::shared_ptr<U> p = s->GetThisPointer<U>();
104 if (!lock) s->mThisPointerLocked.reset();
107 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
109 (p._internal_get_deleter(typeid(D)))
112 Object::InsertInObjectList(p);
113 if (lock) s->LockThis();
119 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
120 static ObjectListType mgObjectList;
121 WeakPointer mThisPointer;
122 Pointer mThisPointerLocked;
126 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
128 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
129 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
130 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
133 #define BBTK_OBJECT_MINIMAL_INTERFACE \
135 typedef boost::shared_ptr<Self> Pointer; \
136 typedef boost::weak_ptr<Self> WeakPointer; \
137 friend struct Object::Deleter;
140 // does not work : why ?
141 // boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt;
144 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
145 public : typedef CLASS Self; \
146 BBTK_OBJECT_MINIMAL_INTERFACE;
149 #define BBTK_OBJECT_INTERFACE(CLASS) \
150 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
152 std::string GetObjectName() const; \
153 std::string GetObjectInfo() const ; \
154 size_t GetObjectSize() const ; \
155 size_t GetObjectInternalSize() const ; \
156 size_t GetObjectRecursiveSize() const ; \
159 CLASS(const CLASS&); \
162 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
163 public : typedef CLASS Self; \
164 BBTK_OBJECT_MINIMAL_INTERFACE; \
167 CLASS(const CLASS&); \
170 //=======================================================================
171 // A struct with one static instance
172 // just to print object list info after main
173 struct StaticInitTime
179 static bool PrintObjectListInfo;
181 static Object mObject;
186 template <class T, class U>
187 inline T BruteForceDownCastPointer(class U p)