1 /*=========================================================================
4 Module: $RCSfile: bbtkObject.h,v $
6 Date: $Date: 2008/07/24 14:37:05 $
7 Version: $Revision: 1.6 $
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 InsertInPackageList(Pointer);
56 static void ReleasePackages();
58 static void PrintObjectListInfo(const std::string& name);
59 // static void PrintObjectInfo(const std::string& name);
60 static void PrintObjectInfo(const Pointer& o);
62 static long GetObjectsCount() { return mgObjectList.size(); }
64 /// Default objects deleter : removes object from list on deletion
65 struct BBTK_EXPORT Deleter
67 Deleter() : mPointer() {}
69 virtual void operator() (Object* p);
70 virtual void Delete(Object* p) { delete p; }
75 void LockThis() { mThisPointerLocked = mThisPointer.lock(); }
76 void UnLockThis() { mThisPointerLocked = Pointer(); }
77 // Object::Pointer GetThis() const { return mThisPointer.lock(); }
79 boost::shared_ptr<U> GetThisPointer() const
81 return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
84 static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
86 if (s->mThisPointer.lock())
88 boost::shared_ptr<U> p = s->GetThisPointer<U>();
89 if (!lock) s->mThisPointerLocked.reset();
92 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
93 static_cast<Object::Deleter*>
94 (p._internal_get_deleter(typeid(Object::Deleter)))
97 Object::InsertInObjectList(p);
98 if (lock) s->LockThis();
101 template <class U, class D>
102 static boost::shared_ptr<U> MakePointer(U* s,
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,del);
114 (p._internal_get_deleter(typeid(D)))
117 Object::InsertInObjectList(p);
118 if (lock) s->LockThis();
124 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
125 static ObjectListType mgObjectList;
126 static ObjectListType mgPackageList;
127 WeakPointer mThisPointer;
128 Pointer mThisPointerLocked;
132 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
134 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
135 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
136 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
139 #define BBTK_OBJECT_MINIMAL_INTERFACE \
141 typedef boost::shared_ptr<Self> Pointer; \
142 typedef boost::weak_ptr<Self> WeakPointer; \
143 friend struct Object::Deleter;
146 // does not work : why ?
147 // boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt;
150 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
151 public : typedef CLASS Self; \
152 BBTK_OBJECT_MINIMAL_INTERFACE;
155 #define BBTK_OBJECT_INTERFACE(CLASS) \
156 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
158 std::string GetObjectName() const; \
159 std::string GetObjectInfo() const ; \
160 size_t GetObjectSize() const ; \
161 size_t GetObjectInternalSize() const ; \
162 size_t GetObjectRecursiveSize() const ; \
165 CLASS(const CLASS&); \
168 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS) \
169 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
171 std::string GetObjectName() const; \
172 std::string GetObjectInfo() const ; \
173 size_t GetObjectSize() const ; \
174 size_t GetObjectInternalSize() const ; \
175 size_t GetObjectRecursiveSize() const ;
177 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
178 public : typedef CLASS Self; \
179 BBTK_OBJECT_MINIMAL_INTERFACE; \
182 CLASS(const CLASS&); \
185 //=======================================================================
186 // A struct with one static instance
187 // just to print object list info after main
188 class BBTK_EXPORT StaticInitTime
195 static bool PrintObjectListInfo;
197 static bbtk::Object mObject;
202 template <class T, class U>
203 inline T BruteForceDownCastPointer(class U p)