OpenDDS  Snapshot(2023/04/28-20:55)
RcHandle_T.h
Go to the documentation of this file.
1 /*
2  * Distributed under the OpenDDS License.
3  * See: http://www.opendds.org/license.html
4  */
5 
6 #ifndef OPENDDS_DCPS_RCHANDLE_T_H
7 #define OPENDDS_DCPS_RCHANDLE_T_H
8 
10 #include "Definitions.h"
11 #include "unique_ptr.h"
12 
14 
15 namespace OpenDDS {
16 namespace DCPS {
17 
18 struct inc_count {};
19 struct keep_count {};
20 
21 /// Templated Reference counted handle to a pointer.
22 /// A non-DDS specific helper class.
23 template <typename T>
24 class RcHandle {
25 public:
27  : ptr_(0)
28  {
29  }
30 
32  : ptr_(p)
33  {
34  }
35 
36  template <typename U>
38  : ptr_(p.release())
39  {
40  }
41 
43  : ptr_(p)
44  {
45  this->bump_up();
46  }
47 
48  template <typename U>
49  RcHandle(const RcHandle<U>& other)
50  : ptr_(other.in())
51  {
52  this->bump_up();
53  }
54 
55  RcHandle(const RcHandle& b)
56  : ptr_(b.ptr_)
57  {
58  this->bump_up();
59  }
60 
62  {
63  this->bump_down();
64  }
65 
66  void reset()
67  {
68  RcHandle tmp;
69  swap(tmp);
70  }
71 
72  template <typename U>
73  void reset(T* p, U counting_strategy)
74  {
75  RcHandle tmp(p, counting_strategy);
76  swap(tmp);
77  }
78 
80  {
81  RcHandle tmp(b);
82  swap(tmp);
83  return *this;
84  }
85 
86  template <class U>
88  {
89  RcHandle<T> tmp(b);
90  swap(tmp);
91  return *this;
92  }
93 
94  template <typename U>
96  {
97  RcHandle<T> tmp(b.release(), keep_count());
98  swap(tmp);
99  return *this;
100  }
101 
102  void swap(RcHandle& rhs)
103  {
104  T* t = this->ptr_;
105  this->ptr_ = rhs.ptr_;
106  rhs.ptr_ = t;
107  }
108 
109  T* operator->() const
110  {
111  OPENDDS_ASSERT(ptr_);
112  return this->ptr_;
113  }
114 
115  T& operator*() const
116  {
117  OPENDDS_ASSERT(ptr_);
118  return *this->ptr_;
119  }
120 
121  bool is_nil() const
122  {
123  return this->ptr_ == 0;
124  }
125 
126  T* in() const
127  {
128  return this->ptr_;
129  }
130 
131  T* get() const
132  {
133  return this->ptr_;
134  }
135 
136  T*& inout()
137  {
138  return this->ptr_;
139  }
140 
141  T*& out()
142  {
143  this->bump_down();
144  return this->ptr_;
145  }
146 
147  T* _retn()
148  {
149  T* retval = this->ptr_;
150  this->ptr_ = 0;
151  return retval;
152  }
153 
154  operator bool() const
155  {
156  return in() != 0;
157  }
158 
159  bool operator==(const RcHandle& rhs) const
160  {
161  return in() == rhs.in();
162  }
163 
164  bool operator!=(const RcHandle& rhs) const
165  {
166  return in() != rhs.in();
167  }
168 
169  bool operator < (const RcHandle& rhs) const
170  {
171  return in() < rhs.in();
172  }
173 
174 private:
175  void bump_up()
176  {
177  if (ptr_ != 0) {
178  ptr_->_add_ref();
179  }
180  }
181 
182  void bump_down()
183  {
184  if (ptr_ != 0) {
185  ptr_->_remove_ref();
186  ptr_ = 0;
187  }
188  }
189 
190  /// The actual "unsmart" pointer to the T object.
191  T* ptr_;
192 };
193 
194 
195 template <typename T>
196 void swap(RcHandle<T>& lhs, RcHandle<T>& rhs)
197 {
198  lhs.swap(rhs);
199 }
200 
201 template <typename T, typename U>
203 {
204  return RcHandle<T>(static_cast<T*>(h.in()), inc_count());
205 }
206 
207 template <typename T, typename U>
209 {
210  return RcHandle<T>(const_cast<T*>(h.in()), inc_count());
211 }
212 
213 template <typename T, typename U>
215 {
216  return RcHandle<T>(dynamic_cast<T*>(h.in()), inc_count());
217 }
218 
219 
220 template< class T >
222 public:
223  // types
224  typedef T type;
225 
226  // construct/copy/destroy
227  reference_wrapper(T& ref): _ptr(&ref) {}
228  // access
229  operator T& () const { return *_ptr; }
230  T& get() const { return *_ptr; }
231 
232 private:
233  T* _ptr;
234 };
235 
236 template <typename T>
238 {
239  return reference_wrapper<T>(r);
240 }
241 
242 template <typename T>
243 T const& unwrap_reference(T const& t)
244 {
245  return t;
246 }
247 
248 template <typename T>
250 {
251  return t.get();
252 }
253 
254 
255 template <typename T>
257 {
258  return RcHandle<T>(new T(), keep_count());
259 }
260 
261 template <typename T, typename U>
263 {
264  return RcHandle<T>(new T(unwrap_reference(u)), keep_count());
265 }
266 
267 template <typename T, typename U0, typename U1>
268 RcHandle<T> make_rch(U0 const& u0, U1 const& u1)
269 {
270  return RcHandle<T>(new T(unwrap_reference(u0), unwrap_reference(u1)), keep_count());
271 }
272 
273 template <typename T, typename U0, typename U1, typename U2>
274 RcHandle<T> make_rch(U0 const& u0, U1 const& u1, U2 const& u2)
275 {
277 }
278 
279 template <typename T, typename U0, typename U1, typename U2, typename U3>
280 RcHandle<T> make_rch(U0 const& u0, U1 const& u1, U2 const& u2, U3 const& u3)
281 {
283 }
284 
285 template <typename T, typename U0, typename U1, typename U2, typename U3, typename U4>
286 RcHandle<T> make_rch(U0 const& u0, U1 const& u1, U2 const& u2, U3 const& u3, U4 const& u4)
287 {
289 }
290 
291 template <typename T, typename U0, typename U1, typename U2, typename U3, typename U4, typename U5>
292 RcHandle<T> make_rch(U0 const& u0, U1 const& u1, U2 const& u2, U3 const& u3, U4 const& u4, U5 const& u5)
293 {
295 }
296 
297 template <typename T, typename U0, typename U1, typename U2, typename U3, typename U4, typename U5, typename U6>
298 RcHandle<T> make_rch(U0 const& u0, U1 const& u1, U2 const& u2, U3 const& u3, U4 const& u4, U5 const& u5, U6 const& u6)
299 {
301 }
302 
303 template <typename T, typename U0, typename U1, typename U2, typename U3, typename U4, typename U5, typename U6, typename U7>
304 RcHandle<T> make_rch(U0 const& u0, U1 const& u1, U2 const& u2, U3 const& u3, U4 const& u4, U5 const& u5, U6 const& u6, U7 const& u7)
305 {
307 }
308 
309 template<typename T>
311 {
312  OPENDDS_ASSERT(pointer == 0 || pointer->ref_count() > 0);
313  return RcHandle<T>(pointer, inc_count());
314 }
315 
316 
317 } // namespace DCPS
318 } // namespace OpenDDS
319 
321 
322 #endif /* OPENDDS_RCHANDLE_T_H */
bool operator!=(const RcHandle &rhs) const
Definition: RcHandle_T.h:164
void swap(MessageBlock &lhs, MessageBlock &rhs)
RcHandle< T > rchandle_from(T *pointer)
Definition: RcHandle_T.h:310
void release(T x)
T * operator->() const
Definition: RcHandle_T.h:109
RcHandle< T > make_rch()
Definition: RcHandle_T.h:256
T const & unwrap_reference(T const &t)
Definition: RcHandle_T.h:243
#define OPENDDS_ASSERT(C)
Definition: Definitions.h:72
reference_wrapper< T > ref(T &r)
Definition: RcHandle_T.h:237
RcHandle & operator=(const RcHandle< U > &b)
Definition: RcHandle_T.h:87
RcHandle< T > const_rchandle_cast(const RcHandle< U > &h)
Definition: RcHandle_T.h:208
RcHandle(T *p, inc_count)
Definition: RcHandle_T.h:42
void swap(RcHandle &rhs)
Definition: RcHandle_T.h:102
RcHandle(unique_ptr< U > p)
Definition: RcHandle_T.h:37
T * ptr_
The actual "unsmart" pointer to the T object.
Definition: RcHandle_T.h:191
void reset(T *p, U counting_strategy)
Definition: RcHandle_T.h:73
RcHandle(const RcHandle< U > &other)
Definition: RcHandle_T.h:49
RcHandle(T *p, keep_count)
Definition: RcHandle_T.h:31
RcHandle & operator=(unique_ptr< U > b)
Definition: RcHandle_T.h:95
RcHandle< T > static_rchandle_cast(const RcHandle< U > &h)
Definition: RcHandle_T.h:202
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
bool operator==(const RcHandle &rhs) const
Definition: RcHandle_T.h:159
RcHandle< T > dynamic_rchandle_cast(const RcHandle< U > &h)
Definition: RcHandle_T.h:214
RcHandle(const RcHandle &b)
Definition: RcHandle_T.h:55
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
RcHandle & operator=(const RcHandle &b)
Definition: RcHandle_T.h:79
bool operator<(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:80