Loading...
Searching...
No Matches
smart_capability
Go to the documentation of this file.
// vim:set ft=cpp: -*- Mode: C++ -*-
/*
* (c) 2008-2009 Author(s)
* economic rights: Technische Universität Dresden (Germany)
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include <l4/sys/capability>
namespace L4 {
template< typename T, typename SMART >
class Smart_cap : public Cap_base, private SMART
{
public:
SMART const &smart() const noexcept { return *this; }
void _delete() noexcept
{
SMART::free(const_cast<Smart_cap<T,SMART>&>(*this));
}
Cap<T> release() const noexcept
{
l4_cap_idx_t r = cap();
SMART::invalidate(const_cast<Smart_cap<T,SMART>&>(*this));
return Cap<T>(r);
}
void reset() noexcept
{
_c = L4_INVALID_CAP;
}
Smart_cap() noexcept : Cap_base(Invalid) {}
Smart_cap(Cap_base::Cap_type t) noexcept : Cap_base(t) {}
template< typename O >
Smart_cap(Cap<O> const &p) noexcept : Cap_base(p.cap())
{ Cap<T>::template check_convertible_from<O>(); }
template< typename O >
Smart_cap(Cap<O> const &p, SMART const &smart) noexcept
: Cap_base(p.cap()), SMART(smart)
{ Cap<T>::template check_convertible_from<O>(); }
template< typename O >
Smart_cap(Smart_cap<O, SMART> const &o) noexcept
: Cap_base(SMART::copy(o)), SMART(o.smart())
{ Cap<T>::template check_convertible_from<O>(); }
Smart_cap(Smart_cap const &o) noexcept
: Cap_base(SMART::copy(o)), SMART(o.smart())
{ }
template< typename O >
Smart_cap(typename Cap<O>::Cap_type cap) noexcept : Cap_base(cap)
{ Cap<T>::template check_convertible_from<O>(); }
void operator = (typename Cap<T>::Cap_type cap) noexcept
{
_delete();
_c = cap;
}
template< typename O >
void operator = (Smart_cap<O, SMART> const &o) noexcept
{
_delete();
_c = this->SMART::copy(o).cap();
this->SMART::operator = (o.smart());
// return *this;
}
Smart_cap const &operator = (Smart_cap const &o) noexcept
{
if (&o == this)
return *this;
_delete();
_c = this->SMART::copy(o).cap();
this->SMART::operator = (o.smart());
return *this;
}
#if __cplusplus >= 201103L
template< typename O >
Smart_cap(Smart_cap<O, SMART> &&o) noexcept
: Cap_base(o.release()), SMART(o.smart())
{ Cap<T>::template check_convertible_from<O>(); }
Smart_cap(Smart_cap &&o) noexcept
: Cap_base(o.release()), SMART(o.smart())
{ }
template< typename O >
void operator = (Smart_cap<O, SMART> &&o) noexcept
{
_delete();
_c = o.release().cap();
this->SMART::operator = (o.smart());
// return *this;
}
Smart_cap const &operator = (Smart_cap &&o) noexcept
{
if (&o == this)
return *this;
_delete();
_c = o.release().cap();
this->SMART::operator = (o.smart());
return *this;
}
#endif
Cap<T> operator -> () const noexcept { return Cap<T>(_c); }
Cap<T> get() const noexcept { return Cap<T>(_c); }
~Smart_cap() noexcept { _delete(); }
};
template< typename T >
class Weak_cap : public Cap_base
{
public:
Weak_cap() noexcept : Cap_base(Invalid) {}
template< typename O >
Weak_cap(typename Cap<O>::Cap_type t) noexcept : Cap_base(t)
{ Cap<T>::template check_convertible_from<O>(); }
template< typename O, typename S >
Weak_cap(Smart_cap<O, S> const &c) noexcept : Cap_base(c.cap())
{ Cap<T>::template check_convertible_from<O>(); }
Weak_cap(Weak_cap const &o) noexcept : Cap_base(o) {}
template< typename O >
Weak_cap(Weak_cap<O> const &o) noexcept : Cap_base(o)
{ Cap<T>::template check_convertible_from<O>(); }
};
namespace Cap_traits {
template< typename T1, typename T2 >
struct Type { enum { Equal = false }; };
template< typename T1 >
struct Type<T1,T1> { enum { Equal = true }; };
};
template< typename T, typename F, typename SMART >
inline
Smart_cap<T, SMART> cap_cast(Smart_cap<F, SMART> const &c) noexcept
{
Cap<T>::template check_castable_from<F>();
return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
}
template< typename T, typename F, typename SMART >
inline
Smart_cap<T, SMART> cap_reinterpret_cast(Smart_cap<F, SMART> const &c) noexcept
{
return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
}
}
capability
L4::Cap related definitions.
L4::Cap_base
Base class for all kinds of capabilities.
Definition capability.h:26
L4::Cap_base::_c
l4_cap_idx_t _c
The C representation of a capability selector.
Definition capability.h:203
L4::Cap_base::cap
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:49
L4::Cap_base::Cap_type
Cap_type
Invalid capability type.
Definition capability.h:41
L4::Cap_base::Invalid
@ Invalid
Invalid capability selector.
Definition capability.h:42
L4::Cap_base::Cap_base
Cap_base(l4_cap_idx_t c) noexcept
Generate a capability from its C representation.
Definition capability.h:149
L4::Cap_base::Cap_base
Cap_base() noexcept
Create an uninitialized instance.
Definition capability.h:165
L4::Cap
C++ interface for capabilities.
Definition capability.h:224
L4::Smart_cap
Smart capability class.
Definition smart_capability:26
L4::Smart_cap::operator->
Cap< T > operator->() const noexcept
Member access of a T.
Definition smart_capability:143
L4::Smart_cap::Smart_cap
Smart_cap(Cap< O > const &p) noexcept
Internal constructor, use to generate a capability from a this pointer.
Definition smart_capability:62
l4_cap_idx_t
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:336
L4_INVALID_CAP
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:153
L4
L4 low-level kernel interface.
Definition l4sys-l4f-groups.dox:50
L4::cap_reinterpret_cast
Cap< T > cap_reinterpret_cast(Cap< F > const &c) noexcept
reinterpret_cast for capabilities.
Definition capability.h:447
L4::cap_cast
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
Definition capability.h:416