bbtkObject.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00037 #ifndef __bbtkObject_h__
00038 #define __bbtkObject_h__
00039
00040 #include <bbtkSystem.h>
00041 #include <boost/shared_ptr.hpp>
00042 #include <boost/weak_ptr.hpp>
00043 #include <set>
00044
00045 namespace bbtk
00046 {
00047
00048
00049 class BBTK_EXPORT Object
00050 {
00051 public:
00052 typedef boost::shared_ptr<Object> Pointer;
00053 typedef boost::weak_ptr<Object> WeakPointer;
00054
00055 Object();
00056 virtual ~Object();
00057
00058 virtual std::string GetObjectName() const;
00059 virtual std::string GetObjectInfo() const;
00060 virtual size_t GetObjectSize() const { return sizeof(Object); }
00061 virtual size_t GetObjectInternalSize() const { return sizeof(Object); }
00062 virtual size_t GetObjectRecursiveSize() const { return sizeof(Object); }
00063 long GetUseCount() { return mThisPointer.use_count(); }
00064
00065 static void InsertInObjectList(Pointer);
00066 static void RemoveFromObjectList(WeakPointer);
00067
00068 static void InsertInPackageList(Pointer);
00069 static void ReleasePackages();
00070
00071 static void PrintObjectListInfo(const std::string& name);
00072
00073 static void PrintObjectInfo(const Pointer& o);
00074
00075 static long GetObjectsCount() { return mgObjectList.size(); }
00076
00078 struct BBTK_EXPORT Deleter
00079 {
00080 Deleter() : mPointer() {}
00081 virtual ~Deleter() {}
00082 virtual void operator() (Object* p);
00083 virtual void Delete(Object* p) { delete p; }
00084 WeakPointer mPointer;
00085 };
00086
00087 protected:
00088 void LockThis() { mThisPointerLocked = mThisPointer.lock(); }
00089 void UnLockThis() { mThisPointerLocked = Pointer(); }
00090
00091 template <class U>
00092 boost::shared_ptr<U> GetThisPointer() const
00093 {
00094 return boost::dynamic_pointer_cast<U>(mThisPointer.lock());
00095 }
00096 template <class U>
00097 static boost::shared_ptr<U> MakePointer(U* s, bool lock = false)
00098 {
00099 if (s->mThisPointer.lock())
00100 {
00101 boost::shared_ptr<U> p = s->GetThisPointer<U>();
00102 if (!lock) s->mThisPointerLocked.reset();
00103 return p;
00104 }
00105 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,Object::Deleter());
00106 static_cast<Object::Deleter*>
00107 (p._internal_get_deleter(typeid(Object::Deleter)))
00108 ->mPointer = p;
00109 s->mThisPointer = p;
00110 Object::InsertInObjectList(p);
00111 if (lock) s->LockThis();
00112 return p;
00113 }
00114 template <class U, class D>
00115 static boost::shared_ptr<U> MakePointer(U* s,
00116 const D& del,
00117 bool lock = false)
00118 {
00119 if (s->mThisPointer.lock())
00120 {
00121 boost::shared_ptr<U> p = s->GetThisPointer<U>();
00122 if (!lock) s->mThisPointerLocked.reset();
00123 return p;
00124 }
00125 boost::shared_ptr<U> p = boost::shared_ptr<U>(s,del);
00126 static_cast<D*>
00127 (p._internal_get_deleter(typeid(D)))
00128 ->mPointer = p;
00129 s->mThisPointer = p;
00130 Object::InsertInObjectList(p);
00131 if (lock) s->LockThis();
00132 return p;
00133 }
00134
00135
00136 private:
00137 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
00138 static ObjectListType mgObjectList;
00139 static ObjectListType mgPackageList;
00140 WeakPointer mThisPointer;
00141 Pointer mThisPointerLocked;
00142
00143 };
00144
00145 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
00146
00147 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
00148 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
00149 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
00150
00151
00152 #define BBTK_OBJECT_MINIMAL_INTERFACE \
00153 public: \
00154 typedef boost::shared_ptr<Self> Pointer; \
00155 typedef boost::weak_ptr<Self> WeakPointer; \
00156 friend struct Object::Deleter;
00157
00158
00159
00160
00161
00162
00163 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
00164 public : typedef CLASS Self; \
00165 BBTK_OBJECT_MINIMAL_INTERFACE;
00166
00167
00168 #define BBTK_OBJECT_INTERFACE(CLASS) \
00169 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
00170 public: \
00171 std::string GetObjectName() const; \
00172 std::string GetObjectInfo() const ; \
00173 size_t GetObjectSize() const ; \
00174 size_t GetObjectInternalSize() const ; \
00175 size_t GetObjectRecursiveSize() const ; \
00176 protected: \
00177 CLASS(); \
00178 CLASS(const CLASS&); \
00179 ~CLASS();
00180
00181 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS) \
00182 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
00183 public: \
00184 std::string GetObjectName() const; \
00185 std::string GetObjectInfo() const ; \
00186 size_t GetObjectSize() const ; \
00187 size_t GetObjectInternalSize() const ; \
00188 size_t GetObjectRecursiveSize() const ;
00189
00190 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
00191 public : typedef CLASS Self; \
00192 BBTK_OBJECT_MINIMAL_INTERFACE; \
00193 protected: \
00194 CLASS(); \
00195 CLASS(const CLASS&); \
00196 virtual ~CLASS();
00197
00198
00199
00200
00201 class BBTK_EXPORT StaticInitTime
00202 {
00203 public:
00204 StaticInitTime();
00205 ~StaticInitTime();
00206
00207
00208 static bool PrintObjectListInfo;
00209 private:
00210 static bbtk::Object mObject;
00211 };
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 }
00223
00224 #endif
00225