C++ Utilities  4.15.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
traits.h
Go to the documentation of this file.
1 #ifndef CPP_UTILITIES_TRAITS_H
2 #define CPP_UTILITIES_TRAITS_H
3 
4 #include <iterator>
5 #include <type_traits>
6 
8 namespace Traits {
9 
11 namespace Detail {
12 enum class Enabler {};
13 }
15 
17 template <typename If, typename Then, typename Else> using Conditional = typename std::conditional<If::value, Then, Else>::type;
18 
20 template <bool B, typename...> struct Bool : std::integral_constant<bool, B> {
21 };
22 
24 template <typename T> using Not = Bool<!T::value>;
25 
27 template <typename... T> struct Any : Bool<false> {
28 };
30 template <typename Head, typename... Tail> struct Any<Head, Tail...> : Conditional<Head, Bool<true>, Any<Tail...>> {
31 };
32 
34 template <typename... T> struct All : Bool<true> {
35 };
37 template <typename Head, typename... Tail> struct All<Head, Tail...> : Conditional<Head, All<Tail...>, Bool<false>> {
38 };
39 
41 template <typename... T> struct None : Bool<true> {
42 };
44 template <typename Head, typename... Tail> struct None<Head, Tail...> : Conditional<Head, Bool<false>, None<Tail...>> {
45 };
46 
48 template <typename... Condition> using EnableIf = typename std::enable_if<All<Condition...>::value, Detail::Enabler>::type;
50 template <typename... Condition> using DisableIf = typename std::enable_if<!All<Condition...>::value, Detail::Enabler>::type;
51 
53 template <typename... Condition> using EnableIfAny = typename std::enable_if<Any<Condition...>::value, Detail::Enabler>::type;
55 template <typename... Condition> using DisableIfAny = typename std::enable_if<!Any<Condition...>::value, Detail::Enabler>::type;
56 
58 template <typename T, template <typename...> class Template> struct IsSpecializationOf : Bool<false> {
59 };
61 template <template <typename...> class Template, typename... Args> struct IsSpecializationOf<Template<Args...>, Template> : Bool<true> {
62 };
64 template <typename Type, template <typename...> class... TemplateTypes> struct IsSpecializingAnyOf : Bool<false> {
65 };
67 template <typename Type, template <typename...> class TemplateType, template <typename...> class... RemainingTemplateTypes>
68 struct IsSpecializingAnyOf<Type, TemplateType, RemainingTemplateTypes...>
69  : Conditional<IsSpecializationOf<Type, TemplateType>, Bool<true>, IsSpecializingAnyOf<Type, RemainingTemplateTypes...>> {
70 };
71 
73 template <typename... T> struct IsAnyOf : Bool<false> {
74 };
76 template <typename Type, typename OtherType, typename... RemainingTypes>
77 struct IsAnyOf<Type, OtherType, RemainingTypes...> : Conditional<std::is_same<Type, OtherType>, Bool<true>, IsAnyOf<Type, RemainingTypes...>> {
78 };
80 template <typename... T> struct IsNoneOf : Bool<true> {
81 };
83 template <typename Type, typename OtherType, typename... RemainingTypes>
84 struct IsNoneOf<Type, OtherType, RemainingTypes...> : Conditional<std::is_same<Type, OtherType>, Bool<false>, IsNoneOf<Type, RemainingTypes...>> {
85 };
86 
88 template <typename T>
89 struct IsCString
90  : Bool<std::is_same<char const *, typename std::decay<T>::type>::value || std::is_same<char *, typename std::decay<T>::type>::value> {
91 };
93 template <typename T> struct IsString : Bool<IsCString<T>::value || IsSpecializationOf<T, std::basic_string>::value> {
94 };
95 
97 template <typename T, typename = void> struct IsComplete : Bool<false> {
98 };
100 template <typename T> struct IsComplete<T, decltype(void(sizeof(T)))> : Bool<true> {
101 };
102 
107 #define CPP_UTILITIES_PP_COMMA ,
108 
115 #define CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(CheckName, CheckCode) \
116  namespace Detail { \
117  template <typename T> auto CheckName(int) -> decltype(CheckCode, ::Traits::Bool<true>{}); \
118  template <typename T>::Traits::Bool<false> CheckName(...); \
119  } \
120  template <typename T> using CheckName = decltype(Detail::CheckName<T>(0))
121 
123 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsDereferencable, *(std::declval<T &>()));
124 
126 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(HasSize, std::is_integral<decltype(std::declval<T &>().size())>::value);
127 
129 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsReservable, std::declval<T &>().reserve(0u));
130 
132 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsResizable, std::declval<T &>().resize(0u));
133 
136  // begin/end and operator !=
137  std::begin(std::declval<T &>())
138  != std::end(std::declval<T &>()) CPP_UTILITIES_PP_COMMA
139  // operator ,
141  // operator ++
142  ++ std::declval<decltype(begin(std::declval<T &>())) &>() CPP_UTILITIES_PP_COMMA
143  // operator*
144  void(*begin(std::declval<T &>())));
145 
147 template <typename T, EnableIf<IsDereferencable<T>> * = nullptr> constexpr auto &dereferenceMaybe(T &value)
148 {
149  return *value;
150 }
151 
153 template <typename T, DisableIf<IsDereferencable<T>> * = nullptr> constexpr auto &dereferenceMaybe(T &value)
154 {
155  return value;
156 }
157 
159 template <typename T, EnableIf<IsDereferencable<T>> * = nullptr> constexpr const auto &dereferenceMaybe(const T &value)
160 {
161  return *value;
162 }
163 
165 template <typename T, DisableIf<IsDereferencable<T>> * = nullptr> constexpr const auto &dereferenceMaybe(const T &value)
166 {
167  return value;
168 }
169 
170 } // namespace Traits
171 
172 #endif // CPP_UTILITIES_TRAITS_H
constexpr auto & dereferenceMaybe(T &value)
Dereferences the specified value if possible; otherwise just returns value itself.
Definition: traits.h:147
Evaluates to Bool<true> if the specified type is any of the specified types; otherwise evaluates to B...
Definition: traits.h:73
Evaluates to Bool<true> if the specified type is based on the specified.
Definition: traits.h:58
Evaluates to Bool<true> if the specified type is complete; if the type is only forward-declared it ev...
Definition: traits.h:97
Evaluates to Bool<true> if all specified conditions are true; otherwise evaluates to Bool<false>...
Definition: traits.h:34
Evaluates to Bool<true> if the specified type is based on one of the specified templates; otherwise e...
Definition: traits.h:64
typename std::enable_if< All< Condition... >::value, Detail::Enabler >::type EnableIf
Shortcut for std::enable_if to omit ::value and ::type.
Definition: traits.h:48
Evaluates to Bool<true> if the specified type is a C-string (char * or const char *); otherwise evalu...
Definition: traits.h:89
#define CPP_UTILITIES_PP_COMMA
The CPP_UTILITIES_PP_COMMA macro helps passing "," as a macro argument.
Definition: traits.h:107
typename std::conditional< If::value, Then, Else >::type Conditional
Shortcut for std::conditional to omit ::value and ::type.
Definition: traits.h:17
Evaluates to Bool<true> if the specified type is none of the specified types; otherwise evaluates to ...
Definition: traits.h:80
typename std::enable_if<!Any< Condition... >::value, Detail::Enabler >::type DisableIfAny
Shortcut for std::enable_if to apply Traits::Any, negate the condition and omit ::value and ::type...
Definition: traits.h:55
typename std::enable_if<!All< Condition... >::value, Detail::Enabler >::type DisableIf
Shortcut for std::enable_if to negate the condition and omit ::value and ::type.
Definition: traits.h:50
Evaluates to Bool<true> if at least one of the specified conditions is true; otherwise evaluates to B...
Definition: traits.h:27
CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsDereferencable, *(std::declval< T &>()))
Evaluates to Bool<true> if the specified type can be dereferenced using the *-operator; otherwise eva...
Evaluates to Bool<true> if the specified type is a C-string (char * or const char *); otherwise evalu...
Definition: traits.h:93
typename std::enable_if< Any< Condition... >::value, Detail::Enabler >::type EnableIfAny
Shortcut for std::enable_if to apply Traits::Any and omit ::value and ::type.
Definition: traits.h:53
Evaluates to Bool<true> if none of the specified conditions are true; otherwise evaluates to Bool<fal...
Definition: traits.h:41
Contains traits for conveniently exploiting SFINAE.
Definition: traits.h:8
Wraps a static boolean constant.
Definition: traits.h:20