Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
refcountobject.h
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 #ifndef _REFCOUNTOBJECT_H
19 #define _REFCOUNTOBJECT_H
20 
21 
22 
23 
24 
25 
27 {
28 public:
29  virtual int AddRef() = 0;
30  virtual int Release() = 0;
31 };
32 
33 
35 {
36 protected:
37  int m_nRefs;
38 
39 public:
41  virtual ~CBasicRefCount();
42 
43  int InternalAddRef();
44  int InternalRelease();
45 
46  virtual void OnFinalRelease();
47 };
48 
49 #define ADDREF_AND_RELEASE_IMPL() \
50  inline int AddRef() {return InternalAddRef();} \
51  inline int Release() {return InternalRelease();}
52 
53 
54 
55 
56 template <class T>
58 {
59 protected:
60  T* m_ptr;
61 
62 public:
63 
64 
65  CRefCountedPtr() : m_ptr(NULL)
66  {
67  ;
68  }
69 
70  CRefCountedPtr(T* ptr) : m_ptr(ptr)
71  {
72  if (m_ptr)
73  {
74  m_ptr->AddRef();
75  }
76  }
77 
78  CRefCountedPtr(const CRefCountedPtr<T>& sp) : m_ptr(sp.m_ptr)
79  {
80  if (m_ptr)
81  {
82  m_ptr->AddRef();
83  }
84  }
85 
87  {
88  if (m_ptr)
89  {
90  m_ptr->Release();
91  m_ptr = NULL;
92  }
93  }
94 
96  {
97  return m_ptr;
98  }
99 
100  operator T*() const
101  {
102  return m_ptr;
103  }
104 
105  // std::vector chokes because it references &element to infer behavior
106  // Use GetPointerPointer instead
107  //T** operator&()
108  //{
109  // return &m_ptr;
110  //}
111 
113  {
114  return &m_ptr;
115  }
116 
117 
118  T* operator->() const
119  {
120  return m_ptr;
121  }
122 
123  T* operator = (T* ptr)
124  {
125  if (ptr)
126  {
127  ptr->AddRef();
128  }
129  if (m_ptr)
130  {
131  m_ptr->Release();
132  }
133  m_ptr = ptr;
134  return m_ptr;
135  }
136 
137  T* operator = (const CRefCountedPtr<T>& sp)
138  {
139  if (sp.m_ptr)
140  {
141  sp.m_ptr->AddRef();
142  }
143  if (m_ptr)
144  {
145  m_ptr->Release();
146  }
147  m_ptr = sp.m_ptr;
148  return m_ptr;
149  }
150 
151  bool operator ! () const
152  {
153  return (m_ptr == NULL);
154  }
155 
156  bool operator != (T* ptr) const
157  {
158  return (ptr != m_ptr);
159  }
160 
161  bool operator == (T* ptr) const
162  {
163  return (ptr == m_ptr);
164  }
165 
166  // manual release
168  {
169  if (m_ptr)
170  {
171  m_ptr->Release();
172  m_ptr = NULL;
173  }
174  }
175 
176  void Attach(T* ptr)
177  {
178  if (m_ptr)
179  {
180  m_ptr->Release();
181  }
182  m_ptr = ptr;
183  }
184 
185  T* Detach()
186  {
187  T* ptr = m_ptr;
188  m_ptr = NULL;
189  return ptr;
190  }
191 
192  HRESULT CopyTo(T** ppT)
193  {
194  if (ppT == NULL)
195  {
196  return E_POINTER;
197  }
198 
199  *ppT = m_ptr;
200  if (m_ptr)
201  {
202  m_ptr->AddRef();
203  }
204  return S_OK;
205  }
206 
207 };
208 
209 
210 
211 #endif /* _REFCOUNTOBJECT_H */
212 
#define S_OK
Definition: hresult.h:46
T * operator->() const
virtual int Release()=0
HRESULT CopyTo(T **ppT)
bool operator==(const StunTransactionId &id1, const StunTransactionId &id2)
Definition: stuntypes.h:157
T ** GetPointerPointer()
void ReleaseAndClear()
int32_t HRESULT
Definition: hresult.h:22
#define E_POINTER
Definition: hresult.h:53
virtual int AddRef()=0
void Attach(T *ptr)
CRefCountedPtr(T *ptr)
CRefCountedPtr(const CRefCountedPtr< T > &sp)