1 #ifndef __FASTJET_SHARED_PTR_HH__
2 #define __FASTJET_SHARED_PTR_HH__
34 #include "fastjet/internal/base.hh"
41 #ifdef __FASTJET_USETR1SHAREDPTR
43 #endif // __FASTJET_USETR1SHAREDPTR
45 FASTJET_BEGIN_NAMESPACE
47 #ifdef __FASTJET_USETR1SHAREDPTR
68 class SharedPtr :
public std::tr1::shared_ptr<T> {
70 SharedPtr() : std::tr1::shared_ptr<T>() {}
71 SharedPtr(T * t) : std::tr1::shared_ptr<T>(t) {}
72 SharedPtr(
const SharedPtr<T> & t) : std::tr1::shared_ptr<T>(t) {}
74 inline operator bool()
const {
return (this->
get()!=NULL);}
76 T* operator ()()
const{
82 #else // __FASTJET_USETR1SHAREDPTR
119 class __SharedCountingPtr;
127 _ptr =
new __SharedCountingPtr(ptr);
133 if (_ptr!=NULL) ++(*_ptr);
150 if (_ptr==NULL)
return;
165 template<
class Y>
void reset(Y * ptr){
186 if (_ptr == share._get_container())
return;
194 _ptr = share._get_container();
196 if (_ptr!=NULL) ++(*_ptr);
214 T* operator ()()
const{
215 if (_ptr==NULL)
return NULL;
226 return *(_ptr->get());
236 if (_ptr==NULL)
return NULL;
241 inline T*
get()
const{
242 if (_ptr==NULL)
return NULL;
248 return (use_count()==1);
253 if (_ptr==NULL)
return 0;
254 return _ptr->use_count();
259 inline operator bool()
const{
260 return (
get()!=NULL);
265 __SharedCountingPtr* share_container = share._ptr;
267 _ptr = share_container;
273 if (_ptr==NULL)
return;
274 _ptr->set_count(count);
287 class __SharedCountingPtr{
290 __SharedCountingPtr() : _ptr(NULL), _count(0){}
293 template<
class Y>
explicit __SharedCountingPtr(Y* ptr) : _ptr(ptr), _count(1){}
296 ~__SharedCountingPtr(){
298 if (_ptr!=NULL){
delete _ptr;}
302 inline T*
get()
const {
return _ptr;}
305 inline long use_count()
const {
return _count;}
308 inline long operator++(){
return ++_count;}
311 inline long operator--(){
return --_count;}
316 inline long operator++(
int){
return _count++;}
321 inline long operator--(
int){
return _count--;}
325 void set_count(
const long & count){
336 inline __SharedCountingPtr* _get_container()
const{
350 void _decrease_count(){
355 if (_ptr->use_count()==0)
360 __SharedCountingPtr *_ptr;
365 template<
class T,
class U>
367 return t.get() == u.get();
371 template<
class T,
class U>
373 return t.get() != u.get();
377 template<
class T,
class U>
379 return t.
get() < u.get();
394 #endif // __FASTJET_USETR1SHAREDPTR
396 FASTJET_END_NAMESPACE
398 #endif // __FASTJET_SHARED_PTR_HH__