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