1  #ifndef __FASTJET_SHARED_PTR_HH__ 
    2 #define __FASTJET_SHARED_PTR_HH__ 
   34 #include "fastjet/internal/base.hh" 
   35 #include "fastjet/config.h" 
   37 #include "fastjet/internal/deprecated.hh" 
   39 #ifdef FASTJET_HAVE_THREAD_SAFETY 
   43 #endif // FASTJET_HAVE_THREAD_SAFETY 
   45 FASTJET_BEGIN_NAMESPACE      
 
   83 #ifdef FASTJET_HAVE_THREAD_SAFETY 
   88   class __SharedCountingPtr;
 
   91   SharedPtr() : _ptr(NULL){}
 
   95   template<
class Y> 
explicit SharedPtr(Y* ptr){
 
   96     _ptr = 
new __SharedCountingPtr(ptr);
 
  101   SharedPtr(SharedPtr 
const & share) : _ptr(share._get_container()){
 
  104     if (_ptr!=NULL) (*_ptr)++;
 
  110     if (_ptr==NULL) 
return;
 
  117     SharedPtr().swap(*
this);
 
  122   template<
class Y> 
void reset(Y * ptr){
 
  123     SharedPtr(ptr).swap(*
this);
 
  129   template<
class Y> 
void reset(SharedPtr<Y> 
const & share){
 
  137       if (_ptr == share._get_container()) 
return;
 
  145     _ptr = share._get_container();  
 
  147     if (_ptr!=NULL) (*_ptr)++;
 
  152   SharedPtr& operator=(SharedPtr 
const & share){
 
  159   template<
class Y> SharedPtr& operator=(SharedPtr<Y> 
const & share){
 
  171     return *(_ptr->get());
 
  180   inline T* operator->()
 const{
 
  181     if (_ptr==NULL) 
return NULL;
 
  186   inline T* get()
 const{
 
  187     if (_ptr==NULL) 
return NULL;
 
  195   T* operator ()()
 const{
 
  196     if (_ptr==NULL) 
return NULL;
 
  201   inline bool unique()
 const{
 
  203     return (use_count()==1);
 
  207   inline long use_count()
 const{
 
  208     if (_ptr==NULL) 
return 0;
 
  209     return _ptr->use_count(); 
 
  214   inline operator bool()
 const{
 
  215     return (get()!=NULL);
 
  219   inline void swap(SharedPtr & share){
 
  220     __SharedCountingPtr* share_container = share._ptr;
 
  222     _ptr = share_container;
 
  227   void set_count(
const long & count){
 
  228     if (_ptr==NULL) 
return;
 
  229     _ptr->set_count(count);
 
  242   class __SharedCountingPtr : 
public std::atomic<long>{
 
  245     __SharedCountingPtr() : std::atomic<long>(0), _ptr(NULL){} 
 
  248     template<
class Y> 
explicit __SharedCountingPtr(Y* ptr)
 
  249       : std::atomic<long>(1), _ptr(ptr){}
 
  252     ~__SharedCountingPtr(){ 
 
  254       if (_ptr!=NULL){ 
delete _ptr;}
 
  258     inline T* get()
 const {
return _ptr;}
 
  261     inline long use_count()
 const {
return (
long)(*this);}
 
  265     inline void set_count(
const long & count){ store(count);}
 
  273   inline __SharedCountingPtr* _get_container()
 const{
 
  287   void _decrease_count(){
 
  297     if (((*_ptr)--) == 1)
 
  302   __SharedCountingPtr *_ptr;
 
  307 template<
class T,
class U>
 
  308 inline bool operator==(SharedPtr<T> 
const & t, SharedPtr<U> 
const & u){
 
  309   return t.get() == u.get();
 
  313 template<
class T,
class U>
 
  314 inline bool operator!=(SharedPtr<T> 
const & t, SharedPtr<U> 
const & u){
 
  315   return t.get() != u.get();
 
  319 template<
class T,
class U>
 
  320 inline bool operator<(SharedPtr<T> 
const & t, SharedPtr<U> 
const & u){
 
  321   return t.get() < u.get();
 
  326 inline void swap(SharedPtr<T> & a, SharedPtr<T> & b){
 
  338 #else  // FASTJET_HAVE_THREAD_SAFETY 
  344   class __SharedCountingPtr;
 
  352     _ptr = 
new __SharedCountingPtr(ptr);
 
  358     if (_ptr!=NULL) ++(*_ptr);
 
  375     if (_ptr==NULL) 
return;
 
  390   template<
class Y> 
void reset(Y * ptr){
 
  411       if (_ptr == share._get_container()) 
return;
 
  419     _ptr = share._get_container();  
 
  421     if (_ptr!=NULL) ++(*_ptr);
 
  445   FASTJET_DEPRECATED_MSG(
"Use SharedPtr<T>::get() instead",
 
  446   T* 
operator ()() 
const){
 
  447     if (_ptr==NULL) 
return NULL;
 
  458     return *(_ptr->get());
 
  468     if (_ptr==NULL) 
return NULL;
 
  474     if (_ptr==NULL) 
return NULL;
 
  480     return (use_count()==1);
 
  485     if (_ptr==NULL) 
return 0;
 
  486     return _ptr->use_count(); 
 
  491   #ifdef FASTJET_HAVE_EXPLICIT_FOR_OPERATORS 
  494   inline operator bool()
 const{
 
  495     return (get()!=NULL);
 
  500     __SharedCountingPtr* share_container = share._ptr;
 
  502     _ptr = share_container;
 
  508     if (_ptr==NULL) 
return;
 
  509     _ptr->set_count(count);
 
  522   class __SharedCountingPtr{
 
  525     __SharedCountingPtr() : _ptr(NULL), _count(0){}
 
  528     template<
class Y> 
explicit __SharedCountingPtr(Y* ptr) : _ptr(ptr), _count(1){}
 
  531     ~__SharedCountingPtr(){ 
 
  533       if (_ptr!=NULL){ 
delete _ptr;}
 
  537     inline T* get()
 const {
return _ptr;}
 
  540     inline long use_count()
 const {
return _count;}
 
  543     inline long operator++(){
return ++_count;}
 
  546     inline long operator--(){
return --_count;}
 
  551     inline long operator++(
int){
return _count++;}
 
  556     inline long operator--(
int){
return _count--;}
 
  560     void set_count(
const long & count){
 
  571   inline __SharedCountingPtr* _get_container()
 const{
 
  585   void _decrease_count(){
 
  590     if (_ptr->use_count()==0)
 
  595   __SharedCountingPtr *_ptr;
 
  600 template<
class T,
class U>
 
  602   return t.get() == u.get();
 
  606 template<
class T,
class U>
 
  608   return t.get() != u.get();
 
  612 template<
class T,
class U>
 
  614   return t.get() < u.get();
 
  629 #endif // FASTJET_HAVE_THREAD_SAFETY 
  631 FASTJET_END_NAMESPACE      
 
  633 #endif   // __FASTJET_SHARED_PTR_HH__