| 1 | #cxx . . $ `<file>/misc/traits.h`/ifndef CPP_UTILITIES_TRAITS_H |
| 2 | #define CPP_UTILITIES_TRAITS_Hcxx . . $ `misc/traits.h:2:9`! |
| 3 | |
| 4 | #include <iterator> |
| 5 | #include <string> |
| 6 | #include <type_traits> |
| 7 | |
| 8 | namespace CppUtilitiescxx . . $ CppUtilities/ { |
| 9 | |
| 10 | /// \brief Contains traits for conveniently exploiting SFINAE. |
| 11 | namespace Traitscxx . . $ CppUtilities/Traits/ { |
| 12 | |
| 13 | /// \cond |
| 14 | namespace Detailcxx . . $ CppUtilities/Traits/Detail/ { |
| 15 | enum class Enablercxx . . $ CppUtilities/Traits/Detail/Enabler# {}; |
| 16 | } |
| 17 | /// \endcond |
| 18 | |
| 19 | /// \brief Shortcut for std::conditional to omit ::value and ::type. |
| 20 | template <typename If, typename Then, typename Else> using Conditionalcxx . . $ CppUtilities/Traits/Conditional# = typename stdcxx . . $ std/::conditional<If::value, Then, Else>::type; |
| 21 | |
| 22 | /// \brief Wraps a static boolean constant. |
| 23 | template <bool B, typename...> struct Boolcxx . . $ CppUtilities/Traits/Bool# : stdcxx . . $ std/::integral_constant<bool, B> {}; |
| 24 | |
| 25 | /// \brief Negates the specified value. |
| 26 | template <typename T> using Notcxx . . $ CppUtilities/Traits/Not# = Boolcxx . . $ CppUtilities/Traits/Bool#<!T::value>; |
| 27 | |
| 28 | /// \brief Evaluates to Bool<true> if at least one of the specified conditions is true; otherwise evaluates to Bool<false>. |
| 29 | template <typename... T> struct Anycxx . . $ CppUtilities/Traits/Any# : Boolcxx . . $ CppUtilities/Traits/Bool#<false> {}; |
| 30 | /// \brief Evaluates to Bool<true> if at least one of the specified conditions is true; otherwise evaluates to Bool<false>. |
| 31 | template <typename Head, typename... Tail> struct Anycxx . . $ CppUtilities/Traits/Any#<Head, Tail...> : Conditionalcxx . . $ CppUtilities/Traits/Conditional#<Head, Boolcxx . . $ CppUtilities/Traits/Bool#<true>, Anycxx . . $ CppUtilities/Traits/Any#<Tail...>> {}; |
| 32 | |
| 33 | /// \brief Evaluates to Bool<true> if all specified conditions are true; otherwise evaluates to Bool<false>. |
| 34 | template <typename... T> struct Allcxx . . $ CppUtilities/Traits/All# : Boolcxx . . $ CppUtilities/Traits/Bool#<true> {}; |
| 35 | /// \brief Evaluates to Bool<true> if all specified conditions are true; otherwise evaluates to Bool<false>. |
| 36 | template <typename Head, typename... Tail> struct Allcxx . . $ CppUtilities/Traits/All#<Head, Tail...> : Conditionalcxx . . $ CppUtilities/Traits/Conditional#<Head, Allcxx . . $ CppUtilities/Traits/All#<Tail...>, Boolcxx . . $ CppUtilities/Traits/Bool#<false>> {}; |
| 37 | |
| 38 | /// \brief Evaluates to Bool<true> if none of the specified conditions are true; otherwise evaluates to Bool<false>. |
| 39 | template <typename... T> struct Nonecxx . . $ CppUtilities/Traits/None# : Boolcxx . . $ CppUtilities/Traits/Bool#<true> {}; |
| 40 | /// \brief Evaluates to Bool<true> if none of the specified conditions are true; otherwise evaluates to Bool<false>. |
| 41 | template <typename Head, typename... Tail> struct Nonecxx . . $ CppUtilities/Traits/None#<Head, Tail...> : Conditionalcxx . . $ CppUtilities/Traits/Conditional#<Head, Boolcxx . . $ CppUtilities/Traits/Bool#<false>, Nonecxx . . $ CppUtilities/Traits/None#<Tail...>> {}; |
| 42 | |
| 43 | /// \brief Shortcut for std::enable_if to omit ::value and ::type. |
| 44 | template <typename... Condition> using EnableIfcxx . . $ CppUtilities/Traits/EnableIf# = typename stdcxx . . $ std/::enable_if<All<Condition...>::value, Detail::Enabler>::type; |
| 45 | /// \brief Shortcut for std::enable_if to negate the condition and omit ::value and ::type. |
| 46 | template <typename... Condition> using DisableIfcxx . . $ CppUtilities/Traits/DisableIf# = typename stdcxx . . $ std/::enable_if<!All<Condition...>::value, Detail::Enabler>::type; |
| 47 | |
| 48 | /// \brief Shortcut for std::enable_if to apply Traits::Any and omit ::value and ::type. |
| 49 | template <typename... Condition> using EnableIfAnycxx . . $ CppUtilities/Traits/EnableIfAny# = typename stdcxx . . $ std/::enable_if<Any<Condition...>::value, Detail::Enabler>::type; |
| 50 | /// \brief Shortcut for std::enable_if to apply Traits::Any, negate the condition and omit ::value and ::type. |
| 51 | template <typename... Condition> using DisableIfAnycxx . . $ CppUtilities/Traits/DisableIfAny# = typename stdcxx . . $ std/::enable_if<!Any<Condition...>::value, Detail::Enabler>::type; |
| 52 | |
| 53 | /// \cond |
| 54 | namespace Detailcxx . . $ CppUtilities/Traits/Detail/ { |
| 55 | template <typename T, template <typename...> class Template> struct IsSpecializationOfHelpercxx . . $ CppUtilities/Traits/Detail/IsSpecializationOfHelper# : Boolcxx . . $ CppUtilities/Traits/Bool#<false> {}; |
| 56 | template <template <typename...> class Template, typename... Args> struct IsSpecializationOfHelpercxx . . $ CppUtilities/Traits/Detail/IsSpecializationOfHelper#<Template<Args...>, Template> : Boolcxx . . $ CppUtilities/Traits/Bool#<true> {}; |
| 57 | } // namespace Detail |
| 58 | /// \endcond |
| 59 | /// \brief Evaluates to Bool<true> if the specified type is based on the specified template; otherwise evaluates to Bool<false>. |
| 60 | template <typename Type, template <typename...> class... TemplateTypes> |
| 61 | struct IsSpecializationOfcxx . . $ CppUtilities/Traits/IsSpecializationOf# |
| 62 | : Detailcxx . . $ CppUtilities/Traits/Detail/::IsSpecializationOfHelpercxx . . $ CppUtilities/Traits/Detail/IsSpecializationOfHelper#<typename stdcxx . . $ std/::remove_cv<typename std::remove_reference<Type>::type>::type, TemplateTypes...> {}; |
| 63 | /// \brief Evaluates to Bool<true> if the specified type is based on one of the specified templates; otherwise evaluates to Bool<false>. |
| 64 | template <typename Type, template <typename...> class... TemplateTypes> struct IsSpecializingAnyOfcxx . . $ CppUtilities/Traits/IsSpecializingAnyOf# : Boolcxx . . $ CppUtilities/Traits/Bool#<false> {}; |
| 65 | /// \brief Evaluates to Bool<true> if the specified type is based on one of the specified templates; otherwise evaluates to Bool<false>. |
| 66 | template <typename Type, template <typename...> class TemplateType, template <typename...> class... RemainingTemplateTypes> |
| 67 | struct IsSpecializingAnyOfcxx . . $ CppUtilities/Traits/IsSpecializingAnyOf#<Type, TemplateType, RemainingTemplateTypes...> |
| 68 | : Conditionalcxx . . $ CppUtilities/Traits/Conditional#<IsSpecializationOfcxx . . $ CppUtilities/Traits/IsSpecializationOf#<Type, TemplateType>, Boolcxx . . $ CppUtilities/Traits/Bool#<true>, IsSpecializingAnyOfcxx . . $ CppUtilities/Traits/IsSpecializingAnyOf#<Type, RemainingTemplateTypes...>> {}; |
| 69 | |
| 70 | /// \brief Evaluates to Bool<true> if the specified type is any of the specified types; otherwise evaluates to Bool<false>. |
| 71 | template <typename... T> struct IsAnyOfcxx . . $ CppUtilities/Traits/IsAnyOf# : Boolcxx . . $ CppUtilities/Traits/Bool#<false> {}; |
| 72 | /// \brief Evaluates to Bool<true> if the specified type is any of the specified types; otherwise evaluates to Bool<false>. |
| 73 | template <typename Type, typename OtherType, typename... RemainingTypes> |
| 74 | struct IsAnyOfcxx . . $ CppUtilities/Traits/IsAnyOf#<Type, OtherType, RemainingTypes...> : Conditionalcxx . . $ CppUtilities/Traits/Conditional#<stdcxx . . $ std/::is_same<Type, OtherType>, Boolcxx . . $ CppUtilities/Traits/Bool#<true>, IsAnyOfcxx . . $ CppUtilities/Traits/IsAnyOf#<Type, RemainingTypes...>> {}; |
| 75 | /// \brief Evaluates to Bool<true> if the specified type is none of the specified types; otherwise evaluates to Bool<false>. |
| 76 | template <typename... T> struct IsNoneOfcxx . . $ CppUtilities/Traits/IsNoneOf# : Boolcxx . . $ CppUtilities/Traits/Bool#<true> {}; |
| 77 | /// \brief Evaluates to Bool<true> if the specified type is none of the specified types; otherwise evaluates to Bool<false>. |
| 78 | template <typename Type, typename OtherType, typename... RemainingTypes> |
| 79 | struct IsNoneOfcxx . . $ CppUtilities/Traits/IsNoneOf#<Type, OtherType, RemainingTypes...> : Conditionalcxx . . $ CppUtilities/Traits/Conditional#<stdcxx . . $ std/::is_same<Type, OtherType>, Boolcxx . . $ CppUtilities/Traits/Bool#<false>, IsNoneOfcxx . . $ CppUtilities/Traits/IsNoneOf#<Type, RemainingTypes...>> {}; |
| 80 | |
| 81 | /// \brief Evaluates to Bool<true> if the specified type is a C-string (char * or const char *); otherwise evaluates to Bool<false>. |
| 82 | template <typename T> |
| 83 | struct IsCStringcxx . . $ CppUtilities/Traits/IsCString# |
| 84 | : Boolcxx . . $ CppUtilities/Traits/Bool#<stdcxx . . $ std/::is_same<char const *, typename std::decay<T>::type>::value || stdcxx . . $ std/::is_same<char *, typename std::decay<T>::type>::value> {}; |
| 85 | /// \brief Evaluates to Bool<true> if the specified type is a standard string, standard string view or C-string (char * or const char *); otherwise |
| 86 | /// evaluates to Bool<false>. |
| 87 | template <typename T> |
| 88 | struct IsStringcxx . . $ CppUtilities/Traits/IsString# |
| 89 | : Boolcxx . . $ CppUtilities/Traits/Bool#<IsCStringcxx . . $ CppUtilities/Traits/IsCString#<T>::value || IsSpecializationOfcxx . . $ CppUtilities/Traits/IsSpecializationOf#<T, std::basic_string>::value || IsSpecializationOfcxx . . $ CppUtilities/Traits/IsSpecializationOf#<T, std::basic_string_view>::value> {}; |
| 90 | |
| 91 | /// \brief Evaluates to Bool<true> if the specified type is complete; if the type is only forward-declared it evaluates to Bool<false>. |
| 92 | template <typename T, typename = void> struct IsCompletecxx . . $ CppUtilities/Traits/IsComplete# : Boolcxx . . $ CppUtilities/Traits/Bool#<false> {}; |
| 93 | /// \brief Evaluates to Bool<true> if the specified type is complete; if the type is only forward-declared it evaluates to Bool<false>. |
| 94 | template <typename T> struct IsCompletecxx . . $ CppUtilities/Traits/IsComplete#<T, decltype(void(sizeof(T)))> : Boolcxx . . $ CppUtilities/Traits/Bool#<true> {}; |
| 95 | |
| 96 | /*! |
| 97 | * \def CPP_UTILITIES_PP_COMMA |
| 98 | * \brief The CPP_UTILITIES_PP_COMMA macro helps passing "," as a macro argument. |
| 99 | */ |
| 100 | #define CPP_UTILITIES_PP_COMMAcxx . . $ `misc/traits.h:100:9`! , |
| 101 | |
| 102 | /*! |
| 103 | * \def CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK |
| 104 | * \brief The CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK macro defines a type trait for checking whether some operation can be done with |
| 105 | * a particular type. |
| 106 | * \sa Traits::HasSize or Traits::IsIteratable for an example how to use it. |
| 107 | */ |
| 108 | #define CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECKcxx . . $ `misc/traits.h:108:9`!(CheckName, CheckCode) \ |
| 109 | namespace Detail { \ |
| 110 | template <typename T> auto CheckName(int) -> decltype(CheckCode, ::CppUtilities::Traits::Bool<true>{}); \ |
| 111 | template <typename T>::CppUtilities::Traits::Bool<false> CheckName(...); \ |
| 112 | } \ |
| 113 | template <typename T> using CheckName = decltype(Detail::CheckName<T>(0)) |
| 114 | |
| 115 | /// \brief Evaluates to Bool<true> if the specified type can be dereferenced using the *-operator; otherwise evaluates to Bool<false>. |
| 116 | CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECKcxx . . $ CppUtilities/Traits/Detail/(IsDereferencable, *(std::declval<T &>())); |
| 117 | |
| 118 | /// \brief Evaluates to Bool<true> if the specified type can has a size() method; otherwise evaluates to Bool<false>. |
| 119 | CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECKcxx . . $ std/(HasSize, std::is_integral<decltype(std::declval<T &>().size())>::value); |
| 120 | |
| 121 | /// \brief Evaluates to Bool<true> if the specified type can has a reserve() method; otherwise evaluates to Bool<false>. |
| 122 | CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECKcxx . . $ CppUtilities/Traits/Detail/IsReservable(9b2f8b1448cd4048).(IsReservable, std::declval<T &>().reserve(0u)); |
| 123 | |
| 124 | /// \brief Evaluates to Bool<true> if the specified type can has a resize() method; otherwise evaluates to Bool<false>. |
| 125 | CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECKcxx . . $ CppUtilities/Traits/IsResizable#(IsResizable, std::declval<T &>().resize(0u)); |
| 126 | |
| 127 | /// \brief Evaluates to Bool<true> if the specified type has operator bool(); otherwise evaluates to Bool<false>. |
| 128 | CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECKcxx . . $ CppUtilities/Traits/Detail/HasOperatorBool(981a2665d23c1a52).(HasOperatorBool, std::declval<T &>() ? true : false); |
| 129 | |
| 130 | /// \brief Evaluates to Bool<true> if the specified type is iteratable (can be used in for-each loop); otherwise evaluates to Bool<false>. |
| 131 | CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECKcxx . . $ std/end(d31acd54edbd351d).(IsIteratable, |
| 132 | // begin/end and operator != |
| 133 | std::begin(std::declval<T &>()) |
| 134 | != std::end(std::declval<T &>()) CPP_UTILITIES_PP_COMMAcxx . . $ `misc/traits.h:100:9`! |
| 135 | // operator , |
| 136 | void() CPP_UTILITIES_PP_COMMAcxx . . $ `misc/traits.h:100:9`! |
| 137 | // operator ++ |
| 138 | ++ std::declval<decltype(begin(std::declval<T &>())) &>() CPP_UTILITIES_PP_COMMAcxx . . $ `misc/traits.h:100:9`! |
| 139 | // operator* |
| 140 | void(*begin(std::declval<T &>()))); |
| 141 | |
| 142 | /// \brief Dereferences the specified \a value if possible; otherwise just returns \a value itself. |
| 143 | template <typename T, EnableIfcxx . . $ CppUtilities/Traits/EnableIf#<IsDereferencablecxx . . $ CppUtilities/Traits/IsDereferencable#<T>> * = nullptr> constexpr auto &dereferenceMaybecxx . . $ CppUtilities/Traits/dereferenceMaybe(194cc22cf6406057).(T &&value) |
| 144 | { |
| 145 | return *value; |
| 146 | } |
| 147 | |
| 148 | /// \brief Dereferences the specified \a value if possible; otherwise just returns \a value itself. |
| 149 | template <typename T, DisableIfcxx . . $ CppUtilities/Traits/DisableIf#<IsDereferencablecxx . . $ CppUtilities/Traits/IsDereferencable#<T>> * = nullptr> constexpr auto &dereferenceMaybecxx . . $ CppUtilities/Traits/dereferenceMaybe(194cc22cf6406057).(T &&value) |
| 150 | { |
| 151 | return value; |
| 152 | } |
| 153 | |
| 154 | } // namespace Traits |
| 155 | |
| 156 | } // namespace CppUtilities |
| 157 | |
| 158 | #endif // CPP_UTILITIES_TRAITS_H |