Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:14

0001 #ifndef DataFormats_GeometrySurface_ReferenceCounted_h
0002 #define DataFormats_GeometrySurface_ReferenceCounted_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Surface
0006 // Class  :     ReferenceCounted
0007 //
0008 /**\class ReferenceCounted ReferenceCounted.h DataFormats/GeometrySurface/interface/ReferenceCounted.h
0009 
0010  Description: <one line class summary>
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Fri Jul 15 09:17:20 EDT 2005
0019 //
0020 
0021 // system include files
0022 #include "boost/intrusive_ptr.hpp"
0023 #include <atomic>
0024 
0025 // user include files
0026 
0027 // forward declarations
0028 
0029 class BasicReferenceCounted {
0030 public:
0031   BasicReferenceCounted() : referenceCount_(0) {}
0032   BasicReferenceCounted(const BasicReferenceCounted& /* iRHS */) : 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   // ---------- const member functions ---------------------
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   // ---------- static member functions --------------------
0051 
0052   // ---------- member functions ---------------------------
0053 
0054 private:
0055   // ---------- member data --------------------------------
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 // condition uses naive RefCount
0079 typedef BasicReferenceCounted ReferenceCountedInConditions;
0080 
0081 typedef BasicReferenceCounted ReferenceCountedInEvent;
0082 
0083 typedef BasicReferenceCounted ReferenceCounted;
0084 
0085 #endif