00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __SHARED_PTR_H
00021 #define __SHARED_PTR_H
00022
00023 #include <iostream>
00024
00025 namespace mdw
00026 {
00027 template <class T>
00028 class SharedPtr
00029 {
00030 public:
00031 SharedPtr()
00032 {
00033 _internal = 0;
00034 _counter = 0;
00035 }
00036 SharedPtr (T* pointer)
00037 {
00038 _internal = pointer;
00039 _counter = new int;
00040 _released = new bool;
00041 *_counter = 1;
00042 *_released = false;
00043 }
00044 SharedPtr (const SharedPtr & other)
00045 {
00046 if (other._internal != 0 && other._counter != 0)
00047 {
00048 _internal = other._internal;
00049 _counter = other._counter;
00050 _released = other._released;
00051 ++ (*_counter);
00052 }
00053 else
00054 {
00055 _internal = 0;
00056 _counter = 0;
00057 _released = 0;
00058 }
00059 }
00060 ~SharedPtr()
00061 {
00062 if (_internal != 0 && _counter != 0)
00063 {
00064 -- (*_counter);
00065 if (*_counter == 0)
00066 {
00067 if (!*_released)
00068 {
00069 delete _internal;
00070 }
00071 delete _counter;
00072 delete _released;
00073 }
00074 }
00075 }
00076 SharedPtr & operator= (const SharedPtr & other)
00077 {
00078 if (this == &other)
00079 {
00080 return;
00081 }
00082 if (other._internal != 0 && other._counter != 0)
00083 {
00084 _internal = other._internal;
00085 _counter = other._counter;
00086 _released = other._released;
00087 ++ (*_counter);
00088 }
00089 else
00090 {
00091 _internal = 0;
00092 _counter = 0;
00093 _released = 0;
00094 }
00095 return *this;
00096 }
00097 SharedPtr & operator= (T* pointer)
00098 {
00099 if (_internal != NULL && _counter != NULL)
00100 {
00101 -- (*_counter);
00102 if (*_counter == 0)
00103 {
00104 delete _internal;
00105 }
00106 else
00107 {
00108 _counter = new int;
00109 _released = new bool;
00110 }
00111 *_counter = 1;
00112 _released = false;
00113 _internal = pointer;
00114 }
00115 else
00116 {
00117 _internal = pointer;
00118 _counter = new int;
00119 *_counter = 1;
00120 }
00121 return *this;
00122 }
00123 T & operator*()
00124 {
00125 return *_internal;
00126 }
00127 T * operator->()
00128 {
00129 return _internal;
00130 }
00131 const T & operator*() const
00132 {
00133 return *_internal;
00134 }
00135 const T* operator->() const
00136 {
00137 return _internal;
00138 }
00139 const T* get() const
00140 {
00141 return _internal;
00142 }
00143 T* release()
00144 {
00145 if (_internal != NULL && _counter != NULL)
00146 {
00147 -- (*_counter);
00148 if (*_counter == 0)
00149 {
00150 delete _counter;
00151 _counter = 0;
00152 delete _released;
00153 _released = 0;
00154 }
00155 else
00156 {
00157 _released = true;
00158 }
00159 }
00160 T* res = _internal;
00161 _internal = 0;
00162 return res;
00163 }
00164 int getCount() const
00165 {
00166 return *_counter;
00167 }
00168 bool isNull() const
00169 {
00170 return _internal == 0;
00171 }
00172 private:
00173 T *_internal;
00174 int * _counter;
00175 bool * _released;
00176 };
00177 }
00178
00179 #endif
00180