00001 #ifndef __FASTJET_SHARED_PTR_HH__
00002 #define __FASTJET_SHARED_PTR_HH__
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 #include "fastjet/internal/base.hh"
00035 #include <cstdlib>
00036
00037
00038
00039
00040
00041 #ifdef USETR1SHAREDPTR
00042 #include <tr1/memory>
00043 #endif // USETR1SHAREDPTR
00044
00045 FASTJET_BEGIN_NAMESPACE
00046
00047 #ifdef USETR1SHAREDPTR
00048
00060 template<class T>
00061 class SharedPtr : public std::tr1::shared_ptr<T> {
00062 public:
00063 SharedPtr() : std::tr1::shared_ptr<T>() {}
00064 SharedPtr(T * t) : std::tr1::shared_ptr<T>(t) {}
00065 SharedPtr(const SharedPtr<T> & t) : std::tr1::shared_ptr<T>(t) {}
00066
00067 inline operator bool() const {return (this->get()!=NULL);}
00069 T* operator ()() const{
00070 return this->get();
00071 }
00072 };
00073
00074
00075 #else // USETR1SHAREDPTR
00076
00106 template<class T>
00107 class SharedPtr{
00108 public:
00110 class __SharedCountingPtr;
00111
00113 SharedPtr() : _ptr(NULL){}
00114
00117 template<class Y> explicit SharedPtr(Y* ptr){
00118 _ptr = new __SharedCountingPtr(ptr);
00119 }
00120
00123 SharedPtr(SharedPtr const & share) : _ptr(NULL){
00124 reset(share);
00125 }
00126
00129 template<class Y> SharedPtr(SharedPtr<Y> const & share) : _ptr(NULL){
00130 reset(share);
00131 }
00132
00134 ~SharedPtr(){
00135
00136 if (_ptr==NULL) return;
00137
00138 _decrease_count();
00139 }
00140
00142 void reset(){
00143
00144
00145
00146 SharedPtr().swap(*this);
00147 }
00148
00150 template<class Y> void reset(Y * ptr){
00151
00152
00153
00154
00155 SharedPtr(ptr).swap(*this);
00156 }
00157
00158
00162 template<class Y> void reset(SharedPtr<Y> const & share){
00163
00164 if (_ptr!=NULL){
00165
00166
00167
00168
00169
00170 if (_ptr == share._get_container()) return;
00171
00172 _decrease_count();
00173 }
00174
00175
00176
00177
00178 _ptr = share._get_container();
00179
00180 if (_ptr!=NULL)
00181 (*_ptr)++;
00182 }
00183
00186 SharedPtr& operator=(SharedPtr const & share){
00187 reset(share);
00188 return *this;
00189 }
00190
00193 template<class Y> SharedPtr& operator=(SharedPtr<Y> const & share){
00194 reset(share);
00195 return *this;
00196 }
00197
00199 T* operator ()() const{
00200 if (_ptr==NULL) return NULL;
00201 return _ptr->get();
00202 }
00203
00209 inline T& operator*() const{
00210 return *(_ptr->get());
00211 }
00212
00218 inline T* operator->() const{
00219 if (_ptr==NULL) return NULL;
00220 return _ptr->get();
00221 }
00222
00224 inline T* get() const{
00225 if (_ptr==NULL) return NULL;
00226 return _ptr->get();
00227 }
00228
00230 inline bool unique() const{
00231 return (use_count()==1);
00232 }
00233
00235 inline long use_count() const{
00236 if (_ptr==NULL) return 0;
00237 return _ptr->use_count();
00238 }
00239
00242 inline operator bool() const{
00243 return (get()!=NULL);
00244 }
00245
00247 inline void swap(SharedPtr & share){
00248 __SharedCountingPtr* share_container = share._ptr;
00249 share._ptr = _ptr;
00250 _ptr = share_container;
00251 }
00252
00263 class __SharedCountingPtr{
00264 public:
00266 __SharedCountingPtr() : _ptr(NULL), _count(0){}
00267
00269 template<class Y> explicit __SharedCountingPtr(Y* ptr) : _ptr(ptr), _count(1){}
00270
00272 ~__SharedCountingPtr(){
00273
00274 if (_ptr!=NULL){ delete _ptr;}
00275 }
00276
00278 inline T* get() const {return _ptr;}
00279
00281 inline long use_count() const {return _count;}
00282
00284 inline long operator++(int unused){return _count++;}
00285
00287 inline long operator--(int unused){return _count--;}
00288
00290 inline long operator++(){return ++_count;}
00291
00293 inline long operator--(){return --_count;}
00294
00295 private:
00296 T *_ptr;
00297 long _count;
00298 };
00299
00300 private:
00302 inline __SharedCountingPtr* _get_container() const{
00303 return _ptr;
00304 }
00305
00308 void _decrease_count(){
00309
00310 (*_ptr)--;
00311
00312
00313 if (_ptr->use_count()==0)
00314 delete _ptr;
00315 }
00316
00317
00318 __SharedCountingPtr *_ptr;
00319 };
00320
00321
00323 template<class T,class U>
00324 inline bool operator==(SharedPtr<T> const & t, SharedPtr<U> const & u){
00325 return t.get() == u.get();
00326 }
00327
00329 template<class T,class U>
00330 inline bool operator!=(SharedPtr<T> const & t, SharedPtr<U> const & u){
00331 return t.get() != u.get();
00332 }
00333
00335 template<class T,class U>
00336 inline bool operator<(SharedPtr<T> const & t, SharedPtr<U> const & u){
00337 return t.get() < u.get();
00338 }
00339
00341 template<class T>
00342 inline void swap(SharedPtr<T> & a, SharedPtr<T> & b){
00343 return a.swap(b);
00344 }
00345
00347 template<class T>
00348 inline T* get_pointer(SharedPtr<T> const & t){
00349 return t.get();
00350 }
00351
00352 #endif // USETR1SHAREDPTR
00353
00354 FASTJET_END_NAMESPACE
00355
00356 #endif // __FASTJET_SHARED_PTR_HH__