meta.h
Go to the documentation of this file.
1 /* Copyright 2020 CNRS-AIST JRL */
2 
3 #pragma once
4 
5 #include <type_traits>
6 
7 namespace jrl::qp::internal
8 {
10 template<template<typename...> class Base>
11 struct is_base
12 {
14  template<typename... T>
15  static std::true_type check(Base<T...> const volatile &);
17  static std::false_type check(...);
18 };
19 
28 template<typename T, template<typename...> class Base>
29 constexpr bool derives_from()
30 {
31  return decltype(is_base<Base>::check(std::declval<const T &>()))::value;
32 }
33 
37 template<typename T, typename Base>
38 constexpr bool derives_from()
39 {
40  return std::is_base_of_v<Base, T>;
41 }
42 
49 template<typename T, typename... Base>
50 using enable_for_t = std::enable_if_t<(... || (std::is_same_v<T, Base> || derives_from<T, Base>())), int>;
51 
58 template<typename T, template<typename...> class... Base>
59 using enable_for_templated_t = std::enable_if_t<(... || derives_from<T, Base>()), int>;
60 
62 template<typename T>
63 class always_true : public std::true_type
64 {
65 };
66 
68 template<typename T>
69 class always_false : public std::false_type
70 {
71 };
72 
77 struct identity
78 {
79  template<typename T>
80  constexpr auto operator()(T && v) const noexcept -> decltype(std::forward<T>(v))
81  {
82  return std::forward<T>(v);
83  }
84 };
85 
88 {
89  template<typename T>
90  constexpr auto operator()(const T & v) const noexcept
91  {
92  static_assert(std::is_enum_v<T>, "This is needed before C++20.");
93  return static_cast<std::underlying_type_t<T>>(v);
94  }
95 };
96 
101 template<typename T>
102 using cast_as_underlying_if_enum = std::conditional_t<std::is_enum_v<T>, to_underlying_type, identity>;
103 
104 } // namespace jrl::qp::internal
jrl::qp::internal::derives_from
constexpr bool derives_from()
Definition: meta.h:29
jrl::qp::internal::always_true
Definition: meta.h:63
jrl::qp::internal::cast_as_underlying_if_enum
std::conditional_t< std::is_enum_v< T >, to_underlying_type, identity > cast_as_underlying_if_enum
Definition: meta.h:102
jrl::qp::internal::to_underlying_type::operator()
constexpr auto operator()(const T &v) const noexcept
Definition: meta.h:90
jrl::qp::internal
Definition: ActiveSet.h:11
jrl::qp::internal::to_underlying_type
Definition: meta.h:87
jrl::qp::internal::always_false
Definition: meta.h:69
jrl::qp::internal::enable_for_t
std::enable_if_t<(...||(std::is_same_v< T, Base >||derives_from< T, Base >())), int > enable_for_t
Definition: meta.h:50
jrl::qp::internal::is_base
Definition: meta.h:11
jrl::qp::internal::identity::operator()
constexpr auto operator()(T &&v) const noexcept -> decltype(std::forward< T >(v))
Definition: meta.h:80
jrl::qp::internal::is_base::check
static std::true_type check(Base< T... > const volatile &)
jrl::qp::internal::enable_for_templated_t
std::enable_if_t<(...||derives_from< T, Base >()), int > enable_for_templated_t
Definition: meta.h:59
jrl::qp::internal::identity
Definition: meta.h:77