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 {
60 IMPL::free(*this);
61 IMPL::invalidate(*this);
62 }
63
64 Cap<T> operator -> () const noexcept { return Cap<T>(this->cap()); }
65 Cap<T> get() const noexcept { return Cap<T>(this->cap()); }
66 ~Smart_cap_base() noexcept { IMPL::free(*this); }
67};
68
69
70template< typename T, typename IMPL >
71class Unique_cap_impl final : public Smart_cap_base<T, IMPL>
72{
73private:
74 using Base = Smart_cap_base<T, IMPL>;
75
76public:
77 using Base::Base;
78 Unique_cap_impl() noexcept = default;
79
80 Unique_cap_impl(Unique_cap_impl &&o) noexcept
81 : Base(o.release(), Base::impl(o))
82 {}
83
84 template<typename O>
85 Unique_cap_impl(Unique_cap_impl<O, IMPL> &&o) noexcept
86 : Base(o.release(), Base::impl(o))
87 { Cap<T>::template check_convertible_from<O>(); }
88
89 Unique_cap_impl &operator = (Unique_cap_impl &&o) noexcept
90 {
91 if (&o == this)
92 return *this;
93
94 IMPL::free(*this);
95 this->_c = o.release().cap();
96 this->IMPL::operator = (Base::impl(o));
97 return *this;
98 }
99
100 template<typename O>
101 Unique_cap_impl &operator = (Unique_cap_impl<O, IMPL> &&o) noexcept
102 {
103 Cap<T>::template check_convertible_from<O>();
104
105 IMPL::free(*this);
106 this->_c = o.release().cap();
107 this->IMPL::operator = (Base::impl(o));
108 return *this;
109 }
110};
111
112template<typename T, typename IMPL>
113class Shared_cap_impl final : public Smart_cap_base<T, IMPL>
114{
115private:
116 using Base = Smart_cap_base<T, IMPL>;
117
118public:
119 using Base::Base;
120 Shared_cap_impl() noexcept = default;
121
122 Shared_cap_impl(Shared_cap_impl &&o) noexcept
123 : Base(o.release(), Base::impl(o))
124 {}
125
126 template<typename O>
127 Shared_cap_impl(Shared_cap_impl<O, IMPL> &&o) noexcept
128 : Base(o.release(), Base::impl(o))
129 { Cap<T>::template check_convertible_from<O>(); }
130
131 template<typename O>
132 Shared_cap_impl(Shared_cap_impl<O, IMPL> &&o, Cap<T> cap) noexcept
133 : Base(cap, Base::impl(o))
134 { o.release(); }
135
136 Shared_cap_impl &operator = (Shared_cap_impl &&o) noexcept
137 {
138 if (&o == this)
139 return *this;
140
141 IMPL::free(*this);
142 this->_c = o.release().cap();
143 this->IMPL::operator = (Base::impl(o));
144 return *this;
145 }
146
147 template<typename O>
148 Shared_cap_impl &operator = (Shared_cap_impl<O, IMPL> &&o) noexcept
149 {
150 Cap<T>::template check_convertible_from<O>();
151
152 IMPL::free(*this);
153 this->_c = o.release().cap();
154 this->IMPL::operator = (Base::impl(o));
155 return *this;
156 }
157
158 Shared_cap_impl(Shared_cap_impl const &o) noexcept
159 : Base()
160 {
161 this->IMPL::operator = (Base::impl(o));
162 this->_c = IMPL::copy(o).cap();
163 }
164
165 template<typename O>
166 Shared_cap_impl(Shared_cap_impl<O, IMPL> const &o) noexcept
167 : Base()
168 {
169 Cap<T>::template check_convertible_from<O>();
170 this->IMPL::operator = (Base::impl(o));
171 this->_c = IMPL::copy(o).cap();
172 }
173
174 template<typename O>
175 Shared_cap_impl(Shared_cap_impl<O, IMPL> const &o, Cap<T> cap) noexcept
176 : Base()
177 {
178 this->IMPL::operator = (Base::impl(o));
179 this->_c = IMPL::copy(cap).cap();
180 }
181
182 Shared_cap_impl &operator = (Shared_cap_impl const &o) noexcept
183 {
184 if (&o == this)
185 return *this;
186
187 IMPL::free(*this);
188 this->IMPL::operator = (static_cast<IMPL const &>(o));
189 this->_c = this->IMPL::copy(o).cap();
190 return *this;
191 }
192
193 template<typename O>
194 Shared_cap_impl &operator = (Shared_cap_impl<O, IMPL> const &o) noexcept
195 {
196 Cap<T>::template check_convertible_from<O>();
197 IMPL::free(*this);
198 this->IMPL::operator = (static_cast<IMPL const &>(o));
199 this->_c = this->IMPL::copy(o).cap();
200 return *this;
201 }
202};
203
204}} // L4::Detail
L4::Cap related definitions.
l4_cap_idx_t _c
The C representation of a capability selector.
Definition capability.h:228
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:372
L4 low-level kernel interface.