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 #ifndef _CEGUIRefCounted_h_
00029 #define _CEGUIRefCounted_h_
00030
00031
00032 namespace CEGUI
00033 {
00039 template<typename T>
00040 class CEGUIEXPORT RefCounted
00041 {
00042 public:
00047 RefCounted() :
00048 d_object(0),
00049 d_count(0)
00050 {
00051 }
00052
00057 RefCounted(T* ob) :
00058 d_object(ob),
00059 d_count((ob != 0) ? new unsigned int(1) : 0)
00060 {
00061 }
00062
00067 RefCounted(const RefCounted<T>& other) :
00068 d_object(other.d_object),
00069 d_count(other.d_count)
00070 {
00071 if (d_count)
00072 addRef();
00073 }
00074
00080 ~RefCounted()
00081 {
00082 if (d_object)
00083 release();
00084 }
00085
00092 RefCounted<T>& operator=(const RefCounted<T>& other)
00093 {
00094 if (*this != other)
00095 {
00096 if (d_object)
00097 release();
00098
00099 d_object = other.d_object;
00100 d_count = d_object ? other.d_count : 0;
00101
00102 if (d_count)
00103 addRef();
00104 }
00105
00106 return *this;
00107 }
00108
00114 bool operator==(const RefCounted<T>& other) const
00115 {
00116 return d_object == other.d_object;
00117 }
00118
00124 bool operator!=(const RefCounted<T>& other) const
00125 {
00126 return d_object != other.d_object;
00127 }
00128
00134 const T& operator*() const
00135 {
00136 return *d_object;
00137 }
00138
00139 T& operator*()
00140 {
00141 return *d_object;
00142 }
00143
00148 const T* operator->() const
00149 {
00150 return d_object;
00151 }
00152
00153 T* operator->()
00154 {
00155 return d_object;
00156 }
00157
00162 bool isValid() const
00163 {
00164 return d_object != 0;
00165 }
00166
00167 private:
00172 void addRef()
00173 {
00174 ++*d_count;
00175 }
00176
00182 void release()
00183 {
00184 if (!--*d_count)
00185 {
00186 delete d_object;
00187 delete d_count;
00188 d_object = 0;
00189 d_count = 0;
00190 }
00191 }
00192
00193 T* d_object;
00194 unsigned int* d_count;
00195 };
00196
00197 }
00198
00199 #endif // end of guard _CEGUIRefCounted_h_