1 /*=========================================================================
3 Module: $RCSfile: bbtkObject.h,v $
5 Date: $Date: 2009/04/30 14:31:31 $
6 Version: $Revision: 1.8 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 *\brief Object : the top class of bbtk class hierarchy
37 #ifndef __bbtkObject_h__
38 #define __bbtkObject_h__
40 #include <bbtkSystem.h>
41 #include <boost/shared_ptr.hpp>
42 #include <boost/weak_ptr.hpp>
48 // The top class of bbtk class hierarchy
49 class BBTK_EXPORT Object
52 typedef boost::shared_ptr<Object> Pointer;
53 typedef boost::weak_ptr<Object> WeakPointer;
58 virtual std::string GetObjectName() const;
59 virtual std::string GetObjectInfo() const;
60 virtual size_t GetObjectSize() const { return sizeof(Object); }
61 virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
62 virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
63 long GetUseCount() { return mThisPointer.use_count(); }
65 static void InsertInObjectList(Pointer);
66 static void RemoveFromObjectList(WeakPointer);
68 static void InsertInPackageList(Pointer);
69 static void ReleasePackages();
71 static void PrintObjectListInfo(const std::string& name);
72 // static void PrintObjectInfo(const std::string& name);
73 static void PrintObjectInfo(const Pointer& o);
75 static long GetObjectsCount() { return mgObjectList.size(); }
77 /// Default objects deleter : removes object from list on deletion
78 struct BBTK_EXPORT Deleter
80 Deleter() : mPointer() {}
82 virtual void operator() (Object* p);
83 virtual int Delete(Object* p) { delete p; return 0; }
88 void LockThis() { mThisPointerLocked = mThisPointer.lock(); }
89 void UnLockThis() { mThisPointerLocked = Pointer(); }
90 // Object::Pointer GetThis() const { return mThisPointer.lock(); }
92 boost::shared_ptr<U> GetThisPointer() const
94 return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
97 static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
99 if (s->mThisPointer.lock())
101 boost::shared_ptr<U> p = s->GetThisPointer<U>();
102 if (!lock) s->mThisPointerLocked.reset();
105 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
106 static_cast<Object::Deleter*>
107 (p._internal_get_deleter(typeid(Object::Deleter)))
110 Object::InsertInObjectList(p);
111 if (lock) s->LockThis();
114 template <class U, class D>
115 static boost::shared_ptr<U> MakePointer(U* s,
119 if (s->mThisPointer.lock())
121 boost::shared_ptr<U> p = s->GetThisPointer<U>();
122 if (!lock) s->mThisPointerLocked.reset();
125 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
127 (p._internal_get_deleter(typeid(D)))
130 Object::InsertInObjectList(p);
131 if (lock) s->LockThis();
136 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
137 static ObjectListType mgObjectList;
138 static ObjectListType mgPackageList;
139 WeakPointer mThisPointer;
140 Pointer mThisPointerLocked;
144 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
146 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
147 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
148 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
151 #define BBTK_OBJECT_MINIMAL_INTERFACE \
153 typedef boost::shared_ptr<Self> Pointer; \
154 typedef boost::weak_ptr<Self> WeakPointer; \
155 friend struct Object::Deleter;
158 // does not work : why ?
159 // boost::get_deleter<Deleter,Pointer>(pt)->mPointer = pt;
162 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
163 public : typedef CLASS Self; \
164 BBTK_OBJECT_MINIMAL_INTERFACE;
167 #define BBTK_OBJECT_INTERFACE(CLASS) \
168 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
170 std::string GetObjectName() const; \
171 std::string GetObjectInfo() const ; \
172 size_t GetObjectSize() const ; \
173 size_t GetObjectInternalSize() const ; \
174 size_t GetObjectRecursiveSize() const ; \
177 CLASS(const CLASS&); \
180 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS) \
181 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
183 std::string GetObjectName() const; \
184 std::string GetObjectInfo() const ; \
185 size_t GetObjectSize() const ; \
186 size_t GetObjectInternalSize() const ; \
187 size_t GetObjectRecursiveSize() const ;
189 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
190 public : typedef CLASS Self; \
191 BBTK_OBJECT_MINIMAL_INTERFACE; \
194 CLASS(const CLASS&); \
197 //=======================================================================
198 // A struct with one static instance
199 // just to print object list info after main
200 class BBTK_EXPORT StaticInitTime
207 static bool PrintObjectListInfo;
209 static bbtk::Object mObject;
214 template <class T, class U>
215 inline T BruteForceDownCastPointer(class U p)