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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #ifndef INCLUDED_SCV_SHARED_PTR_H
00047 #define INCLUDED_SCV_SHARED_PTR_H
00048
00049 #include <assert.h>
00050
00051 #ifdef SCV_USE_IOSTREAM_H
00052 # include <iostream.h>
00053 #else
00054 # include <iostream>
00055 #endif
00056
00057 #define LOCAL_MEMORY_MANAGEMENT \
00058 static int *_createCountP() { return new int(); } \
00059 static void _freeCountP(int *countP) { delete countP; }
00060
00061 template<typename T>
00062 class scv_shared_ptr {
00063 public:
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 scv_shared_ptr(T *coreP=NULL) : _coreP(coreP), _countP(NULL) {
00074 if (_coreP) { _countP = _createCountP(); *_countP = 1; }
00075 }
00076
00077 scv_shared_ptr(T *coreP, int *countP) : _coreP(coreP), _countP(countP) {
00078 if (_countP) { ++(*_countP); }
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 scv_shared_ptr(const scv_shared_ptr<T>& t)
00090 : _coreP(t._coreP), _countP(t._countP) { if (_countP) ++(*_countP); }
00091
00092
00093
00094
00095 ~scv_shared_ptr() {
00096 if (_coreP) { --(*_countP); if (*_countP == 0) { delete _coreP; _freeCountP(_countP); } }
00097 }
00098
00099 template<typename T2>
00100 operator scv_shared_ptr<T2> () { return scv_shared_ptr<T2>(_coreP,_countP); }
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 scv_shared_ptr& operator=(const scv_shared_ptr& t) {
00111 if (t._countP != _countP) {
00112 if (t._coreP) ++(*t._countP);
00113 if (_coreP) {
00114 --(*_countP);
00115 if (*_countP == 0) { delete _coreP; _freeCountP(_countP); }
00116 }
00117 _coreP = t._coreP;
00118 _countP = t._countP;
00119 }
00120 return *this;
00121 }
00122
00123 bool compare(const scv_shared_ptr& other) const {
00124 return this->_coreP == other._coreP;
00125 }
00126
00127 bool isNull() const {
00128 return this->_coreP == NULL;
00129 }
00130
00131
00132
00133
00134 T& operator*() {
00135 if (_coreP) return *_coreP;
00136 assert(0);
00137 return *(T*)0;
00138 }
00139 const T& operator*() const {
00140 if (_coreP) return *_coreP;
00141 assert(0);
00142 return *(T*)0;
00143 }
00144 T *operator->() {
00145 if (_coreP) return _coreP;
00146 assert(0);
00147 return NULL ;
00148 }
00149 const T *operator->() const {
00150 if (_coreP) return _coreP;
00151 assert(0);
00152 return NULL ;
00153 }
00154 #if 0
00155 friend ostream& operator<<(ostream& os, const scv_shared_ptr& a) {
00156 os << *a._coreP << endl;
00157 return os;
00158 }
00159 #endif
00160
00161 private:
00162 T *_coreP;
00163 int *_countP;
00164
00165 LOCAL_MEMORY_MANAGEMENT
00166 };
00167
00168 #undef LOCAL_MEMORY_MANAGEMENT
00169
00170 template <class T>
00171 bool operator==(const scv_shared_ptr<T>& a, const scv_shared_ptr<T>& b) {
00172 return a.compare(b);
00173 }
00174 template <class T>
00175 bool operator!=(const scv_shared_ptr<T>& a, const scv_shared_ptr<T>& b) {
00176 return !(a==b);
00177 }
00178 template <class T>
00179 ostream& operator<<(ostream& os, const scv_shared_ptr<T>& a) {
00180
00181 os << *a << endl;
00182 return os;
00183 }
00184
00185 #endif // INCLUDED_SCV_SHARED_PTR_H