MADARA  3.4.1
StlHelper.h
Go to the documentation of this file.
1 #pragma once
2 
10 #include <type_traits>
11 
20 #define MADARA_MAKE_SUPPORT_TEST(name, var, expr) \
21  template<typename T> \
22  struct supports_##name##_impl \
23  { \
24  template<typename U> \
25  static auto test(U* var) -> decltype((expr), std::true_type()); \
26  template<typename U> \
27  static auto test(...) -> std::false_type; \
28  using type = decltype(test<T>(0)); \
29  }; \
30  template<typename T> \
31  struct supports_##name : supports_##name##_impl<T>::type \
32  { \
33  }
34 
43 #define MADARA_MAKE_VAL_SUPPORT_TEST(name, var, expr) \
44  template<typename T> \
45  struct supports_##name##_impl \
46  { \
47  template<typename U> \
48  static auto test(U var) -> decltype((expr), std::true_type()); \
49  template<typename U> \
50  static auto test(...) -> std::false_type; \
51  using type = decltype(test<T>(std::declval<T>())); \
52  }; \
53  template<typename T> \
54  struct supports_##name : supports_##name##_impl<T>::type \
55  { \
56  }
57 
58 namespace madara
59 {
60 namespace utility
61 {
65 template<class T>
66 struct type
67 {
68  type() = default;
69  using self = T;
70 };
71 
73 template<bool Pred, typename T = void>
74 using enable_if_ = typename std::enable_if<Pred, T>::type;
75 
77 template<typename T>
78 using decay_ = typename std::decay<T>::type;
79 
83 template<typename T, typename... Args>
84 std::unique_ptr<T> mk_unique(Args&&... args)
85 {
86  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
87 }
88 
92 template<typename T>
93 std::unique_ptr<decay_<T>> into_unique(T&& val)
94 {
95  using V = decay_<T>;
96  return mk_unique<V>(std::forward<T>(val));
97 }
98 
99 
101 template<typename T, typename U>
102 constexpr bool is_same()
103 {
104  return std::is_same<T, U>::value;
105 }
106 
107 
109 template<typename T, typename U>
110 constexpr bool is_same_decayed()
111 {
112  return is_same<decay_<T>, decay_<U>>();
113 }
114 
116 template<typename Base, typename Derived>
117 constexpr bool is_base_of()
118 {
119  return std::is_base_of<Base, Derived>::value;
120 }
121 
123 template<typename Base, typename Derived>
124 constexpr bool is_base_of_decayed()
125 {
126  return is_base_of<decay_<Base>, decay_<Derived>>();
127 }
128 
130 template<typename From, typename To>
131 constexpr bool is_convertible()
132 {
133  return std::is_convertible<From, To>::value;
134 }
135 
137 template<typename T>
138 constexpr bool is_integral()
139 {
140  return std::is_integral<T>::value;
141 }
142 
144 template<typename T>
145 constexpr bool is_floating_point()
146 {
147  return std::is_floating_point<T>::value;
148 }
149 
151 template<typename T>
152 constexpr bool is_arithmetic()
153 {
154  return std::is_arithmetic<T>::value;
155 }
156 
158 template<typename T>
159 constexpr bool is_enum()
160 {
161  return std::is_enum<T>::value;
162 }
163 
165 template<typename T>
166 constexpr bool is_numeric()
167 {
168  return is_arithmetic<T>() || is_enum<T>();
169 }
170 
172 template<typename T>
173 constexpr bool is_int_numeric()
174 {
175  return is_integral<T>() || is_enum<T>();
176 }
177 
178 #define MADARA_AUTORET_FUNC(NAME, ARGS, ...) \
179  inline auto NAME \
180  ARGS->::madara::utility::core::decay_<decltype(__VA_ARGS__)> \
181  { \
182  return (__VA_ARGS__); \
183  }
184 
185 #define MADARA_AUTORET_REF_FUNC(NAME, ARGS, ...) \
186  inline auto NAME ARGS->decltype(__VA_ARGS__) \
187  { \
188  return (__VA_ARGS__); \
189  }
190 
191 template<typename Func, typename Arg0>
193  invoke_, (Func func, Arg0&& arg0), std::forward<Arg0>(arg0).*func)
194 
195 template<typename Func, typename Arg0, typename... Args>
196 MADARA_AUTORET_REF_FUNC(invoke_, (Func func, Arg0&& arg0, Args&&... args),
197  (std::forward<Arg0>(arg0).*func)(std::forward<Args>(args)...))
198 
199 template<typename Func, typename... Args>
201  invoke_, (Func func, Args&&... args), func(std::forward<Args>(args)...))
202 
203 } // namespace utility
204 } // namespace madara
#define MADARA_AUTORET_REF_FUNC(NAME, ARGS,...)
Definition: StlHelper.h:185
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:15
constexpr bool is_base_of()
Less verbose equivalent for std::is_base_of.
Definition: StlHelper.h:117
constexpr bool is_int_numeric()
Is T arithmetic or an enum?
Definition: StlHelper.h:173
std::unique_ptr< T > mk_unique(Args &&... args)
Creates a unique_ptr for the templated type.
Definition: StlHelper.h:84
typename std::enable_if< Pred, T >::type enable_if_
Less verbose synonym for std::enable_if.
Definition: StlHelper.h:74
constexpr bool is_enum()
Less verbose equivalent for std::is_enum.
Definition: StlHelper.h:159
constexpr bool is_convertible()
Less verbose equivalent for std::is_convertible.
Definition: StlHelper.h:131
std::unique_ptr< decay_< T > > into_unique(T &&val)
Converts a typed value into a unique_ptr of a decayed type.
Definition: StlHelper.h:93
constexpr bool is_same()
Less verbose equivalent for std::is_same.
Definition: StlHelper.h:102
constexpr bool is_arithmetic()
Less verbose equivalent for std::is_arithmetic.
Definition: StlHelper.h:152
constexpr bool is_integral()
Less verbose equivalent for std::is_integral.
Definition: StlHelper.h:138
auto invoke_(Func func, Arg0 &&arg0) -> decltype(std::forward< Arg0 >(arg0).*func)
Definition: StlHelper.h:193
constexpr bool is_same_decayed()
Composition of std::is_same and std::decay.
Definition: StlHelper.h:110
constexpr bool is_numeric()
Is T arithmetic or an enum?
Definition: StlHelper.h:166
typename std::decay< T >::type decay_
Less verbose synonym for std::decay.
Definition: StlHelper.h:78
constexpr bool is_floating_point()
Less verbose equivalent for std::is_floating_point.
Definition: StlHelper.h:145
constexpr bool is_base_of_decayed()
Composition of std::is_base_of and std::decay.
Definition: StlHelper.h:124
Copyright(c) 2020 Galois.
helper type for specifying template type parameters using a function argument instead of inside expli...
Definition: StlHelper.h:67