BasicReferenceCounted

ConstReferenceCountingPointer

ReferenceCountingPointer

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#ifndef DataFormats_GeometrySurface_ReferenceCounted_h
#define DataFormats_GeometrySurface_ReferenceCounted_h
// -*- C++ -*-
//
// Package:     Surface
// Class  :     ReferenceCounted
//
/**\class ReferenceCounted ReferenceCounted.h DataFormats/GeometrySurface/interface/ReferenceCounted.h

 Description: <one line class summary>

 Usage:
    <usage>

*/
//
// Original Author:  Chris Jones
//         Created:  Fri Jul 15 09:17:20 EDT 2005
//

// system include files
#include "boost/intrusive_ptr.hpp"
#include <atomic>

// user include files

// forward declarations

class BasicReferenceCounted {
public:
  BasicReferenceCounted() : referenceCount_(0) {}
  BasicReferenceCounted(const BasicReferenceCounted& /* iRHS */) : referenceCount_(0) {}
  BasicReferenceCounted(BasicReferenceCounted&&) : referenceCount_(0) {}
  BasicReferenceCounted& operator=(BasicReferenceCounted&&) { return *this; }

  BasicReferenceCounted& operator=(const BasicReferenceCounted&) { return *this; }

  virtual ~BasicReferenceCounted() {}

  // ---------- const member functions ---------------------

  void addReference() const { referenceCount_++; }
  void removeReference() const {
    if (1 == referenceCount_--) {
      delete const_cast<BasicReferenceCounted*>(this);
    }
  }

  unsigned int references() const { return referenceCount_; }
  // ---------- static member functions --------------------

  // ---------- member functions ---------------------------

private:
  // ---------- member data --------------------------------
  mutable std::atomic<unsigned int> referenceCount_;
};

template <class T>
class ReferenceCountingPointer : public boost::intrusive_ptr<T> {
public:
  ReferenceCountingPointer(T* iT) : boost::intrusive_ptr<T>(iT) {}
  ReferenceCountingPointer() {}
};

template <class T>
class ConstReferenceCountingPointer : public boost::intrusive_ptr<const T> {
public:
  ConstReferenceCountingPointer(const T* iT) : boost::intrusive_ptr<const T>(iT) {}
  ConstReferenceCountingPointer() {}
  ConstReferenceCountingPointer(const ReferenceCountingPointer<T>& other) : boost::intrusive_ptr<const T>(&(*other)) {}
};

inline void intrusive_ptr_add_ref(const BasicReferenceCounted* iRef) { iRef->addReference(); }

inline void intrusive_ptr_release(const BasicReferenceCounted* iRef) { iRef->removeReference(); }

// condition uses naive RefCount
typedef BasicReferenceCounted ReferenceCountedInConditions;

typedef BasicReferenceCounted ReferenceCountedInEvent;

typedef BasicReferenceCounted ReferenceCounted;

#endif