L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 typedef Smart_cap_base<T, IMPL> Base;
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 typedef Smart_cap_base<T, IMPL> Base;
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 Shared_cap_impl &operator = (Shared_cap_impl &&o) noexcept
129 {
130 if (&o == this)
131 return *this;
132
133 IMPL::free(*this);
134 this->_c = o.release().cap();
135 this->IMPL::operator = (Base::impl(o));
136 return *this;
137 }
138
139 template<typename O>
140 Shared_cap_impl &operator = (Shared_cap_impl<O, IMPL> &&o) noexcept
141 {
142 Cap<T>::template check_convertible_from<O>();
143
144 IMPL::free(*this);
145 this->_c = o.release().cap();
146 this->IMPL::operator = (Base::impl(o));
147 return *this;
148 }
149
150 Shared_cap_impl(Shared_cap_impl const &o) noexcept
151 : Base()
152 {
153 this->IMPL::operator = (Base::impl(o));
154 this->_c = IMPL::copy(o).cap();
155 }
156
157 template<typename O>
158 Shared_cap_impl(Shared_cap_impl<O, IMPL> const &o) noexcept
159 : Base()
160 {
161 Cap<T>::template check_convertible_from<O>();
162 this->IMPL::operator = (Base::impl(o));
163 this->_c = IMPL::copy(o).cap();
164 }
165
166 Shared_cap_impl &operator = (Shared_cap_impl const &o) noexcept
167 {
168 if (&o == this)
169 return *this;
170
171 IMPL::free(*this);
172 this->IMPL::operator = (static_cast<IMPL const &>(o));
173 this->_c = this->IMPL::copy(o).cap();
174 return *this;
175 }
176
177 template<typename O>
178 Shared_cap_impl &operator = (Shared_cap_impl<O, IMPL> const &o) noexcept
179 {
180 Cap<T>::template check_convertible_from<O>();
181 IMPL::free(*this);
182 this->IMPL::operator = (static_cast<IMPL const &>(o));
183 this->_c = this->IMPL::copy(o).cap();
184 return *this;
185 }
186};
187
188}} // L4::Detail
L4::Cap related definitions.
l4_cap_idx_t _c
The C representation of a capability selector.
Definition capability.h:198
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() noexcept
Create an uninitialized instance.
Definition capability.h:160
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:335
L4 low-level kernel interface.