CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ReferenceCounted.h
Go to the documentation of this file.
1 #ifndef DataFormats_GeometrySurface_ReferenceCounted_h
2 #define DataFormats_GeometrySurface_ReferenceCounted_h
3 // -*- C++ -*-
4 //
5 // Package: Surface
6 // Class : ReferenceCounted
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Fri Jul 15 09:17:20 EDT 2005
19 //
20 
21 // system include files
22 #include "boost/intrusive_ptr.hpp"
23 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
24 #include <atomic>
25 #endif
26 
27 // user include files
28 
29 // forward declarations
30 
32 {
33 
34  public:
37 
39  return *this;
40  }
42 
43  // ---------- const member functions ---------------------
44 
45 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
46  void addReference() const { referenceCount_.fetch_add(1,std::memory_order_acq_rel) ; }
47  void removeReference() const { if( 1 == referenceCount_.fetch_sub(1,std::memory_order_acq_rel ) ) {
48  delete const_cast<BasicReferenceCounted*>(this);
49  }
50  }
51 
52  unsigned int references() const {return referenceCount_.load(std::memory_order_acquire);}
53 #else
54  void addReference() const;
55  void removeReference() const;
56 
57  unsigned int references() const;
58 #endif
59  // ---------- static member functions --------------------
60 
61  // ---------- member functions ---------------------------
62 
63  private:
64 
65  // ---------- member data --------------------------------
66 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
67  mutable std::atomic<unsigned int> referenceCount_;
68 #else
69  unsigned int referenceCount_;
70 #endif
71 };
72 
73 template <class T> class ReferenceCountingPointer :
74  public boost::intrusive_ptr<T>
75 {
76  public:
77  ReferenceCountingPointer(T* iT) : boost::intrusive_ptr<T>(iT) {}
79 };
80 
81 template <class T> class ConstReferenceCountingPointer :
82  public boost::intrusive_ptr<const T>
83 {
84  public:
85  ConstReferenceCountingPointer(const T* iT) : boost::intrusive_ptr<const T>(iT) {}
88  boost::intrusive_ptr<const T>(&(*other)) {}
89 };
90 
91 inline void intrusive_ptr_add_ref( const BasicReferenceCounted* iRef ) {
92  iRef->addReference();
93 }
94 
95 inline void intrusive_ptr_release( const BasicReferenceCounted* iRef ) {
96  iRef->removeReference();
97 }
98 
99 // condition uses naive RefCount
101 
102 
104 
106 
107 #endif
void intrusive_ptr_add_ref(const BasicReferenceCounted *iRef)
BasicReferenceCounted(const BasicReferenceCounted &)
const BasicReferenceCounted & operator=(const BasicReferenceCounted &)
BasicReferenceCounted ReferenceCounted
BasicReferenceCounted ReferenceCountedInConditions
void removeReference() const
std::atomic< unsigned int > referenceCount_
ConstReferenceCountingPointer(const ReferenceCountingPointer< T > &other)
BasicReferenceCounted ReferenceCountedInEvent
string const
Definition: compareJSON.py:14
unsigned int references() const
void intrusive_ptr_release(const BasicReferenceCounted *iRef)
void addReference() const
virtual ~BasicReferenceCounted()
ConstReferenceCountingPointer(const T *iT)
long double T