22 #include "type_traits" 27 template<
typename T >
28 struct Default_ref_counter
30 void h_drop_ref(T *p)
throw()
32 if (p->remove_ref() == 0)
36 void h_take_ref(T *p)
throw()
48 template<
typename T,
template<
typename X >
class CNT = Default_ref_counter>
78 template<
typename X >
class CNT = Default_ref_counter
80 class Ref_ptr :
public Ref_ptr_base,
private CNT<T>
83 #if __cplusplus >= 201103L 84 typedef decltype(
nullptr) Null_type;
86 typedef struct _Null_type const *Null_type;
88 typedef Weak_ptr<T, CNT> Wp;
94 Ref_ptr(Ref_ptr_base::Default_value v) : _p((T*)v) {}
104 #if __cplusplus >= 201103L 105 Ref_ptr(decltype(
nullptr) n) noexcept : _p(n) {}
124 explicit Ref_ptr(T *o)
throw() : _p(o)
138 Ref_ptr(T *o,
bool d)
throw() : _p(o) { (void)d; }
145 T *
get()
const throw()
172 template<
typename OT>
185 template<
typename OT >
203 void operator = (Null_type)
throw()
209 #if __cplusplus >= 201103L 210 template<
typename OT>
217 template<
typename OT >
233 explicit operator bool ()
const throw() {
return _p; }
235 operator Null_type ()
const throw()
236 {
return reinterpret_cast<Null_type
>(_p); }
239 T *operator -> ()
const throw ()
242 bool operator == (
Ref_ptr const &o)
const throw()
243 {
return _p == o._p; }
245 bool operator != (
Ref_ptr const &o)
const throw()
246 {
return _p != o._p; }
248 bool operator < (
Ref_ptr const &o)
const throw()
249 {
return _p < o._p; }
251 bool operator <= (
Ref_ptr const &o)
const throw()
252 {
return _p <= o._p; }
254 bool operator > (
Ref_ptr const &o)
const throw()
255 {
return _p > o._p; }
257 bool operator >= (
Ref_ptr const &o)
const throw()
258 {
return _p >= o._p; }
260 bool operator == (T
const *o)
const throw()
263 bool operator < (T
const *o)
const throw()
266 bool operator <= (T
const *o)
const throw()
269 bool operator > (T
const *o)
const throw()
272 bool operator >= (T
const *o)
const throw()
276 void __drop_ref()
throw()
279 static_cast<CNT<T>*
>(
this)->h_drop_ref(_p);
282 void __take_ref()
throw()
285 static_cast<CNT<T>*
>(
this)->h_take_ref(_p);
292 template<
typename T,
template<
typename X >
class CNT>
300 Weak_ptr()
throw() {}
301 Weak_ptr(Rp
const &o)
throw() : _p(o.ptr()) {}
302 explicit Weak_ptr(T *o)
throw() : _p(o) {}
304 T *
get()
const throw() {
return _p; }
305 T *ptr()
const throw() {
return _p; }
307 T *operator -> ()
const throw () {
return _p; }
308 operator Null_type
const * ()
const throw()
309 {
return reinterpret_cast<Null_type const*
>(_p); }
315 template<
typename OT,
typename T>
inline 317 {
return ref_ptr(static_cast<OT*>(o.
ptr())); }
319 template<
typename T >
323 template<
typename T >
324 inline Weak_ptr<T> weak_ptr(T *t)
325 {
return Weak_ptr<T>(t); }
331 mutable int _ref_cnt;
334 Ref_obj() : _ref_cnt(0) {}
335 void add_ref()
const throw() { ++_ref_cnt; }
336 int remove_ref()
const throw() {
return --_ref_cnt; }
339 #if __cplusplus >= 201103L 341 template<
typename T,
typename... Args >
343 make_ref_obj(Args &&... args)
346 template<
typename T,
typename U>
348 dynamic_pointer_cast(
Ref_ptr<U> const &p) noexcept
354 template<
typename T,
typename U>
356 static_pointer_cast(
Ref_ptr<U> const &p) noexcept
A reference-counting pointer with automatic cleanup.
Ref_ptr(T *o, bool d)
Create a shared pointer from a raw pointer without creating a new reference.
T * release()
Release the shared pointer without removing the reference.
Ref_ptr()
Default constructor creates a pointer with no managed object.
T * ptr() const
Return a raw pointer to the object this shared pointer points to.
Ref_ptr(Wp const &o)
Create a shared pointer from a weak pointer.
Ref_ptr(X *o)
Create a shared pointer from a raw pointer.
T * get() const
Return a raw pointer to the object this shared pointer points to.