TypeTraits.hpp 2.91 KB
Newer Older
1
2
3
4
5
6
7
#pragma once

#include <memory>
#include <type_traits>

namespace AMDiS
{
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /// \brief Remove cv and ref qualifiers of type T.
  /**
   * If the type T is a reference type, provides the member typedef type which
   * is the type referred to by T with its topmost cv-qualifiers removed.
   * Otherwise type is T with its topmost cv-qualifiers removed.
   *
   * Note: This is a backport of c++20 std::remove_cvref
   **/
  template< class T >
  struct remove_cvref
  {
    using type = std::remove_cv_t<std::remove_reference_t<T>>;
  };

  /// Helper alias template for \ref remove_cvref
  template< class T >
  using remove_cvref_t = typename remove_cvref<T>::type;

26
27
28
29
30
  namespace Impl
  {
    template <class T>
    struct UnderlyingType
    {
31
32
33
34
35
36
37
38
39
40
41
42
43
      using type = T;
    };

    template <class T>
    struct UnderlyingType<const T>
    {
      using type = typename UnderlyingType<T>::type;
    };

    template <class T>
    struct UnderlyingType<T&>
    {
      using type = typename UnderlyingType<T>::type;
44
45
    };

46
47
48
    template <class T>
    struct UnderlyingType<T*>
    {
49
      using type = typename UnderlyingType<T>::type;
50
51
    };

52
53
54
    template <class T>
    struct UnderlyingType<std::reference_wrapper<T>>
    {
55
      using type = typename UnderlyingType<T>::type;
56
    };
57
58
59
60

    template <class T>
    struct UnderlyingType<std::shared_ptr<T>>
    {
61
      using type = typename UnderlyingType<T>::type;
62
63
64
65
66
    };

    template <class T>
    struct UnderlyingType<std::unique_ptr<T>>
    {
67
      using type = typename UnderlyingType<T>::type;
68
    };
69
70
71
72
73
  }

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

74
75
76
77
78

  /// Macro for forwarding universal references to obj
  #define FWD(obj) std::forward<decltype(obj)>(obj)

  /// A decay version of decltype, similar to GCCs __typeof__
Praetorius, Simon's avatar
Praetorius, Simon committed
79
  #define TYPEOF(...) AMDiS::remove_cvref_t<decltype(__VA_ARGS__)>
80
81
82
83

  /// Extract the static value of an integral_constant variable
  #define VALUE(...) TYPEOF(__VA_ARGS__)::value

84
85
86
87
88
89
90
  // ---------------------------------------------------------------------------

  /// A variadic type list
  template <class... Ts>
  struct Types {};

  template <class... Ts>
Praetorius, Simon's avatar
Praetorius, Simon committed
91
  using Types_t = Types<remove_cvref_t<Ts>...>;
92
93
94
95
96
97


  /// Alias that indicates ownership of resources
  template <class T>
  using owner = T;

98
99
100
101
102
103
  /// A functor with no operation
  struct NoOp
  {
    template <class... T>
    constexpr void operator()(T&&...) const { /* no nothing */ }
  };
104
105
106
107
108

  /// Create a unique_ptr by copy/move construction
  template <class Obj>
  auto makeUniquePtr(Obj&& obj)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
109
    return std::make_unique<TYPEOF(obj)>(FWD(obj));
110
111
  }

112
113
114
115
116
117
  template <class Obj>
  auto makeSharedPtr(Obj&& obj)
  {
    return std::make_shared<TYPEOF(obj)>(FWD(obj));
  }

118
119
  template <bool... b>
  using enable_if_all_t
120
121
    = std::enable_if_t<std::is_same_v<std::integer_sequence<bool,true,b...>,
                                      std::integer_sequence<bool,b...,true>>>;
122

123
} // end namespace AMDiS