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 SURFACE_REFERENCECOUNTED_H
2 #define SURFACE_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 // $Id: ReferenceCounted.h,v 1.12 2013/01/20 08:09:02 innocent Exp $
20 //
21 
22 // system include files
23 #include "boost/intrusive_ptr.hpp"
24 
25 // user include files
26 
27 // forward declarations
28 
30 {
31 
32  public:
35 
37  return *this;
38  }
40 
41  // ---------- const member functions ---------------------
42 
43  void addReference() const { ++referenceCount_ ; }
44  void removeReference() const { if( 0 == --referenceCount_ ) {
45  delete const_cast<BasicReferenceCounted*>(this);
46  }
47  }
48 
49  unsigned int references() const {return referenceCount_;}
50 
51  // ---------- static member functions --------------------
52 
53  // ---------- member functions ---------------------------
54 
55  private:
56 
57  // ---------- member data --------------------------------
58  mutable unsigned int referenceCount_;
59 };
60 
61 template <class T> class ReferenceCountingPointer :
62  public boost::intrusive_ptr<T>
63 {
64  public:
65  ReferenceCountingPointer(T* iT) : boost::intrusive_ptr<T>(iT) {}
67 };
68 
69 template <class T> class ConstReferenceCountingPointer :
70  public boost::intrusive_ptr<const T>
71 {
72  public:
73  ConstReferenceCountingPointer(const T* iT) : boost::intrusive_ptr<const T>(iT) {}
76  boost::intrusive_ptr<const T>(&(*other)) {}
77 };
78 
79 inline void intrusive_ptr_add_ref( const BasicReferenceCounted* iRef ) {
80  iRef->addReference();
81 }
82 
83 inline void intrusive_ptr_release( const BasicReferenceCounted* iRef ) {
84  iRef->removeReference();
85 }
86 
87 
88 // does not increase ref count
89 // delete if count is 0 (takes owership of orphans )
90 // clone if count is 0
91 // double delete if deleted after the real owner...
92 template<typename T>
94  explicit MixedReference(T const * ip=nullptr) noexcept : p(ip){}
96 
97 #ifndef CMS_NOCXX11
98  MixedReference( MixedReference&& rh) noexcept : p(rh.p){rh.p=0;}
100  destroy();
101  p=rh.p; rh.p=0;
102  return *this;
103 }
104 #endif
105 
106  MixedReference( MixedReference const & rh) : p(rh.p? rh.p->clone(): nullptr ){}
108  if (rh.p!=p) {
109  destroy();
110  p = rh.p? rh.p->clone(): nullptr;
111  }
112  return * this;
113  }
114 
115 
116  T const * get() const {return p;}
117  T const & operator*() const { return *p;}
118 
119 private:
120  void destroy() noexcept {
121  if (p && p->references()==0) { delete const_cast<T*>(p); }
122  }
123  T const * p;
124 };
125 
126 
127 #define CMSSW_POOLALLOCATOR
128 
129 #ifdef CMSSW_POOLALLOCATOR
131 #else
132 template<typename T>
133 struct LocalCache {
134  std::auto_ptr<T> ptr;
135 };
136 
137 #endif
138 
140 #ifdef CMSSW_POOLALLOCATOR
141  : public BlockWipedPoolAllocated
142 #endif
143 {
144 
145 public:
146  static int s_alive;
147  static int s_referenced;
148 
150  s_alive++;
151  }
152 
154  s_alive++;
155  }
156 
158  return *this;
159  }
160 
162  s_alive--;
163  }
164 
165  // ---------- const member functions ---------------------
166 
167  void addReference() const { ++referenceCount_ ; s_referenced++; }
168  void removeReference() const {
169  s_referenced--;
170  if( 0 == --referenceCount_ ) {
171  delete const_cast<ReferenceCountedPoolAllocated*>(this);
172  }
173  }
174 
175  unsigned int references() const {return referenceCount_;}
176 
177  // ---------- static member functions --------------------
178 
179  // ---------- member functions ---------------------------
180 
181  private:
182 
183  // ---------- member data --------------------------------
184  mutable unsigned int referenceCount_;
185 };
186 
188  iRef->addReference();
189 }
190 
192  iRef->removeReference();
193 }
194 
195 // condition uses naive RefCount
197 
198 
199 // transient objects in algo and events are "poo allocated"
201 
202 // just to avoid changing all around
203 // typedef ReferenceCountedPoolAllocated ReferenceCounted;
205 
206 
207 
208 #endif /* SURFACE_REFERENCECOUNTED_H */
MixedReference(MixedReference const &rh)
ReferenceCountedPoolAllocated ReferenceCountedInEvent
void intrusive_ptr_add_ref(const BasicReferenceCounted *iRef)
#define CMSSW_POOLALLOCATOR
BasicReferenceCounted(const BasicReferenceCounted &iRHS)
T const & operator*() const
#define nullptr
const BasicReferenceCounted & operator=(const BasicReferenceCounted &)
BasicReferenceCounted ReferenceCounted
void destroy() noexcept
MixedReference(T const *ip=nullptr) noexcept
BasicReferenceCounted ReferenceCountedInConditions
unsigned int references() const
ReferenceCountedPoolAllocated(const ReferenceCountedPoolAllocated &iRHS)
const ReferenceCountedPoolAllocated & operator=(const ReferenceCountedPoolAllocated &)
void removeReference() const
ConstReferenceCountingPointer(const ReferenceCountingPointer< T > &other)
~MixedReference() noexcept
#define noexcept
string const
Definition: compareJSON.py:14
unsigned int references() const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
void intrusive_ptr_release(const BasicReferenceCounted *iRef)
void addReference() const
unsigned int referenceCount_
virtual ~BasicReferenceCounted()
MixedReference(MixedReference &&rh) noexcept
ConstReferenceCountingPointer(const T *iT)
long double T
MixedReference & operator=(MixedReference const &rh)
std::auto_ptr< T > ptr
MixedReference & operator=(MixedReference &&rh) noexcept