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