File indexing completed on 2023-03-17 10:49:57
0001 #ifndef DataFormats_GeometrySurface_ReferenceCounted_h
0002 #define DataFormats_GeometrySurface_ReferenceCounted_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include "boost/intrusive_ptr.hpp"
0023 #include <atomic>
0024
0025
0026
0027
0028
0029 class BasicReferenceCounted {
0030 public:
0031 BasicReferenceCounted() : referenceCount_(0) {}
0032 BasicReferenceCounted(const BasicReferenceCounted& ) : referenceCount_(0) {}
0033 BasicReferenceCounted(BasicReferenceCounted&&) : referenceCount_(0) {}
0034 BasicReferenceCounted& operator=(BasicReferenceCounted&&) { return *this; }
0035
0036 BasicReferenceCounted& operator=(const BasicReferenceCounted&) { return *this; }
0037
0038 virtual ~BasicReferenceCounted() {}
0039
0040
0041
0042 void addReference() const { referenceCount_++; }
0043 void removeReference() const {
0044 if (1 == referenceCount_--) {
0045 delete const_cast<BasicReferenceCounted*>(this);
0046 }
0047 }
0048
0049 unsigned int references() const { return referenceCount_; }
0050
0051
0052
0053
0054 private:
0055
0056 mutable std::atomic<unsigned int> referenceCount_;
0057 };
0058
0059 template <class T>
0060 class ReferenceCountingPointer : public boost::intrusive_ptr<T> {
0061 public:
0062 ReferenceCountingPointer(T* iT) : boost::intrusive_ptr<T>(iT) {}
0063 ReferenceCountingPointer() {}
0064 };
0065
0066 template <class T>
0067 class ConstReferenceCountingPointer : public boost::intrusive_ptr<const T> {
0068 public:
0069 ConstReferenceCountingPointer(const T* iT) : boost::intrusive_ptr<const T>(iT) {}
0070 ConstReferenceCountingPointer() {}
0071 ConstReferenceCountingPointer(const ReferenceCountingPointer<T>& other) : boost::intrusive_ptr<const T>(&(*other)) {}
0072 };
0073
0074 inline void intrusive_ptr_add_ref(const BasicReferenceCounted* iRef) { iRef->addReference(); }
0075
0076 inline void intrusive_ptr_release(const BasicReferenceCounted* iRef) { iRef->removeReference(); }
0077
0078
0079 typedef BasicReferenceCounted ReferenceCountedInConditions;
0080
0081 typedef BasicReferenceCounted ReferenceCountedInEvent;
0082
0083 typedef BasicReferenceCounted ReferenceCounted;
0084
0085 #endif