L4Re - L4 Runtime Environment
capability
Go to the documentation of this file.
1 // vim:set ft=cpp: -*- Mode: C++ -*-
2 /**
3  * \file
4  * L4::Cap related definitions.
5  *
6  * \author Alexander Warg <alexander.warg@os.inf.tu-dresden.de>
7  *
8  */
9 /*
10  * (c) 2008-2009,2015 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/consts.h>
29 #include <l4/sys/types.h>
30 #include <l4/sys/kobject>
31 #include <l4/sys/task.h>
32 
33 namespace L4
34 {
35 
36 /* Forward declarations for our kernel object classes. */
37 class Task;
38 class Thread;
39 class Factory;
40 class Irq;
41 class Log;
42 class Vm;
43 class Kobject;
44 
45 /**
46  * Disable copy of a class.
47  *
48  * \param _class Name of the class that shall not have value copy semantics.
49  *
50  * The typical use of this is:
51  * ~~~
52  * class Non_value
53  * {
54  * L4_DISABLE_COPY(Non_value)
55  *
56  * ...
57  * }
58  * ~~~
59  */
60 #if __cplusplus >= 201103L
61 # define L4_DISABLE_COPY(_class) \
62  public: \
63  _class(_class const &) = delete; \
64  _class operator = (_class const &) = delete; \
65  private:
66 #else
67 # define L4_DISABLE_COPY(_class) \
68  private: \
69  _class(_class const &); \
70  _class operator = (_class const &);
71 #endif
72 
73 
74 #define L4_KOBJECT_DISABLE_COPY(_class) \
75  protected: \
76  _class(); \
77  L4_DISABLE_COPY(_class)
78 
79 
80 #define L4_KOBJECT(_class) L4_KOBJECT_DISABLE_COPY(_class)
81 
82 inline l4_msgtag_t
83 Cap_base::validate(Cap<Task> task, l4_utcb_t *u) const throw()
84 { return l4_task_cap_valid_u(task.cap(), _c, u); }
85 
86 inline l4_msgtag_t
87 Cap_base::validate(l4_utcb_t *u) const throw()
88 { return l4_task_cap_valid_u(L4_BASE_TASK_CAP, _c, u); }
89 
90 }; // namespace L4
91 
92 #include <l4/sys/meta>
93 
94 namespace L4 {
95 
96 /**
97  * `dynamic_cast` for capabilities.
98  *
99  * \tparam T The target type of the capability.
100  * \tparam F The source type (is usually implicitly set).
101  * \param c The source capability that shall be casted.
102  *
103  * \retval Cap<T> Capability of target interface `T`.
104  * \retval L4_INVALID_CAP `c` does not support the target interface `T` or the
105  * L4::Meta interface.
106  *
107  * The use of this cast operator is similar to the `dynamic_cast<>()` for
108  * C++ pointers. It also induces overhead, because it uses the meta interface
109  * (L4::Meta) to do runtime type checking.
110  *
111  * Example code:
112  *
113  * L4::Cap<L4::Kobject> obj = ... ;
114  * L4::Cap<L4::Icu> icu = L4::cap_dynamic_cast<L4::Icu>(obj);
115  *
116  */
117 template< typename T, typename F >
118 inline
119 Cap<T> cap_dynamic_cast(Cap<F> const &c) throw()
120 {
121  if (!c.is_valid())
122  return Cap<T>::Invalid;
123 
124  Cap<Meta> mc = cap_reinterpret_cast<Meta>(c);
125  Type_info const *m = kobject_typeid<T>();
126  if (m->proto() && l4_error(mc->supports(m->proto())) > 0)
127  return Cap<T>(c.cap());
128 
129  // FIXME: use generic checker
130 #if 0
131  if (l4_error(mc->supports(T::kobject_proto())) > 0)
132  return Cap<T>(c.cap());
133 #endif
134 
135  return Cap<T>::Invalid;
136 }
137 
138 }