L4Re Operating System Framework – Interface and Usage Documentation
Loading...
Searching...
No Matches
weak_ref
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * Copyright (C) 2015, 2017 Kernkonzept GmbH.
4 * Author(s): Sarah Hoffmann <sarah.hoffmann@kernkonzept.com>
5 * Alwexander Warg <alexander.warg@kernkonzept.com>
6 *
7 * This file is distributed under the terms of the GNU General Public
8 * License, version 2. Please see the COPYING-GPL-2 file for details.
9 */
10#pragma once
11
12#include "hlist"
13
14namespace cxx {
15
25class Weak_ref_base : public H_list_item_t<Weak_ref_base>
26{
27protected:
28 Weak_ref_base(void const *ptr = nullptr) : _obj(ptr) {}
29 void reset_hard() { _obj = nullptr; }
30 void const *_obj;
31
32public:
33 struct List : H_list_t<Weak_ref_base>
34 {
35 void reset()
36 {
37 while (!empty())
38 pop_front()->reset_hard();
39 }
40
41 ~List()
42 { reset(); }
43 };
44
45 explicit operator bool () const
46 { return _obj ? true : false; }
47};
48
49
66template <typename T>
67class Weak_ref : public Weak_ref_base
68{
69public:
70 T *get() const
71 { return reinterpret_cast<T*>(const_cast<void *>(_obj)); }
72
73 T *reset(T *n)
74 {
75 T *r = get();
76 if (r)
77 r->remove_weak_ref(this);
78
79 _obj = n;
80 if (n)
81 n->add_weak_ref(this);
82
83 return r;
84 }
85
86 Weak_ref(T *s = nullptr) : Weak_ref_base(s)
87 {
88 if (s)
89 s->add_weak_ref(this);
90 }
91
92 ~Weak_ref() { reset(0); }
93
94 void operator = (T *n)
95 { reset(n); }
96
97 Weak_ref(Weak_ref const &o) : Weak_ref_base(o._obj)
98 {
99 if (T *x = get())
100 x->add_weak_ref(this);
101 }
102
103 Weak_ref &operator = (Weak_ref const &o)
104 {
105 if (&o == this)
106 return *this;
107
108 reset(o.get());
109 return *this;
110 }
111
112 T &operator * () const { return get(); }
113 T *operator -> () const { return get(); }
114};
115
116class Weak_ref_obj
117{
118protected:
119 template <typename T> friend class Weak_ref;
120 mutable Weak_ref_base::List weak_references;
121
122 void add_weak_ref(Weak_ref_base *ref) const
123 { weak_references.push_front(ref); }
124
125 void remove_weak_ref(Weak_ref_base *ref) const
126 { weak_references.remove(ref); }
127};
128
129}
bool empty() const
Check if the list is empty.
Basic element type for a double-linked H_list.
Definition hlist:34
T * pop_front()
Remove and return the head element of the list.
Definition hlist:127
Generic (base) weak reference to some object.
Definition weak_ref:26
Typed weak reference to an object of type T.
Definition weak_ref:68
Our C++ library.
Definition arith:22
Double-linked list of typed H_list_item_t elements.
Definition hlist:260