alpaka
Abstraction Library for Parallel Kernel Acceleration
Concepts.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 
9 namespace alpaka::concepts
10 {
11  //! Tag used in class inheritance hierarchies that describes that a specific concept (TConcept)
12  //! is implemented by the given base class (TBase).
13  template<typename TConcept, typename TBase>
14  struct Implements
15  {
16  };
17 
18  //! Checks whether the concept is implemented by the given class
19  template<typename TConcept, typename TDerived>
21  {
22  template<typename TBase>
23  static auto implements(Implements<TConcept, 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 concept in the inheritance hierarchy.
32  template<typename TConcept, typename TDerived, typename Sfinae = void>
34 
35  //! Base case for types that do not inherit from "Implements<TConcept, ...>" is the type itself.
36  template<typename TConcept, typename TDerived>
38  TConcept,
39  TDerived,
40  std::enable_if_t<!ImplementsConcept<TConcept, TDerived>::value>>
41  {
42  using type = TDerived;
43  };
44 
45  //! For types that inherit from "Implements<TConcept, ...>" it finds the base class (TBase) which
46  //! implements the concept.
47  template<typename TConcept, typename TDerived>
49  TConcept,
50  TDerived,
51  std::enable_if_t<ImplementsConcept<TConcept, TDerived>::value>>
52  {
53  template<typename TBase>
54  static auto implementer(Implements<TConcept, TBase>&) -> 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 concept has to be a publicly accessible base class!");
61  };
62  } // namespace detail
63 
64  //! Returns the type that implements the given concept in the inheritance hierarchy.
65  template<typename TConcept, typename TDerived>
67 } // namespace alpaka::concepts
typename detail::ImplementationBaseType< TConcept, TDerived >::type ImplementationBase
Returns the type that implements the given concept in the inheritance hierarchy.
Definition: Concepts.hpp:66
Checks whether the concept is implemented by the given class.
Definition: Concepts.hpp:21
static auto implements(Implements< TConcept, TBase > &) -> std::true_type
static constexpr auto value
Definition: Concepts.hpp:26
static auto implements(...) -> std::false_type
Tag used in class inheritance hierarchies that describes that a specific concept (TConcept) is implem...
Definition: Concepts.hpp:15
Returns the type that implements the given concept in the inheritance hierarchy.
Definition: Concepts.hpp:33