Concepts.hpp 4.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once

#include <type_traits>

#include <dune/functions/common/functionconcepts.hh>

#include <dune/amdis/common/ConceptsBase.hpp>

namespace AMDiS
{
  /**
    * \defgroup concept Concepts
    * \brief Concept definitions
    * @{
    **/
  namespace Concepts
  {
#ifndef DOXYGEN
    namespace Definition
    {
      // a < b
      struct LessThanComparable
      {
        template <class A, class B>
        auto requires_(A&& a, B&& b) -> decltype( a < b );
      };

      // a + b
      struct Addable
      {
        template <class A, class B>
        auto requires_(A&& a, B&& b) -> decltype(
          Concepts::valid_expr(
            a + b,
            b + a
          ));
      };

      // a - b
      struct Subtractable
      {
        template <class A, class B>
        auto requires_(A&& a, B&& b) -> decltype( a - b );
      };

      // a * b
      struct Multiplicable
      {
        template <class A, class B>
        auto requires_(A&& a, B&& b) -> decltype(
          Concepts::valid_expr(
            a * b,
            b * a
          ));
      };

      // a / b
      struct Divisible
      {
        template <class A, class B>
        auto requires_(A&& a, B&& b) -> decltype( a / b );
      };

      // f(args...)
      struct Callable
      {
        template <class F, class... Args>
        auto requires_(F&& f, Args&&... args) -> decltype( f(std::forward<Args>(args)...));
      };

    } // end namespace Definition

    namespace traits
    {
      template <class A, class B>
      struct is_compatible
        : std::is_same<std::decay_t<A>, std::decay_t<B>> {};

      template <class A, class B>
      struct is_compatible<Types<A>, Types<B>>
        : is_compatible<A,B> {};

      template <>
      struct is_compatible<Types<>, Types<>> : std::true_type {};

      template <class A0, class... As, class B0, class... Bs>
      struct is_compatible<Types<A0,As...>, Types<B0,Bs...>>
        : and_t<is_compatible<A0, B0>::value, is_compatible<Types<As...>, Types<Bs...>>::value> {};

    } // end namespace traits


#endif // DOXYGEN

    /// \brief Argument `A` id (implicitly) convertible to arguemnt `B` and vice versa.
    template <class A, class B >
    constexpr bool Convertible = std::is_convertible<A,B>::value && std::is_convertible<B,A>::value;

    /// Types are the same, up to decay of qualifiers
    template <class A, class B>
    constexpr bool Compatible = traits::is_compatible<A, B>::value;

    /// \brief Argument A is less-than comparable to type B, i.e. A < B is valid
    template <class A, class B = A>
    constexpr bool LessThanComparable = models<Definition::LessThanComparable(A, B)>;

    /// \brief Argument A is addable to type B, i.e. A + B is valid
    template <class A, class B>
    constexpr bool Addable = models<Definition::Addable(A, B)>;

    /// \brief Argument A is subtractable by type B, i.e. A - B is valid
    template <class A, class B>
    constexpr bool Subtractable = models<Definition::Subtractable(A, B)>;

    /// \brief Argument A is multiplicable by type B, i.e. A * B is valid
    template <class A, class B>
    constexpr bool Multiplicable = models<Definition::Multiplicable(A, B)>;

    /// \brief Argument A is divisible by type B, i.e. A / B is valid
    template <class A, class B>
    constexpr bool Divisible = models<Definition::Divisible(A, B)>;

    /// \brief A Collable is a function `F` that can be called with arguments of type `Args...`.
    /**
      * To be used as follows: `Concepts::Collable<F, Args...>`. Returns true, if
      * an instance of `F` can be called by `operator()` with arguments of type
      * `Args...`.
      **/
    template <class F, class... Args>
    constexpr bool Callable = models<Definition::Callable(F, Args...)>;

    /// \brief A Functor is a function `F` with signature `Signature`.
    /**
      * To be used as follows: `Concepts::Functor<F, R(Args...)>`. Returns true, if
      * an instance of `F` can be called by `operator()` with arguments of type
      * `Args...` and returns a value of type `R`, i.e. `Signature := R(Args...)`.
      **/
    template <class F, class Signature> // F, Signature=Return(Arg)
    constexpr bool Functor = Dune::Functions::Concept::isFunction<F, Signature>();

    /// A predicate is a function that returns a boolean.
    template <class F, class... Args>
    constexpr bool Predicate = Functor<F, bool(Args...)>;

    /** @} **/

  } // end namespace Concepts

} // end namespace AMDiS