L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
smart_capability_1x
Go to the documentation of this file.
1// vim:set ft=cpp: -*- Mode: C++ -*-
6/*
7 * (c) 2017 Alexander Warg <alexander.warg@kernkonzept.com>
8 *
9 * License: see LICENSE.spdx (in this directory or the directories above)
10 */
11
12#pragma once
13
14#include <l4/sys/capability>
15
16namespace L4 { namespace Detail {
17
18template< typename T, typename IMPL >
19class Smart_cap_base : public Cap_base, protected IMPL
20{
21protected:
22 template<typename X>
23 static IMPL &impl(Smart_cap_base<X, IMPL> &o) { return o; }
24
25 template<typename X>
26 static IMPL const &impl(Smart_cap_base<X, IMPL> const &o) { return o; }
27
28public:
29 template<typename X, typename I>
30 friend class ::L4::Detail::Smart_cap_base;
31
32 Smart_cap_base(Smart_cap_base const &) = delete;
33 Smart_cap_base &operator = (Smart_cap_base const &) = delete;
34
35 Smart_cap_base() noexcept : Cap_base(Invalid) {}
36
37 explicit Smart_cap_base(Cap_base::Cap_type t) noexcept
38 : Cap_base(t)
39 {}
40
41 template<typename O>
42 explicit constexpr Smart_cap_base(Cap<O> c) noexcept
43 : Cap_base(c.cap())
44 {}
45
46 template<typename O>
47 explicit constexpr Smart_cap_base(Cap<O> c, IMPL const &impl) noexcept
48 : Cap_base(c.cap()), IMPL(impl)
49 {}
50
51 Cap<T> release() noexcept
52 {
53 l4_cap_idx_t c = this->cap();
54 IMPL::invalidate(*this);
55 return Cap<T>(c);
56 }
57
58 void reset()
59 { IMPL::free(*this); }
60
61 Cap<T> operator -> () const noexcept { return Cap<T>(this->cap()); }
62 Cap<T> get() const noexcept { return Cap<T>(this->cap()); }
63 ~Smart_cap_base() noexcept { IMPL::free(*this); }
64};
65
66
67template< typename T, typename IMPL >
68class Unique_cap_impl final : public Smart_cap_base<T, IMPL>
69{
70private:
71 using Base = Smart_cap_base<T, IMPL>;
72
73public:
74 using Base::Base;
75 Unique_cap_impl() noexcept = default;
76
77 Unique_cap_impl(Unique_cap_impl &&o) noexcept
78 : Base(o.release(), Base::impl(o))
79 {}
80
81 template<typename O>
82 Unique_cap_impl(Unique_cap_impl<O, IMPL> &&o) noexcept
83 : Base(o.release(), Base::impl(o))
84 { Cap<T>::template check_convertible_from<O>(); }
85
86 Unique_cap_impl &operator = (Unique_cap_impl &&o) noexcept
87 {
88 if (&o == this)
89 return *this;
90
91 IMPL::free(*this);
92 this->_c = o.release().cap();
93 this->IMPL::operator = (Base::impl(o));
94 return *this;
95 }
96
97 template<typename O>
98 Unique_cap_impl &operator = (Unique_cap_impl<O, IMPL> &&o) noexcept
99 {
100 Cap<T>::template check_convertible_from<O>();
101
102 IMPL::free(*this);
103 this->_c = o.release().cap();
104 this->IMPL::operator = (Base::impl(o));
105 return *this;
106 }
107};
108
109template<typename T, typename IMPL>
110class Shared_cap_impl final : public Smart_cap_base<T, IMPL>
111{
112private:
113 using Base = Smart_cap_base<T, IMPL>;
114
115public:
116 using Base::Base;
117 Shared_cap_impl() noexcept = default;
118
119 Shared_cap_impl(Shared_cap_impl &&o) noexcept
120 : Base(o.release(), Base::impl(o))
121 {}
122
123 template<typename O>
124 Shared_cap_impl(Shared_cap_impl<O, IMPL> &&o) noexcept
125 : Base(o.release(), Base::impl(o))
126 { Cap<T>::template check_convertible_from<O>(); }
127
128 template<typename O>
129 Shared_cap_impl(Shared_cap_impl<O, IMPL> &&o, Cap<T> cap) noexcept
130 : Base(cap, Base::impl(o))
131 { o.release(); }
132
133 Shared_cap_impl &operator = (Shared_cap_impl &&o) noexcept
134 {
135 if (&o == this)
136 return *this;
137
138 IMPL::free(*this);
139 this->_c = o.release().cap();
140 this->IMPL::operator = (Base::impl(o));
141 return *this;
142 }
143
144 template<typename O>
145 Shared_cap_impl &operator = (Shared_cap_impl<O, IMPL> &&o) noexcept
146 {
147 Cap<T>::template check_convertible_from<O>();
148
149 IMPL::free(*this);
150 this->_c = o.release().cap();
151 this->IMPL::operator = (Base::impl(o));
152 return *this;
153 }
154
155 Shared_cap_impl(Shared_cap_impl const &o) noexcept
156 : Base()
157 {
158 this->IMPL::operator = (Base::impl(o));
159 this->_c = IMPL::copy(o).cap();
160 }
161
162 template<typename O>
163 Shared_cap_impl(Shared_cap_impl<O, IMPL> const &o) noexcept
164 : Base()
165 {
166 Cap<T>::template check_convertible_from<O>();
167 this->IMPL::operator = (Base::impl(o));
168 this->_c = IMPL::copy(o).cap();
169 }
170
171 template<typename O>
172 Shared_cap_impl(Shared_cap_impl<O, IMPL> const &o, Cap<T> cap) noexcept
173 : Base()
174 {
175 this->IMPL::operator = (Base::impl(o));
176 this->_c = IMPL::copy(cap).cap();
177 }
178
179 Shared_cap_impl &operator = (Shared_cap_impl const &o) noexcept
180 {
181 if (&o == this)
182 return *this;
183
184 IMPL::free(*this);
185 this->IMPL::operator = (static_cast<IMPL const &>(o));
186 this->_c = this->IMPL::copy(o).cap();
187 return *this;
188 }
189
190 template<typename O>
191 Shared_cap_impl &operator = (Shared_cap_impl<O, IMPL> const &o) noexcept
192 {
193 Cap<T>::template check_convertible_from<O>();
194 IMPL::free(*this);
195 this->IMPL::operator = (static_cast<IMPL const &>(o));
196 this->_c = this->IMPL::copy(o).cap();
197 return *this;
198 }
199};
200
201}} // L4::Detail
L4::Cap related definitions.
l4_cap_idx_t _c
The C representation of a capability selector.
Definition capability.h:203
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:49
Cap_type
Invalid capability type.
Definition capability.h:41
@ Invalid
Invalid capability selector.
Definition capability.h:42
Cap_base(l4_cap_idx_t c) noexcept
Generate a capability from its C representation.
Definition capability.h:149
Cap_base() noexcept
Create an uninitialized instance.
Definition capability.h:165
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:357
L4 low-level kernel interface.