Utility.hpp 3.05 KB
Newer Older
1
2
3
4
5
6
7
8
#pragma once

#include <list>
#include <map>
#include <memory>
#include <type_traits>
#include <vector>

9
#include <dune/amdis/common/Mpl.hpp>
10
11
12

namespace AMDiS
{
13
14
15
  // pull in std implementations
  using std::shared_ptr;
  using std::make_shared;
16

17
18
  using std::unique_ptr;
  using std::make_unique;
19

20
  // ---------------------------------------------------------------------------
21

22
23
  namespace Impl
  {
24
25
26
27
28
29
30
31
32
33
34
35
    // workaround for MSVC (problems with alias templates in pack expansion)
    template <class, class T>
    struct InvokeType { using type = T; };

    template <class, class, class T>
    struct InvokeType2 { using type = T; };

    template <class T>
    struct ValueType
    {
      using type = typename InvokeType<T, typename T::value_type>::type;
    };
36
37
38
  }

  template <class T>
39
  using Value_t = typename Impl::ValueType<T>::type;
40
41
42
43
44
45
46
47
48
49
50
51
52

  template <class T>
  using Size_t = typename Impl::InvokeType<T, typename T::size_type>::type;

  template <class T>
  using Result_t = typename Impl::InvokeType<T, typename T::result_type>::type;

  template <class T>
  using Decay_t = typename Impl::InvokeType<T, typename std::decay<T>::type>::type;

  template <class T1, class T2>
  using Common_t = typename Impl::InvokeType2<T1, T2, typename std::common_type<T1,T2>::type>::type;

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  namespace Impl
  {
    template <class T>
    struct UnderlyingType
    {
      using type = T;
    };

    template <class T>
    struct UnderlyingType<std::reference_wrapper<T>>
    {
      using type = T;
    };
  }

  template <class T>
  using Underlying_t = typename Impl::UnderlyingType<T>::type;

71
72
73

  // ---------------------------------------------------------------------------

74

75
76
77
78
79
80
81
  /// A variadic type list
  template <class... Ts>
  struct Types {};

  template <class... Ts>
  using Types_t = Types<Decay_t<Ts>...>;

82

83
84
85
86
87
88
  /// An identity type wrapper, represents the type itself
  template <class T>
  struct Id
  {
    using type = T;
  };
89

90
91
92
  template <class T>
  using Id_t = typename Id<T>::type;

93

94
95
96
97
98
99
100
  /// Alias that indicates ownership of resources
  template <class T>
  using owner = T;

  /// Dummy type for unknown return type
  struct no_valid_type {};

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
  template <class F1, class F2>
  struct overloaded : F1, F2
  {
    overloaded(F1 x1, F2 x2)
      : F1(x1), F2(x2)
    {}

    using F1::operator();
    using F2::operator();
  };

  template <class F>
  F overload(F f) { return f; }

  template <class F1, class F2>
  overloaded<F1, F2> overload(F1 f1, F2 f2)
  {
    return {f1, f2};
  }

  template <class F1, class... Fs>
  auto overload(F1 f1, Fs... fs)
  {
    return overload(f1, overload(fs...));
  }

128
129
  // ---------------------------------------------------------------------------

130

131
  /// generalization of get<tuple>, get<array> for vectors
132
  template <std::size_t I, class T, class A>
133
134
135
136
137
  T const& get(std::vector<T,A> const& vec)
  {
      return vec[I];
  }

138

139
140
  // ---------------------------------------------------------------------------

141
142
143

  struct VectorComponent
  {
144
    std::size_t index;
145
146
  };

147
148
  struct MatrixComponent
  {
149
150
    std::size_t row;
    std::size_t col;
151
  };
152

153
} // end namespace AMDiS