1 #ifndef CPP_UTILITIES_TRAITS_H
2 #define CPP_UTILITIES_TRAITS_H
15 enum class Enabler {};
20 template <
typename If,
typename Then,
typename Else>
using Conditional =
typename std::conditional<If::value, Then, Else>::type;
23 template <
bool B,
typename...>
struct Bool : std::integral_constant<bool, B> {
30 template <
typename... T>
struct Any :
Bool<false> {
33 template <
typename Head,
typename... Tail>
struct Any<Head, Tail...> :
Conditional<Head, Bool<true>, Any<Tail...>> {
37 template <
typename... T>
struct All :
Bool<true> {
40 template <
typename Head,
typename... Tail>
struct All<Head, Tail...> :
Conditional<Head, All<Tail...>, Bool<false>> {
44 template <
typename... T>
struct None :
Bool<true> {
47 template <
typename Head,
typename... Tail>
struct None<Head, Tail...> :
Conditional<Head, Bool<false>, None<Tail...>> {
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;
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;
62 template <
typename T,
template <
typename...>
class Template>
struct IsSpecializationOfHelper :
Bool<false> {
64 template <
template <
typename...>
class Template,
typename... Args>
struct IsSpecializationOfHelper<Template<Args...>, Template> :
Bool<true> {
69 template <
typename Type,
template <
typename...>
class... TemplateTypes>
71 : Detail::IsSpecializationOfHelper<typename std::remove_cv<typename std::remove_reference<Type>::type>::type, TemplateTypes...> {
77 template <
typename Type,
template <
typename...>
class TemplateType,
template <
typename...>
class... RemainingTemplateTypes>
79 :
Conditional<IsSpecializationOf<Type, TemplateType>, Bool<true>, IsSpecializingAnyOf<Type, RemainingTemplateTypes...>> {
86 template <
typename Type,
typename OtherType,
typename... RemainingTypes>
87 struct IsAnyOf<Type, OtherType, RemainingTypes...> :
Conditional<std::is_same<Type, OtherType>, Bool<true>, IsAnyOf<Type, RemainingTypes...>> {
93 template <
typename Type,
typename OtherType,
typename... RemainingTypes>
94 struct IsNoneOf<Type, OtherType, RemainingTypes...> :
Conditional<std::is_same<Type, OtherType>, Bool<false>, IsNoneOf<Type, RemainingTypes...>> {
100 :
Bool<std::is_same<char const *, typename std::decay<T>::type>::value || std::is_same<char *, typename std::decay<T>::type>::value> {
104 template <
typename T>
106 :
Bool<IsCString<T>::value || IsSpecializationOf<T, std::basic_string>::value || IsSpecializationOf<T, std::basic_string_view>::value> {
113 template <
typename T>
struct IsComplete<T, decltype(void(sizeof(T)))> :
Bool<true> {
120 #define CPP_UTILITIES_PP_COMMA ,
128 #define CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(CheckName, CheckCode) \
130 template <typename T> auto CheckName(int) -> decltype(CheckCode, ::CppUtilities::Traits::Bool<true>{}); \
131 template <typename T>::CppUtilities::Traits::Bool<false> CheckName(...); \
133 template <typename T> using CheckName = decltype(Detail::CheckName<T>(0))
153 std::begin(std::declval<T &>())
160 void(*begin(std::declval<T &>())));
163 template <
typename T, EnableIf<IsDereferencable<T>> * =
nullptr> constexpr
auto &
dereferenceMaybe(T &&value)
169 template <
typename T, DisableIf<IsDereferencable<T>> * =
nullptr> constexpr
auto &
dereferenceMaybe(T &&value)
constexpr auto & dereferenceMaybe(T &&value)
Dereferences the specified value if possible; otherwise just returns value itself.
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...
typename std::enable_if< All< Condition... >::value, Detail::Enabler >::type EnableIf
Shortcut for std::enable_if to omit ::value and ::type.
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.
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.
typename std::conditional< If::value, Then, Else >::type Conditional
Shortcut for std::conditional to omit ::value and ::type.
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.
Contains all utilities provides by the c++utilities library.
Evaluates to Bool<true> if all specified conditions are true; otherwise evaluates to Bool<false>.
Evaluates to Bool<true> if at least one of the specified conditions is true; otherwise evaluates to B...
Wraps a static boolean constant.
Evaluates to Bool<true> if the specified type is any of the specified types; otherwise evaluates to B...
Evaluates to Bool<true> if the specified type is a C-string (char * or const char *); otherwise evalu...
Evaluates to Bool<true> if the specified type is complete; if the type is only forward-declared it ev...
Evaluates to Bool<true> if the specified type is none of the specified types; otherwise evaluates to ...
Evaluates to Bool<true> if the specified type is based on the specified template; otherwise evaluates...
Evaluates to Bool<true> if the specified type is based on one of the specified templates; otherwise e...
Evaluates to Bool<true> if the specified type is a standard string, standard string view or C-string ...
Evaluates to Bool<true> if none of the specified conditions are true; otherwise evaluates to Bool<fal...
#define CPP_UTILITIES_PP_COMMA
The CPP_UTILITIES_PP_COMMA macro helps passing "," as a macro argument.