|
| 1 | +#ifndef UTILITY_META_UTIL_H_INCLUDED |
| 2 | +#define UTILITY_META_UTIL_H_INCLUDED |
| 3 | + |
| 4 | +#include <type_traits> |
| 5 | + |
| 6 | +namespace JSON |
| 7 | +{ |
| 8 | + namespace internal |
| 9 | + { |
| 10 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 11 | +// PACK |
| 12 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 13 | + template <typename...> |
| 14 | + struct pack {}; |
| 15 | + |
| 16 | + template <typename Pack> |
| 17 | + struct pack_size; |
| 18 | + |
| 19 | + template <typename... List> |
| 20 | + struct pack_size <pack <List...>> { |
| 21 | + enum { |
| 22 | + value = sizeof...(List) |
| 23 | + }; |
| 24 | + }; |
| 25 | + |
| 26 | + template <std::size_t Index, typename Pack> |
| 27 | + struct pack_element; |
| 28 | + |
| 29 | + template <typename Head, typename... Tail> |
| 30 | + struct pack_element <0, pack <Head, Tail...>> { |
| 31 | + using type = Head; |
| 32 | + }; |
| 33 | + |
| 34 | + template <std::size_t Index, typename Head, typename... Tail> |
| 35 | + struct pack_element <Index, pack <Head, Tail...>> |
| 36 | + : pack_element <Index-1, pack <Tail...>> |
| 37 | + { |
| 38 | + }; |
| 39 | + |
| 40 | + template <typename, typename> |
| 41 | + struct concat |
| 42 | + { }; |
| 43 | + |
| 44 | + template <typename ... Ts, typename ... Us> |
| 45 | + struct concat <pack <Ts...>, pack <Us...>> { |
| 46 | + using type = pack <Ts..., Us...>; |
| 47 | + }; |
| 48 | + |
| 49 | + template <typename T, typename U> |
| 50 | + using concat_t = typename concat <T, U>::type; |
| 51 | + |
| 52 | + template <typename Pack> |
| 53 | + struct pop_back |
| 54 | + { }; |
| 55 | + |
| 56 | + template <> |
| 57 | + struct pop_back <pack <>> { |
| 58 | + using type = pack <>; |
| 59 | + }; |
| 60 | + |
| 61 | + template <typename T> |
| 62 | + struct pop_back <pack <T>> { |
| 63 | + using type = pack <>; |
| 64 | + }; |
| 65 | + |
| 66 | + template <typename T, typename ... List> |
| 67 | + struct pop_back <pack <T, List...>> { |
| 68 | + using type = concat_t <pack <T>, typename pop_back <pack <List...>>::type>; |
| 69 | + }; |
| 70 | + |
| 71 | + template <typename Pack> |
| 72 | + using pop_back_t = typename pop_back <Pack>::type; |
| 73 | + |
| 74 | + template <typename Pack, template <typename...> class Function> |
| 75 | + struct apply { }; |
| 76 | + |
| 77 | + template <template <typename...> class Function, typename... List> |
| 78 | + struct apply <pack <List...>, Function> { |
| 79 | + using type = Function <List...>; |
| 80 | + }; |
| 81 | + |
| 82 | + template <typename Pack, template <typename...> class Function> |
| 83 | + using apply_t = typename apply <Pack, Function>::type; |
| 84 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 85 | +// TRIM |
| 86 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 87 | + template <typename Pack, template <typename> class Predicate, bool Abort = false> |
| 88 | + struct trim_right { |
| 89 | + // Abort == false |
| 90 | + using type = typename std::conditional <Predicate<typename pack_element <pack_size <Pack>::value - 1, Pack>::type>::value, |
| 91 | + typename trim_right <pop_back_t<Pack>, Predicate>::type, |
| 92 | + Pack>::type; |
| 93 | + }; |
| 94 | + |
| 95 | + template <typename Pack, template <typename> class Predicate> |
| 96 | + struct trim_right <Pack, Predicate, true> { |
| 97 | + using type = Pack; |
| 98 | + }; |
| 99 | + |
| 100 | + template <template <typename> class Predicate> |
| 101 | + struct trim_right <pack <>, Predicate, false> { |
| 102 | + using type = pack <>; |
| 103 | + }; |
| 104 | + |
| 105 | + template <typename Pack, template <typename> class Predicate> |
| 106 | + using trim_right_t = typename trim_right <Pack, Predicate>::type; |
| 107 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 108 | +// CEXPR_STRING |
| 109 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 110 | + template <char C> |
| 111 | + using char_ = std::integral_constant <char, C>; |
| 112 | + |
| 113 | + template <typename... List> |
| 114 | + struct cexpr_string |
| 115 | + { |
| 116 | + using size_type = std::make_signed_t <std::size_t>; |
| 117 | + |
| 118 | + using type = pack <List...>; |
| 119 | + constexpr static const size_type size = sizeof...(List); |
| 120 | + constexpr static const size_type length = size; |
| 121 | + |
| 122 | + template <size_type N> |
| 123 | + struct at { |
| 124 | + static_assert(N < size, "Index out of bounds"); |
| 125 | + |
| 126 | + constexpr static const char value = pack_element <N, type>::type::value; |
| 127 | + using type = typename pack_element <N, type>::type; |
| 128 | + }; |
| 129 | + |
| 130 | + template <size_type N> |
| 131 | + using at_t = typename at <N>::type; |
| 132 | + |
| 133 | + constexpr static const char c_str[] = { |
| 134 | + List::value..., '\0' |
| 135 | + }; |
| 136 | + |
| 137 | + constexpr static const size_type npos = -1; |
| 138 | + }; |
| 139 | + |
| 140 | + template <typename... List> |
| 141 | + constexpr const char cexpr_string <List...>::c_str[]; |
| 142 | + |
| 143 | + template <typename Char> |
| 144 | + struct is_zero { |
| 145 | + constexpr static const bool value = (Char::value == 0); |
| 146 | + }; |
| 147 | + |
| 148 | + #define SPLIT_1(s, x) JSON::internal::char_<( x < sizeof(s) ? s[x] : '\0' )> |
| 149 | + #define SPLIT_4(s, x) SPLIT_1 (s, x), SPLIT_1 (s, x+1) , SPLIT_1 (s, x+2) , SPLIT_1 (s, x+3) |
| 150 | + #define SPLIT_16(s, x) SPLIT_4 (s, x), SPLIT_4 (s, x+4) , SPLIT_4 (s, x+8) , SPLIT_4 (s, x+12) |
| 151 | + #define SPLIT_64(s, x) SPLIT_16 (s, x), SPLIT_16 (s, x+16) , SPLIT_16 (s, x+32) , SPLIT_16 (s, x+48) |
| 152 | + #define SPLIT_256(s, x) SPLIT_64 (s, x), SPLIT_64 (s, x+64) , SPLIT_64 (s, x+128) , SPLIT_64 (s, x+194) |
| 153 | + #define SPLIT_1024(s, x) SPLIT_256(s, x), SPLIT_256(s, x+256), SPLIT_256(s, x+512), SPLIT_256(s, x+768) |
| 154 | + |
| 155 | + #define STRING_IMPL(str, n) JSON::internal::apply_t <JSON::internal::trim_right_t <JSON::internal::pack <SPLIT_##n(str, 0)>, JSON::internal::is_zero>, JSON::internal::cexpr_string> |
| 156 | + |
| 157 | + #define SHORT_STRING(str) STRING_IMPL(str, 16) |
| 158 | + #define STRING(str) STRING_IMPL(str, 64) |
| 159 | + #define LONG_STRING(str) STRING_IMPL(str, 256) // HEAVY |
| 160 | + #define LONG_LONG_STRING(str) STRING_IMPL(str, 1024) // ULTRA HEAVY |
| 161 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +#endif // UTILITY_META_UTIL_H_INCLUDED |
0 commit comments