29struct Default_ref_counter
31 void h_drop_ref(T *p)
noexcept
33 if (p->remove_ref() == 0)
37 void h_take_ref(T *p)
noexcept
49template<
typename T,
template<
typename X >
class CNT = Default_ref_counter>
79 template<
typename X >
class CNT = Default_ref_counter
81class Ref_ptr :
public Ref_ptr_base,
private CNT<T>
84 typedef decltype(
nullptr) Null_type;
85 typedef Weak_ptr<T, CNT> Wp;
91 Ref_ptr(Ref_ptr_base::Default_value v)
92 : _p(reinterpret_cast<T*>(static_cast<unsigned long>(v))) {}
99 Ref_ptr(Wp
const &o) noexcept : _p(o.ptr())
103 Ref_ptr(
decltype(
nullptr) n) noexcept : _p(n) {}
125 Ref_ptr(T *o, [[maybe_unused]]
bool d) noexcept : _p(o) { }
132 T *
get() const noexcept __attribute__((pure))
138 T *
ptr() const noexcept __attribute__((pure))
159 template<
typename OT>
160 Ref_ptr(Ref_ptr<OT, CNT>
const &o)
noexcept
166 Ref_ptr(Ref_ptr<T>
const &o)
noexcept
172 template<
typename OT >
173 void operator = (Ref_ptr<OT>
const &o)
noexcept
180 void operator = (Ref_ptr<T>
const &o)
noexcept
190 void operator = (Null_type)
noexcept
196 template<
typename OT>
197 Ref_ptr(Ref_ptr<OT, CNT> &&o)
noexcept
198 { _p = o.release(); }
200 Ref_ptr(Ref_ptr<T> &&o)
noexcept
201 { _p = o.release(); }
203 template<
typename OT >
204 void operator = (Ref_ptr<OT> &&o)
noexcept
210 void operator = (Ref_ptr<T> &&o)
noexcept
219 explicit operator bool () const noexcept {
return _p; }
221 T *operator -> () const noexcept
224 bool operator == (
Ref_ptr const &o)
const noexcept
225 {
return _p == o._p; }
227 bool operator != (
Ref_ptr const &o)
const noexcept
228 {
return _p != o._p; }
230 bool operator < (
Ref_ptr const &o)
const noexcept
231 {
return _p < o._p; }
233 bool operator <= (
Ref_ptr const &o)
const noexcept
234 {
return _p <= o._p; }
236 bool operator > (
Ref_ptr const &o)
const noexcept
237 {
return _p > o._p; }
239 bool operator >= (
Ref_ptr const &o)
const noexcept
240 {
return _p >= o._p; }
242 bool operator == (T
const *o)
const noexcept
245 bool operator < (T
const *o)
const noexcept
248 bool operator <= (T
const *o)
const noexcept
251 bool operator > (T
const *o)
const noexcept
254 bool operator >= (T
const *o)
const noexcept
258 void __drop_ref() noexcept
261 static_cast<CNT<T>*
>(
this)->h_drop_ref(_p);
264 void __take_ref() noexcept
267 static_cast<CNT<T>*
>(
this)->h_take_ref(_p);
274template<
typename T,
template<
typename X >
class CNT>
279 typedef Ref_ptr<T, CNT> Rp;
282 Weak_ptr() =
default;
283 Weak_ptr(
decltype(
nullptr)) : _p(nullptr) {}
285 explicit Weak_ptr(
int x)
noexcept
288 {
if (x != 0) __builtin_trap(); }
290 Weak_ptr(Rp
const &o) noexcept : _p(o.ptr()) {}
291 explicit Weak_ptr(T *o) noexcept : _p(o) {}
293 template<
typename OT>
294 Weak_ptr(Weak_ptr<OT, CNT>
const &o) noexcept : _p(o.ptr()) {}
296 Weak_ptr(Weak_ptr<T, CNT>
const &o) noexcept : _p(o._p) {}
298 Weak_ptr<T, CNT> &operator = (
const Weak_ptr<T, CNT> &o) =
default;
300 T *get() const noexcept {
return _p; }
301 T *ptr() const noexcept {
return _p; }
303 T *operator -> () const noexcept {
return _p; }
304 operator Null_type
const * ()
const noexcept
305 {
return reinterpret_cast<Null_type const*
>(_p); }
311template<
typename OT,
typename T>
inline
312Ref_ptr<OT> ref_ptr_static_cast(Ref_ptr<T>
const &o)
313{
return ref_ptr(
static_cast<OT*
>(o.ptr())); }
315template<
typename T >
316inline Ref_ptr<T> ref_ptr(T *t)
317{
return Ref_ptr<T>(t); }
319template<
typename T >
320inline Weak_ptr<T> weak_ptr(T *t)
321{
return Weak_ptr<T>(t); }
327 mutable int _ref_cnt;
330 Ref_obj() : _ref_cnt(0) {}
331 void add_ref() const noexcept { ++_ref_cnt; }
332 int remove_ref() const noexcept {
return --_ref_cnt; }
335template<
typename T,
typename... Args >
337make_ref_obj(Args &&... args)
340template<
typename T,
typename U>
342dynamic_pointer_cast(Ref_ptr<U>
const &p)
noexcept
345 return Ref_ptr<T>(
dynamic_cast<T *
>(p.get()));
348template<
typename T,
typename U>
350static_pointer_cast(Ref_ptr<U>
const &p)
noexcept
353 return Ref_ptr<T>(
static_cast<T *
>(p.get()));
A reference-counting pointer with automatic cleanup.
T * ptr() const noexcept)
Return a raw pointer to the object this shared pointer points to.
Ref_ptr(Wp const &o) noexcept
Create a shared pointer from a weak pointer.
Ref_ptr() noexcept
Default constructor creates a pointer with no managed object.
Ref_ptr(decltype(nullptr) n) noexcept
allow creation from nullptr
T * get() const noexcept)
Return a raw pointer to the object this shared pointer points to.
T * release() noexcept
Release the shared pointer without removing the reference.
Ref_ptr(X *o) noexcept
Create a shared pointer from a raw pointer.
Ref_ptr(T *o, bool d) noexcept
Create a shared pointer from a raw pointer without creating a new reference.
L4 compiler related defines.
#define L4_DEPRECATED(s)
Mark symbol deprecated.