Templates.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #ifndef PLAYRHO_COMMON_TEMPLATES_HPP
22 #define PLAYRHO_COMMON_TEMPLATES_HPP
23 
24 #include <PlayRho/Defines.hpp>
25 
26 #include <algorithm>
27 #include <functional>
28 #include <iterator>
29 #include <limits>
30 #include <typeinfo>
31 #include <type_traits>
32 #include <tuple>
33 #include <utility>
34 
35 namespace playrho {
36 
37 // Bring standard customization points into the namespace...
38 using std::begin;
39 using std::end;
40 using std::cbegin;
41 using std::cend;
42 using std::size;
43 using std::empty;
44 using std::data;
45 using std::swap;
46 
47 namespace detail {
48 
50 template<class...> struct Voidify {
52  using type = void;
53 };
54 
56 template<class... Ts> using VoidT = typename Voidify<Ts...>::type;
57 
59 template<class T, class = void>
60 struct IsIterableImpl: std::false_type {};
61 
63 template<class T>
65  decltype(begin(std::declval<T>())),
66  decltype(end(std::declval<T>())),
67  decltype(++std::declval<decltype(begin(std::declval<T&>()))&>()),
68  decltype(*begin(std::declval<T>()))
69  >>:
70  std::true_type
71 {};
72 
74 template <class T>
75 PLAYRHO_CONSTEXPR inline auto max_size(const T& arg) -> decltype(arg.max_size())
76 {
77  return arg.max_size();
78 }
79 
81 template <class T>
82 PLAYRHO_CONSTEXPR inline auto IsFull(const T& arg) -> decltype(size(arg) == max_size(arg))
83 {
84  return size(arg) == max_size(arg);
85 }
86 
89 template <typename T>
90 static auto Data(T& v)
91 {
92  using ::playrho::data;
93  return data(v);
94 }
95 
98 template <typename T>
99 static auto Size(T& v)
100 {
102  return size(v);
103 }
104 
105 } // namespace detail
106 
108  template<class... T> void NOT_USED(T&&...){}
109 
111  template <typename T>
112  PLAYRHO_CONSTEXPR inline T GetInvalid() noexcept
113  {
114  static_assert(sizeof(T) == 0, "No available specialization");
115  }
116 
118  template <typename T>
119  PLAYRHO_CONSTEXPR inline bool IsValid(const T& value) noexcept
120  {
121  // Note: This is not necessarily a no-op!! But it is a "PLAYRHO_CONSTEXPR inline".
122  //
123  // From http://en.cppreference.com/w/cpp/numeric/math/isnan:
124  // "Another way to test if a floating-point value is NaN is
125  // to compare it with itself:
126  // bool is_nan(double x) { return x != x; }
127  //
128  // So for all T, for which isnan() is implemented, this should work
129  // correctly and quite usefully!
130  //
131  return value == value;
132  }
133 
134  // GetInvalid template specializations.
135 
137  template <>
138  PLAYRHO_CONSTEXPR inline float GetInvalid() noexcept
139  {
140  return std::numeric_limits<float>::signaling_NaN();
141  }
142 
144  template <>
145  PLAYRHO_CONSTEXPR inline double GetInvalid() noexcept
146  {
147  return std::numeric_limits<double>::signaling_NaN();
148  }
149 
151  template <>
152  PLAYRHO_CONSTEXPR inline long double GetInvalid() noexcept
153  {
154  return std::numeric_limits<long double>::signaling_NaN();
155  }
156 
158  template <>
159  PLAYRHO_CONSTEXPR inline std::size_t GetInvalid() noexcept
160  {
161  return static_cast<std::size_t>(-1);
162  }
163 
164  // IsValid template specializations.
165 
167  template <>
168  PLAYRHO_CONSTEXPR inline bool IsValid(const std::size_t& value) noexcept
169  {
170  return value != GetInvalid<std::size_t>();
171  }
172 
173  // Other templates.
174 
176  template <class T>
177  PLAYRHO_CONSTEXPR const T* GetPtr(const T* value) noexcept
178  {
179  return value;
180  }
181 
183  template <class T>
184  PLAYRHO_CONSTEXPR inline T* GetPtr(T* value) noexcept
185  {
186  return value;
187  }
188 
190  template <class T>
191  PLAYRHO_CONSTEXPR const T* GetPtr(const T& value) noexcept
192  {
193  return &value;
194  }
195 
197  template <class T>
198  PLAYRHO_CONSTEXPR inline T* GetPtr(T& value) noexcept
199  {
200  return &value;
201  }
202 
204  template <class T>
205  PLAYRHO_CONSTEXPR const T& GetRef(const T* value) noexcept
206  {
207  return *value;
208  }
209 
211  template <class T>
212  PLAYRHO_CONSTEXPR inline T& GetRef(T* value) noexcept
213  {
214  return *value;
215  }
216 
218  template <class T>
219  PLAYRHO_CONSTEXPR const T& GetRef(const T& value) noexcept
220  {
221  return value;
222  }
223 
225  template <class T>
226  PLAYRHO_CONSTEXPR inline T& GetRef(T& value) noexcept
227  {
228  return value;
229  }
230 
238  template <typename T>
239  bool Visit(const T& /*object*/, void* /*userData*/)
240  {
241  return false;
242  }
243 
248  template <typename T>
249  inline const char* GetTypeName() noexcept
250  {
251  // No gaurantee of what the following returns. Could be mangled!
252  // See http://en.cppreference.com/w/cpp/types/type_info/name
253  return typeid(T).name();
254  }
255 
257  template <>
258  inline const char* GetTypeName<float>() noexcept
259  {
260  return "float";
261  }
262 
264  template <>
265  inline const char* GetTypeName<double>() noexcept
266  {
267  return "double";
268  }
269 
271  template <>
272  inline const char* GetTypeName<long double>() noexcept
273  {
274  return "long double";
275  }
276 
280  template<class T1, class T2, class = void>
281  struct IsEqualityComparable: std::false_type {};
282 
284  template<class T1, class T2>
285  struct IsEqualityComparable<T1, T2, detail::VoidT<decltype(T1{} == T2{})> >: std::true_type {};
286 
288  template<class T1, class T2, class = void>
289  struct IsInequalityComparable: std::false_type {};
290 
292  template<class T1, class T2>
293  struct IsInequalityComparable<T1, T2, detail::VoidT<decltype(T1{} != T2{})> >: std::true_type {};
294 
296  template<class T1, class T2 = T1, class = void>
297  struct IsAddable: std::false_type {};
298 
300  template<class T1, class T2>
301  struct IsAddable<T1, T2, detail::VoidT<decltype(T1{} + T2{})> >: std::true_type {};
302 
304  template<class T1, class T2, class = void>
305  struct IsMultipliable: std::false_type {};
306 
308  template<class T1, class T2>
309  struct IsMultipliable<T1, T2, detail::VoidT<decltype(T1{} * T2{})> >: std::true_type {};
310 
312  template<class T1, class T2, class = void>
313  struct IsDivisable: std::false_type {};
314 
316  template<class T1, class T2>
317  struct IsDivisable<T1, T2, detail::VoidT<decltype(T1{} / T2{})> >: std::true_type {};
318 
322  template<class T, class = void>
323  struct IsArithmetic: std::false_type {};
324 
326  template<class T>
327  struct IsArithmetic<T, detail::VoidT<
328  decltype(T{} + T{}), decltype(T{} - T{}), decltype(T{} * T{}), decltype(T{} / T{})
329  > >: std::true_type {};
330 
332  template<class T>
334 
339  template <typename T, typename Tuple>
340  struct HasType;
341 
346  template <typename T>
347  struct HasType<T, std::tuple<>> : std::false_type {};
348 
353  template <typename T, typename... Ts>
354  struct HasType<T, std::tuple<T, Ts...>> : std::true_type {};
355 
360  template <typename T, typename U, typename... Ts>
361  struct HasType<T, std::tuple<U, Ts...>> : HasType<T, std::tuple<Ts...>> {};
362 
369  template <typename T, typename Tuple>
371 
374  using detail::max_size;
375 
378  using detail::IsFull;
379 
384  template <typename T>
386  {
389  constexpr bool operator()(const T& lhs, const T& rhs) const
390  {
391  using std::less;
392  using ElementType = decltype(*begin(lhs));
393  return std::lexicographical_compare(begin(lhs), end(lhs), begin(rhs), end(rhs),
394  less<ElementType>{});
395  }
396  };
397 
402  template <typename T>
404  {
407  constexpr bool operator()(const T& lhs, const T& rhs) const
408  {
409  using std::greater;
410  using ElementType = decltype(*begin(lhs));
411  return std::lexicographical_compare(begin(lhs), end(lhs), begin(rhs), end(rhs),
412  greater<ElementType>{});
413  }
414  };
415 
420  template <typename T>
422  {
425  constexpr bool operator()(const T& lhs, const T& rhs) const
426  {
427  using std::mismatch;
428  using std::less;
429  using std::get;
430  using ElementType = decltype(*begin(lhs));
431  const auto lhsEnd = end(lhs);
432  const auto diff = mismatch(begin(lhs), lhsEnd, begin(rhs), end(rhs));
433  return (get<0>(diff) == lhsEnd) || less<ElementType>{}(*get<0>(diff), *get<1>(diff));
434  }
435  };
436 
441  template <typename T>
443  {
446  constexpr bool operator()(const T& lhs, const T& rhs) const
447  {
448  using std::mismatch;
449  using std::greater;
450  using std::get;
451  using ElementType = decltype(*begin(lhs));
452  const auto lhsEnd = end(lhs);
453  const auto diff = mismatch(begin(lhs), lhsEnd, begin(rhs), end(rhs));
454  return (get<0>(diff) == lhsEnd) || greater<ElementType>{}(*get<0>(diff), *get<1>(diff));
455  }
456  };
457 
458 } // namespace playrho
459 
460 #endif // PLAYRHO_COMMON_TEMPLATES_HPP