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
33namespace L4
34{
35
36/* Forward declarations for our kernel object classes. */
37class Task;
38class Thread;
39class Factory;
40class Irq;
41class Log;
42class Vm;
43class 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
82inline l4_msgtag_t
83Cap_base::validate(Cap<Task> task, l4_utcb_t *u) const noexcept
84{
85  return is_valid() ? l4_task_cap_valid_u(task.cap(), _c, u)
86                    : l4_msgtag(0, 0, 0, 0);
87}
88
89inline l4_msgtag_t
90Cap_base::validate(l4_utcb_t *u) const noexcept
91{
92  return is_valid() ? l4_task_cap_valid_u(L4_BASE_TASK_CAP, _c, u)
93                    : l4_msgtag(0, 0, 0, 0);
94}
95
96}; // namespace L4
97
98#include <l4/sys/meta>
99
100namespace L4 {
101
102/**
103 * `dynamic_cast` for capabilities.
104 *
105 * \tparam T  The target type of the capability.
106 * \tparam F  The source type (is usually implicitly set).
107 * \param  c  The source capability that shall be casted.
108 *
109 * \retval Cap<T>          Capability of target interface `T`.
110 * \retval L4_INVALID_CAP  `c` does not support the target interface `T` or the
111 *                         L4::Meta interface.
112 *
113 * The use of this cast operator is similar to the `dynamic_cast<>()` for
114 * C++ pointers.  It also induces overhead, because it uses the meta interface
115 * (L4::Meta) to do runtime type checking.
116 *
117 * Example code:
118 *
119 *     L4::Cap<L4::Kobject> obj = ... ;
120 *     L4::Cap<L4::Icu> icu = L4::cap_dynamic_cast<L4::Icu>(obj);
121 *
122 */
123template< typename T, typename F >
124inline
125Cap<T> cap_dynamic_cast(Cap<F> const &c) noexcept
126{
127  if (!c.is_valid())
128    return Cap<T>::Invalid;
129
130  Cap<Meta> mc = cap_reinterpret_cast<Meta>(c);
131  Type_info const *m = kobject_typeid<T>();
132  if (m->proto() && l4_error(mc->supports(m->proto())) > 0)
133    return Cap<T>(c.cap());
134
135  // FIXME: use generic checker
136#if 0
137  if (l4_error(mc->supports(T::kobject_proto())) > 0)
138    return Cap<T>(c.cap());
139#endif
140
141  return Cap<T>::Invalid;
142}
143
144}
145