00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef OPENALPP_REF_PTR
00015 #define OPENALPP_REF_PTR 1
00016
00017 namespace openalpp {
00018
00020 template<class T>
00021 class ref_ptr
00022 {
00023
00024 public:
00025 typedef T element_type;
00026
00027 ref_ptr() :_ptr(0L) {}
00028 ref_ptr(T* t):_ptr(t) { if (_ptr) _ptr->ref(); }
00029 ref_ptr(const ref_ptr& rp):_ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
00030 ~ref_ptr() { if (_ptr) _ptr->unref(); _ptr=0; }
00031
00032 inline ref_ptr& operator = (const ref_ptr& rp)
00033 {
00034 if (_ptr==rp._ptr) return *this;
00035 T* tmp_ptr = _ptr;
00036 _ptr = rp._ptr;
00037 if (_ptr) _ptr->ref();
00038
00039
00040
00041 if (tmp_ptr) tmp_ptr->unref();
00042 return *this;
00043 }
00044
00045 inline ref_ptr& operator = (T* ptr)
00046 {
00047 if (_ptr==ptr) return *this;
00048 T* tmp_ptr = _ptr;
00049 _ptr = ptr;
00050 if (_ptr) _ptr->ref();
00051
00052
00053
00054 if (tmp_ptr) tmp_ptr->unref();
00055 return *this;
00056 }
00057
00058
00059 inline bool operator == (const ref_ptr& rp) const { return (_ptr==rp._ptr); }
00060 inline bool operator != (const ref_ptr& rp) const { return (_ptr!=rp._ptr); }
00061 inline bool operator < (const ref_ptr& rp) const { return (_ptr<rp._ptr); }
00062 inline bool operator > (const ref_ptr& rp) const { return (_ptr>rp._ptr); }
00063
00064
00065 inline bool operator == (const T* ptr) const { return (_ptr==ptr); }
00066 inline bool operator != (const T* ptr) const { return (_ptr!=ptr); }
00067 inline bool operator < (const T* ptr) const { return (_ptr<ptr); }
00068 inline bool operator > (const T* ptr) const { return (_ptr>ptr); }
00069
00070
00071 inline T& operator*() { return *_ptr; }
00072
00073 inline const T& operator*() const { return *_ptr; }
00074
00075 inline T* operator->() { return _ptr; }
00076
00077 inline const T* operator->() const { return _ptr; }
00078
00079 inline bool operator!() const { return _ptr==0L; }
00080
00081 inline bool valid() const { return _ptr!=0L; }
00082
00083 inline T* get() { return _ptr; }
00084
00085 inline const T* get() const { return _ptr; }
00086
00091 inline T* take() { return release();}
00092
00093 inline T* release() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=0; return tmp;}
00094
00095 private:
00096 T* _ptr;
00097 };
00098
00099 }
00100
00101 #endif