1 #ifndef __FASTJET_SHARED_PTR_HH__ 2 #define __FASTJET_SHARED_PTR_HH__ 32 #include "fastjet/internal/base.hh" 39 #ifdef __FASTJET_USETR1SHAREDPTR 41 #endif // __FASTJET_USETR1SHAREDPTR 43 FASTJET_BEGIN_NAMESPACE
45 #ifdef __FASTJET_USETR1SHAREDPTR 66 class SharedPtr :
public std::tr1::shared_ptr<T> {
68 SharedPtr() :
std::tr1::shared_ptr<T>() {}
69 SharedPtr(T * t) :
std::tr1::shared_ptr<T>(t) {}
70 SharedPtr(
const SharedPtr<T> & t) :
std::tr1::shared_ptr<T>(t) {}
72 inline operator bool()
const {
return (this->
get()!=NULL);}
74 T* operator ()()
const{
80 #else // __FASTJET_USETR1SHAREDPTR 117 class __SharedCountingPtr;
125 _ptr =
new __SharedCountingPtr(ptr);
131 if (_ptr!=NULL) ++(*_ptr);
148 if (_ptr==NULL)
return;
163 template<
class Y>
void reset(Y * ptr){
184 if (_ptr == share._get_container())
return;
192 _ptr = share._get_container();
194 if (_ptr!=NULL) ++(*_ptr);
212 T* operator ()()
const{
213 if (_ptr==NULL)
return NULL;
224 return *(_ptr->get());
234 if (_ptr==NULL)
return NULL;
239 inline T*
get()
const{
240 if (_ptr==NULL)
return NULL;
246 return (use_count()==1);
251 if (_ptr==NULL)
return 0;
252 return _ptr->use_count();
257 inline operator bool()
const{
258 return (
get()!=NULL);
263 __SharedCountingPtr* share_container = share._ptr;
265 _ptr = share_container;
271 if (_ptr==NULL)
return;
272 _ptr->set_count(count);
285 class __SharedCountingPtr{
288 __SharedCountingPtr() : _ptr(NULL), _count(0){}
291 template<
class Y>
explicit __SharedCountingPtr(Y* ptr) : _ptr(ptr), _count(1){}
294 ~__SharedCountingPtr(){
296 if (_ptr!=NULL){
delete _ptr;}
300 inline T*
get()
const {
return _ptr;}
303 inline long use_count()
const {
return _count;}
306 inline long operator++(){
return ++_count;}
309 inline long operator--(){
return --_count;}
314 inline long operator++(
int){
return _count++;}
319 inline long operator--(
int){
return _count--;}
323 void set_count(
const long & count){
334 inline __SharedCountingPtr* _get_container()
const{
348 void _decrease_count(){
353 if (_ptr->use_count()==0)
358 __SharedCountingPtr *_ptr;
363 template<
class T,
class U>
365 return t.get() == u.get();
369 template<
class T,
class U>
371 return t.get() != u.get();
375 template<
class T,
class U>
377 return t.
get() < u.get();
392 #endif // __FASTJET_USETR1SHAREDPTR 394 FASTJET_END_NAMESPACE
396 #endif // __FASTJET_SHARED_PTR_HH__ T & operator*() const
indirection, get a reference to the stored pointer
T * get_pointer(SharedPtr< T > const &t)
getting the pointer
T * get() const
get the stored pointer
SharedPtr & operator=(SharedPtr< Y > const &share)
overload the = operator so that it updates count
SharedPtr(SharedPtr const &share)
overload the copy ctor so that it updates count
void reset(Y *ptr)
reset from a pointer
void reset(SharedPtr< Y > const &share)
do a smart copy
bool operator==(const PseudoJet &a, const PseudoJet &b)
returns true if the 4 momentum components of the two PseudoJets are identical and all the internal in...
void swap(SharedPtr &share)
exchange the content of the two pointers
SharedPtr(Y *ptr)
initialise with the main data
bool unique() const
check if the instance is unique
an implementation of C++0x shared pointers (or boost's)
bool operator!=(const PseudoJet &a, const PseudoJet &b)
inequality test which is exact opposite of operator==
void swap(SharedPtr< T > &a, SharedPtr< T > &b)
swapping
long use_count() const
return the number of counts
T * operator->() const
indirection, get the stored pointer
SharedPtr & operator=(SharedPtr const &share)
overload the = operator so that it updates count
void set_count(const long &count)
force the count to be set to a specified value
void reset()
reset the pointer to default value (NULL)