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
00031
00032
00033 #ifndef __XPLC_PTR_H__
00034 #define __XPLC_PTR_H__
00035
00036 #if defined(__GNUC__) && __GNUC__ > 3
00037 # pragma GCC system_header
00038 #endif
00039
00045 #include <xplc/IObject.h>
00046
00047 #ifndef UNSTABLE
00048 #error "xplc_ptr is experimental!"
00049 #endif
00050
00051
00055 template<class T>
00056 class xplc_ptr {
00057 private:
00058 T* ptr;
00059
00060 class ProtectedPtr: public T {
00061 private:
00062 virtual unsigned int addRef() = 0;
00063 virtual unsigned int release() = 0;
00064 #ifndef __XPLC_DELETE_H__
00065 void operator delete(void*);
00066 #endif
00067 };
00068
00069 xplc_ptr& operator=(const xplc_ptr&);
00070
00071 public:
00072 xplc_ptr():
00073 ptr(0) {
00074 }
00080 explicit xplc_ptr(T* aObj):
00081 ptr(aObj) {
00082 }
00087 template<class P>
00088 explicit xplc_ptr(const xplc_ptr<P>& aObj):
00089 ptr(aObj) {
00090 if(ptr)
00091 ptr->addRef();
00092 }
00093 ~xplc_ptr() {
00094 if(ptr)
00095 ptr->release();
00096 }
00104 ProtectedPtr* operator->() const {
00105 return static_cast<ProtectedPtr*>(ptr);
00106 }
00112 operator ProtectedPtr*() const {
00113 return static_cast<ProtectedPtr*>(ptr);
00114 }
00120 xplc_ptr& operator=(T* _ptr) {
00121 if(_ptr)
00122 _ptr->addRef();
00123
00124 if(ptr)
00125 ptr->release();
00126
00127 ptr = _ptr;
00128
00129 return *this;
00130 }
00131 };
00132
00133
00138 template<class T>
00139 T* do_addRef(T* obj) {
00140 if (obj)
00141 obj->addRef();
00142
00143 return obj;
00144 }
00145
00146
00147 #endif