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
7namespace 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
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
RefCounted & operator=(const RefCounted &r)
Definition RefCounted.h:51
unsigned int Release() const
Definition RefCounted.h:31
RefCounted(const RefCounted< T > &r)
Definition RefCounted.h:17
unsigned int AddRef() const
Definition RefCounted.h:23
virtual ~RefCounted()
Definition RefCounted.h:58