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
7namespace 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
25 RefCount::RefCount() : m_refCount(1)
26 {
27 }
28
29 RefCount::RefCount(const RefCount&) : m_refCount(1)
30 {
31 // do not copy the ref count!
32 }
33
34 unsigned int
36 {
37 //#pragma omp atomic
38 ++m_refCount;
39 return m_refCount;
40 }
41
42 unsigned int
44 {
45 if (m_refCount == 1)
46 {
47 //#pragma omp critical
48 {
49 if (m_refCount)
50 {
51 m_refCount = 0;
52 delete this;
53 }
54 }
55 return 0;
56 }
57 //#pragma omp atomic
58 --m_refCount;
59 return m_refCount;
60 }
61
64 {
65 // do not copy the ref count!!!
66 return *this;
67 }
68}; // namespace MiscLib
69
70#endif
RefCount & operator=(const RefCount &)
Definition RefCount.h:63
virtual ~RefCount()
Definition RefCount.cpp:8
unsigned int Release() const
Definition RefCount.h:43
unsigned int AddRef() const
Definition RefCount.h:35