C++ Utilities  5.3.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 <string>
6 #include <type_traits>
7 
8 namespace CppUtilities {
9 
11 namespace Traits {
12 
14 namespace Detail {
15 enum class Enabler {};
16 }
18 
20 template <typename If, typename Then, typename Else> using Conditional = typename std::conditional<If::value, Then, Else>::type;
21 
23 template <bool B, typename...> struct Bool : std::integral_constant<bool, B> {
24 };
25 
27 template <typename T> using Not = Bool<!T::value>;
28 
30 template <typename... T> struct Any : Bool<false> {
31 };
33 template <typename Head, typename... Tail> struct Any<Head, Tail...> : Conditional<Head, Bool<true>, Any<Tail...>> {
34 };
35 
37 template <typename... T> struct All : Bool<true> {
38 };
40 template <typename Head, typename... Tail> struct All<Head, Tail...> : Conditional<Head, All<Tail...>, Bool<false>> {
41 };
42 
44 template <typename... T> struct None : Bool<true> {
45 };
47 template <typename Head, typename... Tail> struct None<Head, Tail...> : Conditional<Head, Bool<false>, None<Tail...>> {
48 };
49 
51 template <typename... Condition> using EnableIf = typename std::enable_if<All<Condition...>::value, Detail::Enabler>::type;
53 template <typename... Condition> using DisableIf = typename std::enable_if<!All<Condition...>::value, Detail::Enabler>::type;
54 
56 template <typename... Condition> using EnableIfAny = typename std::enable_if<Any<Condition...>::value, Detail::Enabler>::type;
58 template <typename... Condition> using DisableIfAny = typename std::enable_if<!Any<Condition...>::value, Detail::Enabler>::type;
59 
61 namespace Detail {
62 template <typename T, template <typename...> class Template> struct IsSpecializationOfHelper : Bool<false> {
63 };
64 template <template <typename...> class Template, typename... Args> struct IsSpecializationOfHelper<Template<Args...>, Template> : Bool<true> {
65 };
66 } // namespace Detail
69 template <typename Type, template <typename...> class... TemplateTypes>
70 struct IsSpecializationOf : Detail::IsSpecializationOfHelper<typename std::remove_cv<Type>::type, TemplateTypes...> {
71 };
73 template <typename Type, template <typename...> class... TemplateTypes> struct IsSpecializingAnyOf : Bool<false> {
74 };
76 template <typename Type, template <typename...> class TemplateType, template <typename...> class... RemainingTemplateTypes>
77 struct IsSpecializingAnyOf<Type, TemplateType, RemainingTemplateTypes...>
78  : Conditional<IsSpecializationOf<Type, TemplateType>, Bool<true>, IsSpecializingAnyOf<Type, RemainingTemplateTypes...>> {
79 };
80 
82 template <typename... T> struct IsAnyOf : Bool<false> {
83 };
85 template <typename Type, typename OtherType, typename... RemainingTypes>
86 struct IsAnyOf<Type, OtherType, RemainingTypes...> : Conditional<std::is_same<Type, OtherType>, Bool<true>, IsAnyOf<Type, RemainingTypes...>> {
87 };
89 template <typename... T> struct IsNoneOf : Bool<true> {
90 };
92 template <typename Type, typename OtherType, typename... RemainingTypes>
93 struct IsNoneOf<Type, OtherType, RemainingTypes...> : Conditional<std::is_same<Type, OtherType>, Bool<false>, IsNoneOf<Type, RemainingTypes...>> {
94 };
95 
97 template <typename T>
98 struct IsCString
99  : Bool<std::is_same<char const *, typename std::decay<T>::type>::value || std::is_same<char *, typename std::decay<T>::type>::value> {
100 };
103 template <typename T>
104 struct IsString
105  : Bool<IsCString<T>::value || IsSpecializationOf<T, std::basic_string>::value || IsSpecializationOf<T, std::basic_string_view>::value> {
106 };
107 
109 template <typename T, typename = void> struct IsComplete : Bool<false> {
110 };
112 template <typename T> struct IsComplete<T, decltype(void(sizeof(T)))> : Bool<true> {
113 };
114 
119 #define CPP_UTILITIES_PP_COMMA ,
120 
127 #define CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(CheckName, CheckCode) \
128  namespace Detail { \
129  template <typename T> auto CheckName(int) -> decltype(CheckCode, ::CppUtilities::Traits::Bool<true>{}); \
130  template <typename T>::CppUtilities::Traits::Bool<false> CheckName(...); \
131  } \
132  template <typename T> using CheckName = decltype(Detail::CheckName<T>(0))
133 
135 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsDereferencable, *(std::declval<T &>()));
136 
138 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(HasSize, std::is_integral<decltype(std::declval<T &>().size())>::value);
139 
141 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsReservable, std::declval<T &>().reserve(0u));
142 
144 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsResizable, std::declval<T &>().resize(0u));
145 
147 CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(HasOperatorBool, std::declval<T &>() ? true : false);
148 
151  // begin/end and operator !=
152  std::begin(std::declval<T &>())
153  != std::end(std::declval<T &>()) CPP_UTILITIES_PP_COMMA
154  // operator ,
156  // operator ++
157  ++ std::declval<decltype(begin(std::declval<T &>())) &>() CPP_UTILITIES_PP_COMMA
158  // operator*
159  void(*begin(std::declval<T &>())));
160 
162 template <typename T, EnableIf<IsDereferencable<T>> * = nullptr> constexpr auto &dereferenceMaybe(T &&value)
163 {
164  return *value;
165 }
166 
168 template <typename T, DisableIf<IsDereferencable<T>> * = nullptr> constexpr auto &dereferenceMaybe(T &&value)
169 {
170  return value;
171 }
172 
173 } // namespace Traits
174 
175 } // namespace CppUtilities
176 
177 #endif // CPP_UTILITIES_TRAITS_H
CppUtilities::Traits::EnableIf
typename std::enable_if< All< Condition... >::value, Detail::Enabler >::type EnableIf
Shortcut for std::enable_if to omit ::value and ::type.
Definition: traits.h:51
CppUtilities::Traits::IsAnyOf
Evaluates to Bool<true> if the specified type is any of the specified types; otherwise evaluates to B...
Definition: traits.h:82
CppUtilities::Traits::DisableIf
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:53
CppUtilities::Traits::IsCString
Evaluates to Bool<true> if the specified type is a C-string (char * or const char *); otherwise evalu...
Definition: traits.h:98
CppUtilities::Traits::EnableIfAny
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:56
CppUtilities::Traits::Conditional
typename std::conditional< If::value, Then, Else >::type Conditional
Shortcut for std::conditional to omit ::value and ::type.
Definition: traits.h:20
CppUtilities::Traits::IsSpecializationOf
Evaluates to Bool<true> if the specified type is based on the specified.
Definition: traits.h:70
CppUtilities::Traits::IsString
Evaluates to Bool<true> if the specified type is a standard string, standard string view or C-string ...
Definition: traits.h:104
CppUtilities::Traits::DisableIfAny
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:58
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
CppUtilities::Traits::CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK
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...
CppUtilities::Traits::dereferenceMaybe
constexpr auto & dereferenceMaybe(T &&value)
Dereferences the specified value if possible; otherwise just returns value itself.
Definition: traits.h:162
CppUtilities::Traits::Any
Evaluates to Bool<true> if at least one of the specified conditions is true; otherwise evaluates to B...
Definition: traits.h:30
CppUtilities::Traits::IsComplete
Evaluates to Bool<true> if the specified type is complete; if the type is only forward-declared it ev...
Definition: traits.h:109
CppUtilities::Traits::IsSpecializingAnyOf
Evaluates to Bool<true> if the specified type is based on one of the specified templates; otherwise e...
Definition: traits.h:73
CppUtilities::Traits::Bool
Wraps a static boolean constant.
Definition: traits.h:23
CppUtilities::Traits::All
Evaluates to Bool<true> if all specified conditions are true; otherwise evaluates to Bool<false>.
Definition: traits.h:37
CppUtilities::Traits::None
Evaluates to Bool<true> if none of the specified conditions are true; otherwise evaluates to Bool<fal...
Definition: traits.h:44
CPP_UTILITIES_PP_COMMA
#define CPP_UTILITIES_PP_COMMA
The CPP_UTILITIES_PP_COMMA macro helps passing "," as a macro argument.
Definition: traits.h:119
CppUtilities::Traits::IsNoneOf
Evaluates to Bool<true> if the specified type is none of the specified types; otherwise evaluates to ...
Definition: traits.h:89