RefCounted.h
Go to the documentation of this file.
1 #ifndef MiscLib__REFCOUNTED_HEADER__
2 #define MiscLib__REFCOUNTED_HEADER__
3 #ifdef DOPARALLEL
4 #include <omp.h>
5 #endif
6 
7 namespace MiscLib
8 {
9  template <class T>
10  class RefCounted : public T
11  {
12  public:
13  RefCounted() : m_refCount(1)
14  {
15  }
16 
17  RefCounted(const RefCounted<T>& r) : T(r), m_refCount(1)
18  {
19  // do not copy the ref count!
20  }
21 
22  unsigned int
23  AddRef() const
24  {
25  //#pragma omp atomic
26  ++m_refCount;
27  return m_refCount;
28  }
29 
30  unsigned int
31  Release() const
32  {
33  if (m_refCount == 1)
34  {
35  //#pragma omp critical
36  {
37  if (m_refCount)
38  {
39  m_refCount = 0;
40  delete this;
41  }
42  }
43  return 0;
44  }
45  //#pragma omp atomic
46  --m_refCount;
47  return m_refCount;
48  }
49 
50  RefCounted&
52  {
53  *((T*)this) = r;
54  return *this; // do not copy the ref count!
55  }
56 
57  protected:
58  virtual ~RefCounted()
59  {
60  }
61 
62  private:
63  mutable unsigned int m_refCount;
64  };
65 }; // namespace MiscLib
66 
67 #endif
MiscLib::RefCounted::~RefCounted
virtual ~RefCounted()
Definition: RefCounted.h:58
MiscLib::RefCounted
Definition: RefCounted.h:10
MiscLib::RefCounted::RefCounted
RefCounted()
Definition: RefCounted.h:13
MiscLib::RefCounted::Release
unsigned int Release() const
Definition: RefCounted.h:31
MiscLib::RefCounted::operator=
RefCounted & operator=(const RefCounted &r)
Definition: RefCounted.h:51
MiscLib
Definition: AlignedAllocator.h:12
MiscLib::RefCounted::RefCounted
RefCounted(const RefCounted< T > &r)
Definition: RefCounted.h:17
MiscLib::RefCounted::AddRef
unsigned int AddRef() const
Definition: RefCounted.h:23
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38