Files
foc/l4/pkg/l4sys/include/smart_capability
2013-01-11 17:00:47 +01:00

179 lines
4.5 KiB
C++

// vim:set ft=cpp:
/**
* \file
* \brief L4::Capability class.
*
* \author Alexander Warg <alexander.warg@os.inf.tu-dresden.de>
*
*/
/*
* (c) 2008-2009 Author(s)
* economic rights: Technische Universität Dresden (Germany)
*
* This file is part of TUD:OS and distributed under the terms of the
* GNU General Public License 2.
* Please see the COPYING-GPL-2 file for details.
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
#pragma once
#include <l4/sys/capability>
namespace L4 {
/**
* \brief Smart capability class.
*/
template< typename T, typename SMART >
class Smart_cap : public Cap_base, private SMART
{
public:
SMART const &smart() const { return *this; }
void _delete()
{
SMART::free(const_cast<Smart_cap<T,SMART>&>(*this));
}
Cap<T> release() const
{
l4_cap_idx_t r = cap();
SMART::invalidate(const_cast<Smart_cap<T,SMART>&>(*this));
return Cap<T>(r);
}
Smart_cap() : Cap_base(Invalid) {}
/**
* \brief Internal Constructor, use to generate a capability from a \a this
* pointer.
*
* \attention This constructor is only useful to generate a capability
* from the \a this pointer of an objected that is an L4::Kobject.
* Do \em never use this constructor for something else!
* \param p The \a this pointer of the Kobject or derived object
*/
template< typename O >
Smart_cap(Cap<O> const &p) throw() : Cap_base(p.cap())
{ register T* __t = ((O*)100); (void)__t; }
template< typename O >
Smart_cap(Cap<O> const &p, SMART const &smart) throw()
: Cap_base(p.cap()), SMART(smart)
{ register T* __t = ((O*)100); (void)__t; }
template< typename O >
Smart_cap(Smart_cap<O, SMART> const &o) throw()
: Cap_base(SMART::copy(o)), SMART(o.smart())
{ register T* __t = ((O*)100); (void)__t; }
Smart_cap(Smart_cap const &o) throw()
: Cap_base(SMART::copy(o)), SMART(o.smart())
{ }
template< typename O >
Smart_cap(typename Cap<O>::Cap_type cap) throw() : Cap_base(cap)
{ register T* __t = ((O*)100); (void)__t; }
void operator = (typename Cap<T>::Cap_type cap) throw()
{
_delete();
_c = cap;
}
template< typename O >
void operator = (Smart_cap<O, SMART> const &o) throw()
{
_delete();
_c = this->SMART::copy(o).cap();
this->SMART::operator = (o.smart());
// return *this;
}
Smart_cap const &operator = (Smart_cap const &o) throw()
{
if (&o == this)
return *this;
_delete();
_c = this->SMART::copy(o).cap();
this->SMART::operator = (o.smart());
return *this;
}
/**
* \brief Member access of a \a T.
*/
Cap<T> operator -> () const throw() { return Cap<T>(_c); }
Cap<T> get() const throw() { return Cap<T>(_c); }
~Smart_cap() { _delete(); }
};
template< typename T >
class Weak_cap : public Cap_base
{
public:
Weak_cap() : Cap_base(Invalid) {}
template< typename O >
Weak_cap(typename Cap<O>::Cap_type t) : Cap_base(t)
{ register T* __t = ((O*)100); (void)__t; }
template< typename O, typename S >
Weak_cap(Smart_cap<O, S> const &c) : Cap_base(c.cap())
{ register T* __t = ((O*)100); (void)__t; }
Weak_cap(Weak_cap const &o) : Cap_base(o) {}
template< typename O >
Weak_cap(Weak_cap<O> const &o) : Cap_base(o)
{ register T* __t = ((O*)100); (void)__t; }
};
namespace Cap_traits {
template< typename T1, typename T2 >
struct Type { enum { Equal = false }; };
template< typename T1 >
struct Type<T1,T1> { enum { Equal = true }; };
};
/**
* \brief static_cast for capabilities.
*/
template< typename T, typename F, typename SMART >
inline
Smart_cap<T, SMART> cap_cast(Smart_cap<F, SMART> const &c) throw()
{
(void)static_cast<T const *>(reinterpret_cast<F const *>(100));
return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
}
/**
* \brief reinterpret_cast for capabilities.
*/
template< typename T, typename F, typename SMART >
inline
Smart_cap<T, SMART> cap_reinterpret_cast(Smart_cap<F, SMART> const &c) throw()
{
return Smart_cap<T, SMART>(Cap<T>(SMART::copy(c).cap()));
}
}