L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 * License: see LICENSE.spdx (in this directory or the directories above)
14 */
15#pragma once
16
17#include <l4/sys/capability>
18
19namespace L4 {
20
24template< typename T, typename SMART >
25class Smart_cap : public Cap_base, private SMART
26{
27public:
28
29 SMART const &smart() const noexcept { return *this; }
30
31 void _delete() noexcept
32 {
33 SMART::free(const_cast<Smart_cap<T,SMART>&>(*this));
34 }
35
36 Cap<T> release() const noexcept
37 {
38 l4_cap_idx_t r = cap();
39 SMART::invalidate(const_cast<Smart_cap<T,SMART>&>(*this));
40
41 return Cap<T>(r);
42 }
43
44 void reset() noexcept
45 {
47 }
48
49 Smart_cap() noexcept : Cap_base(Invalid) {}
50
51 Smart_cap(Cap_base::Cap_type t) noexcept : Cap_base(t) {}
52
61 template< typename O >
62 Smart_cap(Cap<O> const &p) noexcept : Cap_base(p.cap())
63 { Cap<T>::template check_convertible_from<O>(); }
64
65 template< typename O >
66 Smart_cap(Cap<O> const &p, SMART const &smart) noexcept
67 : Cap_base(p.cap()), SMART(smart)
68 { Cap<T>::template check_convertible_from<O>(); }
69
70 template< typename O >
71 Smart_cap(Smart_cap<O, SMART> const &o) noexcept
72 : Cap_base(SMART::copy(o)), SMART(o.smart())
73 { Cap<T>::template check_convertible_from<O>(); }
74
75 Smart_cap(Smart_cap const &o) noexcept
76 : Cap_base(SMART::copy(o)), SMART(o.smart())
77 { }
78
79 template< typename O >
80 Smart_cap(typename Cap<O>::Cap_type cap) noexcept : Cap_base(cap)
81 { Cap<T>::template check_convertible_from<O>(); }
82
83 void operator = (typename Cap<T>::Cap_type cap) noexcept
84 {
85 _delete();
86 _c = cap;
87 }
88
89 template< typename O >
90 void operator = (Smart_cap<O, SMART> const &o) noexcept
91 {
92 _delete();
93 _c = this->SMART::copy(o).cap();
94 this->SMART::operator = (o.smart());
95 // return *this;
96 }
97
98 Smart_cap const &operator = (Smart_cap const &o) noexcept
99 {
100 if (&o == this)
101 return *this;
102
103 _delete();
104 _c = this->SMART::copy(o).cap();
105 this->SMART::operator = (o.smart());
106 return *this;
107 }
108
109#if __cplusplus >= 201103L
110 template< typename O >
111 Smart_cap(Smart_cap<O, SMART> &&o) noexcept
112 : Cap_base(o.release()), SMART(o.smart())
113 { Cap<T>::template check_convertible_from<O>(); }
114
115 Smart_cap(Smart_cap &&o) noexcept
116 : Cap_base(o.release()), SMART(o.smart())
117 { }
118
119 template< typename O >
120 void operator = (Smart_cap<O, SMART> &&o) noexcept
121 {
122 _delete();
123 _c = o.release().cap();
124 this->SMART::operator = (o.smart());
125 // return *this;
126 }
127
128 Smart_cap const &operator = (Smart_cap &&o) noexcept
129 {
130 if (&o == this)
131 return *this;
132
133 _delete();
134 _c = o.release().cap();
135 this->SMART::operator = (o.smart());
136 return *this;
137 }
138#endif
139
143 Cap<T> operator -> () const noexcept { return Cap<T>(_c); }
144
145 Cap<T> get() const noexcept { return Cap<T>(_c); }
146
147 ~Smart_cap() noexcept { _delete(); }
148};
149
150template< typename T >
151class Weak_cap : public Cap_base
152{
153public:
154 Weak_cap() noexcept : Cap_base(Invalid) {}
155
156 template< typename O >
157 Weak_cap(typename Cap<O>::Cap_type t) noexcept : Cap_base(t)
158 { Cap<T>::template check_convertible_from<O>(); }
159
160 template< typename O, typename S >
161 Weak_cap(Smart_cap<O, S> const &c) noexcept : Cap_base(c.cap())
162 { Cap<T>::template check_convertible_from<O>(); }
163
164 Weak_cap(Weak_cap const &o) noexcept : Cap_base(o) {}
165
166 template< typename O >
167 Weak_cap(Weak_cap<O> const &o) noexcept : Cap_base(o)
168 { Cap<T>::template check_convertible_from<O>(); }
169
170};
171
172namespace Cap_traits {
173 template< typename T1, typename T2 >
174 struct Type { enum { Equal = false }; };
175
176 template< typename T1 >
177 struct Type<T1,T1> { enum { Equal = true }; };
178};
179
190template< typename T, typename F, typename SMART >
191inline
193{
194 Cap<T>::template check_castable_from<F>();
195 return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
196}
197
198
209template< typename T, typename F, typename SMART >
210inline
212{
213 return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
214}
215
216
217}
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:335
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:157
L4 low-level kernel interface.
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