00001 #ifndef IntrusivePtr_HEADER
00002 #define IntrusivePtr_HEADER
00003
00004
00005
00006
00007
00008 namespace hudson {
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 template <class T, class AddRelease=T>
00034 class IntrusivePtr {
00035 T *ptr_;
00036
00037 void addref() {
00038 if (ptr_) {
00039 AddRelease::add_ref(ptr_);
00040 }
00041 }
00042 void delref() {
00043 if (ptr_) {
00044 AddRelease::release(ptr_);
00045 }
00046 }
00047
00048 public:
00049 IntrusivePtr(): ptr_(0) { }
00050 IntrusivePtr(T *p): ptr_(p) { addref(); }
00051 IntrusivePtr(const IntrusivePtr& other): ptr_(other.get()) { addref(); }
00052 ~IntrusivePtr() { delref(); }
00053
00054 IntrusivePtr& operator= (T *p) {
00055
00056 if (p == ptr_) { return *this; }
00057 delref();
00058 ptr_ = p;
00059 addref();
00060 return *this;
00061 }
00062 IntrusivePtr& operator= (const IntrusivePtr& other) {
00063 return this->operator= (other.get());
00064 }
00065
00066 T *get() const {
00067 return ptr_;
00068 }
00069
00070 T& operator*() const {
00071 return *ptr_;
00072 }
00073
00074 T *operator->() const {
00075 return ptr_;
00076 }
00077
00078 operator bool() const {
00079 return ptr_ != 0;
00080 }
00081
00082 bool operator== (const T* p) const { return ptr_ == p; }
00083 bool operator!= (const T* p) const { return ptr_ != p; }
00084
00085 bool operator== (const IntrusivePtr& p) const { return ptr_ == p.get(); }
00086 bool operator!= (const IntrusivePtr& p) const { return ptr_ != p.get(); }
00087 };
00088
00089 }
00090
00091 #endif