TypeTraits.hpp 2.31 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
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
31
      using type = remove_cvref_t<T>;
32
33
    };

34
35
36
37
38
39
    template <class T>
    struct UnderlyingType<T*>
    {
      using type = remove_cvref_t<T>;
    };

40
41
42
    template <class T>
    struct UnderlyingType<std::reference_wrapper<T>>
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
43
      using type = remove_cvref_t<T>;
44
    };
45
46
47
48
49
50
51
52
53
54
55
56

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

    template <class T>
    struct UnderlyingType<std::unique_ptr<T>>
    {
      using type = remove_cvref_t<T>;
    };
57
58
59
60
61
  }

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

62
63
64
65
66

  /// 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
67
  #define TYPEOF(...) AMDiS::remove_cvref_t<decltype(__VA_ARGS__)>
68
69
70
71

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

72
73
74
75
76
77
78
  // ---------------------------------------------------------------------------

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

  template <class... Ts>
Praetorius, Simon's avatar
Praetorius, Simon committed
79
  using Types_t = Types<remove_cvref_t<Ts>...>;
80
81
82
83
84
85


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

86
87
88
89
90
91
  /// A functor with no operation
  struct NoOp
  {
    template <class... T>
    constexpr void operator()(T&&...) const { /* no nothing */ }
  };
92
93
94
95
96

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

} // end namespace AMDiS