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 == (T
const *o)
const throw()
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()
273 void __drop_ref()
throw()
276 static_cast<CNT<T>*
>(
this)->h_drop_ref(_p);
279 void __take_ref()
throw()
282 static_cast<CNT<T>*
>(
this)->h_take_ref(_p);
289 template<
typename T,
template<
typename X >
class CNT>
297 Weak_ptr()
throw() {}
298 Weak_ptr(Rp
const &o)
throw() : _p(o.ptr()) {}
299 explicit Weak_ptr(T *o)
throw() : _p(o) {}
301 T *
get()
const throw() {
return _p; }
302 T *ptr()
const throw() {
return _p; }
304 T *operator -> ()
const throw () {
return _p; }
305 operator Null_type
const * ()
const throw()
306 {
return reinterpret_cast<Null_type const*
>(_p); }
312 template<
typename OT,
typename T>
inline 314 {
return ref_ptr(static_cast<OT*>(o.
ptr())); }
316 template<
typename T >
320 template<
typename T >
321 inline Weak_ptr<T> weak_ptr(T *t)
322 {
return Weak_ptr<T>(t); }
328 mutable int _ref_cnt;
331 Ref_obj() : _ref_cnt(0) {}
332 void add_ref()
const throw() { ++_ref_cnt; }
333 int remove_ref()
const throw() {
return --_ref_cnt; }
336 #if __cplusplus >= 201103L 338 template<
typename T,
typename... Args >
340 make_ref_obj(Args &&... args)
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.