alpaka
Abstraction Library for Parallel Kernel Acceleration
Interface.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Benjamin Worpitz, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include <type_traits>
8 
10 {
11  //! Tag used in class inheritance hierarchies that describes that a specific interface (TInterface)
12  //! is implemented by the given base class (TBase).
13  template<typename TInterface, typename TBase>
14  struct Implements
15  {
16  };
17 
18  //! Checks whether the interface is implemented by the given class
19  template<typename TInterface, typename TDerived>
21  {
22  template<typename TBase>
23  static auto implements(Implements<TInterface, TBase>&) -> std::true_type;
24  static auto implements(...) -> std::false_type;
25 
26  static constexpr auto value = decltype(implements(std::declval<TDerived&>()))::value;
27  };
28 
29  namespace detail
30  {
31  //! Returns the type that implements the given interface in the inheritance hierarchy.
32  template<typename TInterface, typename TDerived, typename Sfinae = void>
34 
35  //! Base case for types that do not inherit from "Implements<TInterface, ...>" is the type itself.
36  template<typename TInterface, typename TDerived>
38  TInterface,
39  TDerived,
40  std::enable_if_t<!ImplementsInterface<TInterface, TDerived>::value>>
41  {
42  using type = TDerived;
43  };
44 
45  //! For types that inherit from "Implements<TInterface, ...>" it finds the base class (TBase) which
46  //! implements the interface.
47  template<typename TInterface, typename TDerived>
49  TInterface,
50  TDerived,
51  std::enable_if_t<ImplementsInterface<TInterface, TDerived>::value>>
52  {
53  template<typename TBase>
55 
56  using type = decltype(implementer(std::declval<TDerived&>()));
57 
58  static_assert(
59  std::is_base_of_v<type, TDerived>,
60  "The type implementing the interface has to be a publicly accessible base class!");
61  };
62  } // namespace detail
63 
64  //! Returns the type that implements the given interface in the inheritance hierarchy.
65  template<typename TInterface, typename TDerived>
67 } // namespace alpaka::interface
typename detail::ImplementationBaseType< TInterface, TDerived >::type ImplementationBase
Returns the type that implements the given interface in the inheritance hierarchy.
Definition: Interface.hpp:66
Checks whether the interface is implemented by the given class.
Definition: Interface.hpp:21
static auto implements(Implements< TInterface, TBase > &) -> std::true_type
static auto implements(...) -> std::false_type
Tag used in class inheritance hierarchies that describes that a specific interface (TInterface) is im...
Definition: Interface.hpp:15
Returns the type that implements the given interface in the inheritance hierarchy.
Definition: Interface.hpp:33