1 #ifndef __FASTJET_SHARED_PTR_HH__ 2 #define __FASTJET_SHARED_PTR_HH__ 34 #include "fastjet/internal/base.hh" 41 #include "fastjet/internal/deprecated.hh" 43 #ifdef __FASTJET_USETR1SHAREDPTR 45 #endif // __FASTJET_USETR1SHAREDPTR 47 FASTJET_BEGIN_NAMESPACE
49 #ifdef __FASTJET_USETR1SHAREDPTR 70 class SharedPtr :
public std::tr1::shared_ptr<T> {
72 SharedPtr() :
std::tr1::shared_ptr<T>() {}
73 SharedPtr(T * t) :
std::tr1::shared_ptr<T>(t) {}
74 SharedPtr(
const SharedPtr<T> & t) :
std::tr1::shared_ptr<T>(t) {}
76 #ifdef FASTJET_HAVE_EXPLICIT_FOR_OPERATORS 79 inline operator bool()
const {
return (this->
get()!=NULL);}
81 T* operator ()()
const{
87 #else // __FASTJET_USETR1SHAREDPTR 124 class __SharedCountingPtr;
132 _ptr =
new __SharedCountingPtr(ptr);
138 if (_ptr!=NULL) ++(*_ptr);
155 if (_ptr==NULL)
return;
170 template<
class Y>
void reset(Y * ptr){
191 if (_ptr == share._get_container())
return;
199 _ptr = share._get_container();
201 if (_ptr!=NULL) ++(*_ptr);
222 FASTJET_DEPRECATED_MSG(
"Use SharedPtr<T>::get() instead")
223 T* operator ()()
const{
224 if (_ptr==NULL)
return NULL;
235 return *(_ptr->get());
245 if (_ptr==NULL)
return NULL;
250 inline T*
get()
const{
251 if (_ptr==NULL)
return NULL;
257 return (use_count()==1);
262 if (_ptr==NULL)
return 0;
263 return _ptr->use_count();
268 #ifdef FASTJET_HAVE_EXPLICIT_FOR_OPERATORS 271 inline operator bool()
const{
272 return (
get()!=NULL);
277 __SharedCountingPtr* share_container = share._ptr;
279 _ptr = share_container;
285 if (_ptr==NULL)
return;
286 _ptr->set_count(count);
299 class __SharedCountingPtr{
302 __SharedCountingPtr() : _ptr(NULL), _count(0){}
305 template<
class Y>
explicit __SharedCountingPtr(Y* ptr) : _ptr(ptr), _count(1){}
308 ~__SharedCountingPtr(){
310 if (_ptr!=NULL){
delete _ptr;}
314 inline T*
get()
const {
return _ptr;}
317 inline long use_count()
const {
return _count;}
320 inline long operator++(){
return ++_count;}
323 inline long operator--(){
return --_count;}
328 inline long operator++(
int){
return _count++;}
333 inline long operator--(
int){
return _count--;}
337 void set_count(
const long & count){
348 inline __SharedCountingPtr* _get_container()
const{
362 void _decrease_count(){
367 if (_ptr->use_count()==0)
372 __SharedCountingPtr *_ptr;
377 template<
class T,
class U>
379 return t.get() == u.get();
383 template<
class T,
class U>
385 return t.get() != u.get();
389 template<
class T,
class U>
391 return t.
get() < u.get();
406 #endif // __FASTJET_USETR1SHAREDPTR 408 FASTJET_END_NAMESPACE
410 #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)