Concepts.hpp 4.99 KB
Newer Older
1
2
3
4
5
6
7
#pragma once

#include <type_traits>

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

#include <dune/amdis/common/ConceptsBase.hpp>
8
#include <dune/amdis/common/Mpl.hpp>
9
#include <dune/amdis/common/Utility.hpp>
10
11
12
13

namespace AMDiS
{
  /**
14
    * \defgroup Concepts Concepts
15
16
17
    * \brief Concept definitions
    * @{
    **/
18

19
  namespace Traits
20
21
  {
    template <class A, class B>
22
    struct IsSimilar
23
24
25
      : std::is_same<std::decay_t<A>, std::decay_t<B>> {};

    template <class A, class B>
26
27
    struct IsSimilar<Types<A>, Types<B>>
      : IsSimilar<A,B> {};
28
29

    template <>
30
    struct IsSimilar<Types<>, Types<>> : std::true_type {};
31
32

    template <class A0, class... As, class B0, class... Bs>
33
34
    struct IsSimilar<Types<A0,As...>, Types<B0,Bs...>>
      : and_t<IsSimilar<A0, B0>::value, IsSimilar<Types<As...>, Types<Bs...>>::value> {};
35

36
    template <class T>
37
    struct IsReferenceWrapper
38
39
40
      : std::false_type {};

    template <class T>
41
    struct IsReferenceWrapper<std::reference_wrapper<T>>
42
43
      : std::true_type {};

44
  } // end namespace Traits
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
  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)...));
      };

101
102
103
104
105
106
107
108
109
110
      // f(args...)
      struct Evaluable
      {
        template <class F, class... Args>
        auto requires_(F&& f, Args&&... args) -> Void_t<
          decltype( f(std::forward<Args>(args)...)),
          std::result_of_t<std::decay_t<F>(std::decay_t<Args>...)>
          >;
      };

111
112
113
    } // end namespace Definition
#endif // DOXYGEN

114

115
    /// \brief Argument `A` is (implicitly) convertible to arguemnt `B` and vice versa.
116
117
118
119
120
    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>
121
    constexpr bool Similar = Traits::IsSimilar<A, B>::value;
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
150
151

    /// \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...)>;

152
153
154
155
    template <class F, class... Args>
    constexpr bool Evaluable = models<Definition::Evaluable(F, Args...)>;


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    /// \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