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 int Delete(Object* p) { delete p; return 0; }
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 private:
00136 typedef std::set<boost::weak_ptr<Object> > ObjectListType;
00137 static ObjectListType mgObjectList;
00138 static ObjectListType mgPackageList;
00139 WeakPointer mThisPointer;
00140 Pointer mThisPointerLocked;
00141
00142 };
00143
00144 #define BBTK_OBJECT_DEFINE_SELF(CLASS) public : typedef CLASS Self;
00145
00146 #define BBTK_FORWARD_DECLARE_POINTER(CLASS) \
00147 typedef boost::shared_ptr<CLASS> CLASS ## Pointer; \
00148 typedef boost::weak_ptr<CLASS> CLASS ## WeakPointer;
00149
00150
00151 #define BBTK_OBJECT_MINIMAL_INTERFACE \
00152 public: \
00153 typedef boost::shared_ptr<Self> Pointer; \
00154 typedef boost::weak_ptr<Self> WeakPointer; \
00155 friend struct Object::Deleter;
00156
00157
00158
00159
00160
00161
00162 #define BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS) \
00163 public : typedef CLASS Self; \
00164 BBTK_OBJECT_MINIMAL_INTERFACE;
00165
00166
00167 #define BBTK_OBJECT_INTERFACE(CLASS) \
00168 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
00169 public: \
00170 std::string GetObjectName() const; \
00171 std::string GetObjectInfo() const ; \
00172 size_t GetObjectSize() const ; \
00173 size_t GetObjectInternalSize() const ; \
00174 size_t GetObjectRecursiveSize() const ; \
00175 protected: \
00176 CLASS(); \
00177 CLASS(const CLASS&); \
00178 ~CLASS();
00179
00180 #define BBTK_OBJECT_INTERFACE_NO_CONDES(CLASS) \
00181 BBTK_OBJECT_MINIMAL_INTERFACE_WITH_SELF(CLASS); \
00182 public: \
00183 std::string GetObjectName() const; \
00184 std::string GetObjectInfo() const ; \
00185 size_t GetObjectSize() const ; \
00186 size_t GetObjectInternalSize() const ; \
00187 size_t GetObjectRecursiveSize() const ;
00188
00189 #define BBTK_ABSTRACT_OBJECT_INTERFACE(CLASS) \
00190 public : typedef CLASS Self; \
00191 BBTK_OBJECT_MINIMAL_INTERFACE; \
00192 protected: \
00193 CLASS(); \
00194 CLASS(const CLASS&); \
00195 virtual ~CLASS();
00196
00197
00198
00199
00200 class BBTK_EXPORT StaticInitTime
00201 {
00202 public:
00203 StaticInitTime();
00204 ~StaticInitTime();
00205
00206
00207 static bool PrintObjectListInfo;
00208 private:
00209 static bbtk::Object mObject;
00210 };
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 }
00222
00223 #endif
00224