00001 #ifndef MObject_H 00002 #define MObject_H 00003 00004 #include <cstddef> 00005 #include <string> 00006 #include "boost/smart_ptr/intrusive_ptr.hpp" 00007 #include "boost/smart_ptr/shared_ptr.hpp" 00008 00009 using namespace std; 00010 namespace momdp 00011 { 00012 class MObject; 00013 void intrusive_ptr_add_ref(MObject * p); 00014 void intrusive_ptr_release(MObject * p); 00015 } 00016 using namespace momdp; 00017 00018 namespace momdp 00019 { 00020 // Base class similar to Java Object 00021 // Provide following functions: 00022 // Memory usage counting: 00023 // thisSize 00024 // update Memory usage to GlobalResource::getInstance()->memoryUsage by overloading new and delete 00025 00026 // Reference counting for intrusive smart point : referenceCount 00027 00028 class MObject 00029 { 00030 private: 00031 size_t thisSize; 00032 int referenceCount; 00033 friend void intrusive_ptr_add_ref(MObject * p); 00034 friend void intrusive_ptr_release(MObject * p); 00035 00036 public: 00037 MObject(void); 00038 virtual ~MObject(void); 00039 00040 virtual string ToString(); 00041 00042 void* operator new(size_t nSize); 00043 void operator delete(void* p); 00044 00045 }; 00046 00047 // WARNING: this implementation is not thread-safe 00048 // add in mutex for critical section to make it thread-safe 00049 // remember to modify and test this section when APPL become multi-threaded program... 00050 00051 inline void intrusive_ptr_add_ref(MObject * p) 00052 { 00053 // increment reference count of object *p 00054 ++(p->referenceCount); 00055 } 00056 00057 00058 00059 inline void intrusive_ptr_release(MObject * p) 00060 { 00061 // decrement reference count, and delete object when reference count reaches 0 00062 if (--(p->referenceCount) == 0) 00063 { 00064 delete p; 00065 } 00066 } 00067 00068 } 00069 00070 #define SharedPointer boost::intrusive_ptr 00071 #define dynamic_pointer_cast boost::dynamic_pointer_cast 00072 #define static_pointer_cast boost::static_pointer_cast 00073 00074 #endif 00075