L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
smart_capability
Go to the documentation of this file.
1// vim:set ft=cpp: -*- Mode: C++ -*-
9/*
10 * (c) 2008-2009 Author(s)
11 * economic rights: Technische Universität Dresden (Germany)
12 *
13 * This file is part of TUD:OS and distributed under the terms of the
14 * GNU General Public License 2.
15 * Please see the COPYING-GPL-2 file for details.
16 *
17 * As a special exception, you may use this file as part of a free software
18 * library without restriction. Specifically, if other files instantiate
19 * templates or use macros or inline functions from this file, or you compile
20 * this file and link it with other files to produce an executable, this
21 * file does not by itself cause the resulting executable to be covered by
22 * the GNU General Public License. This exception does not however
23 * invalidate any other reasons why the executable file might be covered by
24 * the GNU General Public License.
25 */
26#pragma once
27
28#include <l4/sys/capability>
29
30namespace L4 {
31
35template< typename T, typename SMART >
36class Smart_cap : public Cap_base, private SMART
37{
38public:
39
40 SMART const &smart() const noexcept { return *this; }
41
42 void _delete() noexcept
43 {
44 SMART::free(const_cast<Smart_cap<T,SMART>&>(*this));
45 }
46
47 Cap<T> release() const noexcept
48 {
49 l4_cap_idx_t r = cap();
50 SMART::invalidate(const_cast<Smart_cap<T,SMART>&>(*this));
51
52 return Cap<T>(r);
53 }
54
55 void reset() noexcept
56 {
58 }
59
60 Smart_cap() noexcept : Cap_base(Invalid) {}
61
62 Smart_cap(Cap_base::Cap_type t) noexcept : Cap_base(t) {}
63
72 template< typename O >
73 Smart_cap(Cap<O> const &p) noexcept : Cap_base(p.cap())
74 { Cap<T>::template check_convertible_from<O>(); }
75
76 template< typename O >
77 Smart_cap(Cap<O> const &p, SMART const &smart) noexcept
78 : Cap_base(p.cap()), SMART(smart)
79 { Cap<T>::template check_convertible_from<O>(); }
80
81 template< typename O >
82 Smart_cap(Smart_cap<O, SMART> const &o) noexcept
83 : Cap_base(SMART::copy(o)), SMART(o.smart())
84 { Cap<T>::template check_convertible_from<O>(); }
85
86 Smart_cap(Smart_cap const &o) noexcept
87 : Cap_base(SMART::copy(o)), SMART(o.smart())
88 { }
89
90 template< typename O >
91 Smart_cap(typename Cap<O>::Cap_type cap) noexcept : Cap_base(cap)
92 { Cap<T>::template check_convertible_from<O>(); }
93
94 void operator = (typename Cap<T>::Cap_type cap) noexcept
95 {
96 _delete();
97 _c = cap;
98 }
99
100 template< typename O >
101 void operator = (Smart_cap<O, SMART> const &o) noexcept
102 {
103 _delete();
104 _c = this->SMART::copy(o).cap();
105 this->SMART::operator = (o.smart());
106 // return *this;
107 }
108
109 Smart_cap const &operator = (Smart_cap const &o) noexcept
110 {
111 if (&o == this)
112 return *this;
113
114 _delete();
115 _c = this->SMART::copy(o).cap();
116 this->SMART::operator = (o.smart());
117 return *this;
118 }
119
120#if __cplusplus >= 201103L
121 template< typename O >
122 Smart_cap(Smart_cap<O, SMART> &&o) noexcept
123 : Cap_base(o.release()), SMART(o.smart())
124 { Cap<T>::template check_convertible_from<O>(); }
125
126 Smart_cap(Smart_cap &&o) noexcept
127 : Cap_base(o.release()), SMART(o.smart())
128 { }
129
130 template< typename O >
131 void operator = (Smart_cap<O, SMART> &&o) noexcept
132 {
133 _delete();
134 _c = o.release().cap();
135 this->SMART::operator = (o.smart());
136 // return *this;
137 }
138
139 Smart_cap const &operator = (Smart_cap &&o) noexcept
140 {
141 if (&o == this)
142 return *this;
143
144 _delete();
145 _c = o.release().cap();
146 this->SMART::operator = (o.smart());
147 return *this;
148 }
149#endif
150
154 Cap<T> operator -> () const noexcept { return Cap<T>(_c); }
155
156 Cap<T> get() const noexcept { return Cap<T>(_c); }
157
158 ~Smart_cap() noexcept { _delete(); }
159};
160
161template< typename T >
162class Weak_cap : public Cap_base
163{
164public:
165 Weak_cap() noexcept : Cap_base(Invalid) {}
166
167 template< typename O >
168 Weak_cap(typename Cap<O>::Cap_type t) noexcept : Cap_base(t)
169 { Cap<T>::template check_convertible_from<O>(); }
170
171 template< typename O, typename S >
172 Weak_cap(Smart_cap<O, S> const &c) noexcept : Cap_base(c.cap())
173 { Cap<T>::template check_convertible_from<O>(); }
174
175 Weak_cap(Weak_cap const &o) noexcept : Cap_base(o) {}
176
177 template< typename O >
178 Weak_cap(Weak_cap<O> const &o) noexcept : Cap_base(o)
179 { Cap<T>::template check_convertible_from<O>(); }
180
181};
182
183namespace Cap_traits {
184 template< typename T1, typename T2 >
185 struct Type { enum { Equal = false }; };
186
187 template< typename T1 >
188 struct Type<T1,T1> { enum { Equal = true }; };
189};
190
201template< typename T, typename F, typename SMART >
202inline
204{
205 Cap<T>::template check_castable_from<F>();
206 return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
207}
208
209
220template< typename T, typename F, typename SMART >
221inline
223{
224 return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
225}
226
227
228}
L4::Cap related definitions.
Base class for all kinds of capabilities.
Definition capability.h:26
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
C++ interface for capabilities.
Definition capability.h:219
Smart capability class.
Cap< T > operator->() const noexcept
Member access of a T.
Smart_cap(Cap< O > const &p) noexcept
Internal constructor, use to generate a capability from a this pointer.
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:359
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:168
L4 low-level kernel interface.
Definition io_regblock.h:19
Cap< T > cap_reinterpret_cast(Cap< F > const &c) noexcept
reinterpret_cast for capabilities.
Definition capability.h:442
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
Definition capability.h:411