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