RefCountPtr.h
Go to the documentation of this file.
1 #ifndef MiscLib__REFCOUNTPTR_HEADER__
2 #define MiscLib__REFCOUNTPTR_HEADER__
3 
4 namespace MiscLib
5 {
6  template< class T >
7  class RefCountPtr
8  {
9  public:
11  : m_ptr(0)
12  {}
13 
14  template< class P >
15  RefCountPtr(P* ptr)
16  : m_ptr(ptr)
17  {
18  if (m_ptr)
19  {
20  m_ptr->AddRef();
21  }
22  }
23 
25  : m_ptr(ptr.m_ptr)
26  {
27  if (m_ptr)
28  {
29  m_ptr->AddRef();
30  }
31  }
32 
33  template< class P >
35  : m_ptr(ptr.m_ptr)
36  {
37  if (m_ptr)
38  {
39  m_ptr->AddRef();
40  }
41  }
42 
44  {
45  if (m_ptr)
46  {
47  m_ptr->Release();
48  }
49  }
50 
51  void Release()
52  {
53  if (m_ptr)
54  {
55  m_ptr->Release();
56  m_ptr = 0;
57  }
58  }
59 
60  template< class N >
61  T* New()
62  {
63  if (m_ptr)
64  {
65  m_ptr->Release();
66  }
67  m_ptr = new N();
68  return m_ptr;
69  }
70 
72  {
73  if (m_ptr == ptr.m_ptr)
74  {
75  return *this;
76  }
77  if (m_ptr)
78  {
79  m_ptr->Release();
80  }
81  m_ptr = ptr.m_ptr;
82  if (m_ptr)
83  {
84  m_ptr->AddRef();
85  }
86  return *this;
87  }
88 
89  template< class P >
91  {
92  if (m_ptr == ptr.m_ptr)
93  {
94  return *this;
95  }
96  if (m_ptr)
97  {
98  m_ptr->Release();
99  }
100  m_ptr = ptr.m_ptr;
101  if (m_ptr)
102  {
103  m_ptr->AddRef();
104  }
105  return *this;
106  }
107 
108  template< class P >
110  {
111  if (m_ptr == ptr)
112  {
113  return *this;
114  }
115  if (m_ptr)
116  {
117  m_ptr->Release();
118  }
119  m_ptr = ptr;
120  if (m_ptr)
121  {
122  m_ptr->AddRef();
123  }
124  return *this;
125  }
126 
128  {
129  return m_ptr;
130  }
131 
132  const T* operator->() const
133  {
134  return m_ptr;
135  }
136 
138  {
139  return *m_ptr;
140  }
141 
142  const T& operator*() const
143  {
144  return *m_ptr;
145  }
146 
147  operator T* ()
148  {
149  return m_ptr;
150  }
151 
152  operator const T* () const
153  {
154  return m_ptr;
155  }
156 
157  T* Ptr()
158  {
159  return m_ptr;
160  }
161 
162  const T* Ptr() const
163  {
164  return m_ptr;
165  }
166 
167  template< class P >
168  bool operator==(const RefCountPtr< P >& ptr) const
169  {
170  return m_ptr == ptr.m_ptr;
171  }
172 
173  template< class P >
174  bool operator==(P* ptr) const
175  {
176  return m_ptr == ptr;
177  }
178 
179  private:
180  T* m_ptr;
181  };
182 };
183 
184 #endif
MiscLib::RefCountPtr::operator->
T * operator->()
Definition: RefCountPtr.h:127
MiscLib::RefCountPtr::RefCountPtr
RefCountPtr(P *ptr)
Definition: RefCountPtr.h:15
MiscLib::RefCountPtr::Ptr
T * Ptr()
Definition: RefCountPtr.h:157
MiscLib::RefCountPtr::operator*
T & operator*()
Definition: RefCountPtr.h:137
MiscLib::RefCountPtr::RefCountPtr
RefCountPtr()
Definition: RefCountPtr.h:10
MiscLib::RefCountPtr::Ptr
const T * Ptr() const
Definition: RefCountPtr.h:162
MiscLib::RefCountPtr::operator=
RefCountPtr< T > & operator=(const RefCountPtr< P > &ptr)
Definition: RefCountPtr.h:90
MiscLib::RefCountPtr::RefCountPtr
RefCountPtr(const RefCountPtr< P > &ptr)
Definition: RefCountPtr.h:34
MiscLib::RefCountPtr::New
T * New()
Definition: RefCountPtr.h:61
MiscLib
Definition: AlignedAllocator.h:11
MiscLib::RefCountPtr::operator==
bool operator==(P *ptr) const
Definition: RefCountPtr.h:174
MiscLib::RefCountPtr::operator==
bool operator==(const RefCountPtr< P > &ptr) const
Definition: RefCountPtr.h:168
MiscLib::RefCountPtr::~RefCountPtr
~RefCountPtr()
Definition: RefCountPtr.h:43
MiscLib::RefCountPtr
Definition: RefCountPtr.h:7
MiscLib::RefCountPtr::operator->
const T * operator->() const
Definition: RefCountPtr.h:132
MiscLib::RefCountPtr::operator=
RefCountPtr< T > & operator=(const RefCountPtr< T > &ptr)
Definition: RefCountPtr.h:71
MiscLib::RefCountPtr::operator*
const T & operator*() const
Definition: RefCountPtr.h:142
MiscLib::RefCountPtr::operator=
RefCountPtr< T > & operator=(P *ptr)
Definition: RefCountPtr.h:109
MiscLib::RefCountPtr::Release
void Release()
Definition: RefCountPtr.h:51
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
MiscLib::RefCountPtr::RefCountPtr
RefCountPtr(const RefCountPtr< T > &ptr)
Definition: RefCountPtr.h:24