00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef REF_PTR_H
00025 #define REF_PTR_H
00026
00027 namespace ICQ2000 {
00028
00029 template <typename Object>
00030 class ref_ptr {
00031 protected:
00032 Object *m_instance;
00033
00034
00035
00036
00037 public:
00038 ref_ptr()
00039 : m_instance(NULL)
00040 { }
00041
00042 ref_ptr(const ref_ptr<Object>& that)
00043 : m_instance(that.m_instance)
00044 {
00045 if (m_instance != NULL)
00046 ++(m_instance->count);
00047 }
00048
00049 ref_ptr(Object *o)
00050 : m_instance(o)
00051 {
00052 if (m_instance != NULL)
00053 ++(m_instance->count);
00054
00055 }
00056
00057 ~ref_ptr()
00058 {
00059 if (m_instance != NULL && --(m_instance->count) == 0)
00060 delete m_instance;
00061 }
00062
00063 Object* get()
00064 {
00065 return m_instance;
00066 }
00067
00068 Object* operator->()
00069 {
00070 return m_instance;
00071 }
00072
00073 const Object* operator->() const
00074 {
00075 return m_instance;
00076 }
00077
00078 Object& operator*()
00079 {
00080 return *m_instance;
00081 }
00082
00083 const Object& operator*() const
00084 {
00085 return *m_instance;
00086 }
00087
00088
00089 unsigned int count()
00090 {
00091 return m_instance->count;
00092 }
00093
00094 ref_ptr& operator=(const ref_ptr<Object>& that) {
00095 if (m_instance != NULL && --(m_instance->count) == 0) {
00096 delete m_instance;
00097 }
00098 m_instance = that.m_instance;
00099 if (m_instance != NULL)
00100 ++(m_instance->count);
00101 return *this;
00102 }
00103
00104 };
00105
00106 }
00107
00108 #endif