diff --git a/src/mpp/Constants.hpp b/src/mpp/Constants.hpp index e4bc8f035..a953c012e 100644 --- a/src/mpp/Constants.hpp +++ b/src/mpp/Constants.hpp @@ -36,7 +36,6 @@ namespace mpp { -namespace compact { enum Family : uint8_t { MP_NIL /* = 0x00 */, MP_IGNR /* = 0x01 */, @@ -50,43 +49,6 @@ enum Family : uint8_t { MP_EXT /* = 0x09 */, MP_END }; -} // namespace compact { - -using FamilyUnder_t = uint32_t; -enum Family : FamilyUnder_t { - MP_NIL = 1u << compact::MP_NIL, - MP_IGNR = 1u << compact::MP_IGNR, - MP_BOOL = 1u << compact::MP_BOOL, - MP_INT = 1u << compact::MP_INT, - MP_FLT = 1u << compact::MP_FLT, - MP_STR = 1u << compact::MP_STR, - MP_BIN = 1u << compact::MP_BIN, - MP_ARR = 1u << compact::MP_ARR, - MP_MAP = 1u << compact::MP_MAP, - MP_EXT = 1u << compact::MP_EXT, - MP_NUM = (1u << compact::MP_INT) | (1u << compact::MP_FLT), - MP_NONE = 0, - MP_ANY = std::numeric_limits::max(), -}; - -enum ReadError_t { - READ_ERROR_NEED_MORE, - READ_ERROR_BAD_MSGPACK, - READ_ERROR_WRONG_TYPE, - READ_ERROR_MAX_DEPTH_REACHED, - READ_ERROR_ABORTED_BY_USER, - READ_ERROR_END -}; - -enum ReadResult_t : FamilyUnder_t { - READ_SUCCESS = 0, - READ_NEED_MORE = 1u << READ_ERROR_NEED_MORE, - READ_BAD_MSGPACK = 1u << READ_ERROR_BAD_MSGPACK, - READ_WRONG_TYPE = 1u << READ_ERROR_WRONG_TYPE, - READ_MAX_DEPTH_REACHED = 1u << READ_ERROR_MAX_DEPTH_REACHED, - READ_ABORTED_BY_USER = 1u << READ_ERROR_ABORTED_BY_USER, - READ_RESULT_END -}; inline const char *FamilyName[] = { "MP_NIL", @@ -102,7 +64,7 @@ inline const char *FamilyName[] = { "MP_BAD", "MP_NONE" }; -static_assert(std::size(FamilyName) == compact::MP_END + 2, "Smth is forgotten"); +static_assert(std::size(FamilyName) == MP_END + 2, "Smth is forgotten"); inline const char *FamilyHumanName[] = { "nil", @@ -118,84 +80,18 @@ inline const char *FamilyHumanName[] = { "bad", "none" }; -static_assert(std::size(FamilyHumanName) == compact::MP_END + 2, "Smth is forgotten"); - -inline const char *ReadErrorName[] = { - "READ_ERROR_NEED_MORE", - "READ_ERROR_BAD_MSGPACK", - "READ_ERROR_WRONG_TYPE", - "READ_ERROR_MAX_DEPTH_REACHED", - "READ_ERROR_ABORTED_BY_USER", - "READ_ERROR_UNKNOWN", - "READ_SUCCESS", -}; -static_assert(std::size(ReadErrorName) == READ_ERROR_END + 2, "Forgotten"); - -inline constexpr Family -operator|(Family a, Family b) -{ - return static_cast(static_cast(a) | - static_cast(b)); -} - -inline constexpr Family -operator&(Family a, Family b) -{ - return static_cast(static_cast(a) & - static_cast(b)); -} - -inline constexpr ReadResult_t -operator|(ReadResult_t a, ReadResult_t b) -{ - return static_cast(static_cast(a) | - static_cast(b)); -} - -inline constexpr ReadResult_t -operator&(ReadResult_t a, ReadResult_t b) -{ - return static_cast(static_cast(a) & - static_cast(b)); -} - -inline constexpr ReadResult_t -operator~(ReadResult_t a) -{ - return static_cast(~static_cast(a)); -} +static_assert(std::size(FamilyHumanName) == MP_END + 2, "Smth is forgotten"); inline std::ostream& -operator<<(std::ostream& strm, compact::Family t) +operator<<(std::ostream& strm, Family t) { - if (t >= compact::Family::MP_END) - return strm << FamilyName[compact::Family::MP_END] + if (t >= Family::MP_END) + return strm << FamilyName[Family::MP_END] << "(" << static_cast(t) << ")"; return strm << FamilyName[t]; } -inline std::ostream& -operator<<(std::ostream& strm, Family t) -{ - if (t == MP_NONE) - return strm << FamilyName[compact::Family::MP_END + 1]; - static_assert(sizeof(FamilyUnder_t) == sizeof(t), "Very wrong"); - FamilyUnder_t base = t; - bool first = true; - do { - static_assert(sizeof(unsigned) == sizeof(t), "Wrong ctz"); - unsigned part = __builtin_ctz(base); - base ^= 1u << part; - if (first) - first = false; - else - strm << "|"; - strm << static_cast(part); - } while (base != 0); - return strm; -} - -template +template struct family_sequence { static constexpr std::size_t size() noexcept { @@ -203,13 +99,13 @@ struct family_sequence { } }; -template +template static constexpr auto family_sequence_populate(struct family_sequence) { return family_sequence{}; } -template +template inline constexpr auto operator+(family_sequence, family_sequence) { @@ -218,90 +114,54 @@ operator+(family_sequence, family_sequence) namespace details { -template +template struct family_sequence_contains_impl_h { static constexpr bool value = family_sequence_contains_impl_h::value; }; -template +template struct family_sequence_contains_impl_h { static constexpr bool value = false; }; -template +template struct family_sequence_contains_impl_h { static constexpr bool value = true; }; -template +template struct family_sequence_contains_impl_h { static constexpr bool value = true; }; -template +template struct family_sequence_contains_h { static constexpr bool value = family_sequence_contains_impl_h::value; }; -template +template struct family_sequence_contains_h { static constexpr bool value = false; }; -} //namespace details +} // namespace details -template +template static constexpr bool family_sequence_contains(family_sequence) { return details::family_sequence_contains_h::value; } -template +template std::ostream& operator<<(std::ostream& strm, family_sequence) { if (sizeof ...(FAMILY) == 0) - return strm << FamilyName[compact::Family::MP_END + 1]; + return strm << FamilyName[Family::MP_END + 1]; size_t count = 0; ((strm << (count++ ? ", " : "") << FamilyName[FAMILY]), ...); return strm; } -inline std::ostream& -operator<<(std::ostream& strm, ReadError_t t) -{ - if (t >= READ_ERROR_END) - return strm << ReadErrorName[READ_ERROR_END] - << "(" << static_cast(t) << ")"; - return strm << ReadErrorName[t]; -} - -inline std::ostream& -operator<<(std::ostream& strm, ReadResult_t t) -{ - if (t == READ_SUCCESS) - return strm << ReadErrorName[READ_ERROR_END + 1]; - static_assert(sizeof(FamilyUnder_t) == sizeof(t), "Very wrong"); - FamilyUnder_t base = t; - bool first = true; - do { - static_assert(sizeof(unsigned) == sizeof(t), "Wrong ctz"); - unsigned part = __builtin_ctz(base); - base ^= 1u << part; - if (first) - first = false; - else - strm << "|"; - strm << static_cast(part); - } while (base != 0); - return strm; -} - -struct StrValue { uint32_t offset; uint32_t size; }; -struct BinValue { uint32_t offset; uint32_t size; }; -struct ArrValue { uint32_t offset; uint32_t size; }; -struct MapValue { uint32_t offset; uint32_t size; }; -struct ExtValue { int8_t type; uint8_t offset; uint32_t size; }; - -} // namespace mpp { +} // namespace mpp diff --git a/src/mpp/Dec.hpp b/src/mpp/Dec.hpp index 76f9c0f0b..1666c77d7 100644 --- a/src/mpp/Dec.hpp +++ b/src/mpp/Dec.hpp @@ -131,33 +131,33 @@ constexpr auto detectFamily() } else if constexpr (has_dec_rule_v) { return detectFamily())>(); } else if constexpr (tnt::is_optional_v) { - return family_sequence_populate( + return family_sequence_populate( detectFamily>()); } else if constexpr (tnt::is_variant_v) { return detectFamilyVariant(); } else if constexpr (std::is_convertible_v) { - return family_sequence{}; + return family_sequence{}; } else if constexpr (std::is_same_v) { - return family_sequence{}; + return family_sequence{}; } else if constexpr (tnt::is_integer_v) { - return family_sequence{}; + return family_sequence{}; } else if constexpr (std::is_floating_point_v) { - return family_sequence{}; + return family_sequence{}; } else if constexpr (tnt::is_contiguous_char_v) { - return family_sequence{}; + return family_sequence{}; } else if constexpr (tnt::is_tuplish_v) { if constexpr (tnt::tuple_size_v == 0) - return family_sequence{}; + return family_sequence{}; else if constexpr (tnt::is_tuplish_of_pairish_v) - return family_sequence{}; + return family_sequence{}; else - return family_sequence{}; + return family_sequence{}; } else if constexpr (is_any_putable_v || tnt::is_contiguous_v) { if constexpr (tnt::is_pairish_v>) - return family_sequence{}; + return family_sequence{}; else - return family_sequence{}; + return family_sequence{}; } else { static_assert(tnt::always_false_v, "Failed to recognise type"); @@ -165,7 +165,7 @@ constexpr auto detectFamily() } } -template +template constexpr bool hasChildren(family_sequence) { return (rule_by_family_t::has_children || ...); @@ -393,7 +393,7 @@ constexpr auto&& path_resolve_parent(tnt::iseq, T... t) constexpr size_t SIMPLEX_SUBRULE = 16; -template +template constexpr auto get_subrules() { using RULE = rule_by_family_t; @@ -405,7 +405,7 @@ constexpr auto get_subrules() return complex_seq{}; } -template +template auto read_value(BUF& buf) { using RULE = rule_by_family_t; @@ -417,7 +417,7 @@ auto read_value(BUF& buf) [[maybe_unused]] typename RULE::simplex_value_t val = tag - RULE::simplex_tag; - if constexpr (FAMILY == compact::MP_NIL) + if constexpr (FAMILY == MP_NIL) return tnt::empty_value; else if constexpr (RULE::is_bool) return bool(val); @@ -438,7 +438,7 @@ auto read_value(BUF& buf) } } -template +template auto read_item(BUF& buf, ITEM& item) { using RULE = rule_by_family_t; @@ -481,7 +481,7 @@ auto read_item(BUF& buf, ITEM& item) item.resize(val); } else if constexpr (std::is_enum_v) { item = static_cast(val); - } else if constexpr (tnt::is_optional_v && FAMILY == compact::MP_NIL) { + } else if constexpr (tnt::is_optional_v && FAMILY == MP_NIL) { item.reset(); } else { item = val; @@ -580,7 +580,7 @@ struct Jumps { } }; -template bool jump_common(BUF& buf, T... t); @@ -592,7 +592,7 @@ bool broken_msgpack_jump(BUF&, T...); template struct JumpsBuilder { - template + template static constexpr auto build() { using RULE = rule_by_family_t; @@ -609,13 +609,13 @@ struct JumpsBuilder { } } - template + template static constexpr auto build(tnt::iseq) { return (build() + ...); } - template + template static constexpr auto build(family_sequence s) { if constexpr (s.size() == 0) @@ -665,11 +665,11 @@ struct JumpsBuilder { constexpr bool has_str = (is_str_key>() || ...); if constexpr (has_int && has_str) - return family_sequence{}; + return family_sequence{}; else if constexpr (has_int) - return family_sequence{}; + return family_sequence{}; else if constexpr (has_str) - return family_sequence{}; + return family_sequence{}; else return family_sequence<>{}; } @@ -912,7 +912,7 @@ bool decode_next(BUF& buf, T... t) } } -template bool jump_skip(BUF& buf, T... t) { @@ -934,7 +934,7 @@ bool jump_skip(BUF& buf, T... t) return decode_next(buf, t...); } -template bool jump_add(BUF& buf, T... t) { @@ -981,7 +981,7 @@ constexpr size_t get_next_arr_static_size() return 0; } -template bool jump_read(BUF& buf, T... t) { @@ -994,7 +994,7 @@ bool jump_read(BUF& buf, T... t) return decode_next(buf, t...); } - if constexpr (FAMILY == compact::MP_ARR) { + if constexpr (FAMILY == MP_ARR) { constexpr auto NT = get_next_arr_item_type(); constexpr size_t NS = get_next_arr_static_size(); using NEXT_PATH = path_push_t; @@ -1028,7 +1028,7 @@ bool jump_read(BUF& buf, T... t) uint64_t arg = val; return decode_impl(buf, t..., arg); } - } else if constexpr (FAMILY == compact::MP_MAP) { + } else if constexpr (FAMILY == MP_MAP) { if constexpr (tnt::is_tuplish_v) { uint64_t arg = val; using NEXT_PATH = path_push_t; @@ -1072,14 +1072,14 @@ constexpr bool signed_compare(K k, V v) } } -template +template constexpr bool compare_key([[maybe_unused]] K k, W&& w, [[maybe_unused]] BUF& buf) { auto&& u = mpp::unwrap(w); using U = mpp::unwrap_t; static_assert(std::is_integral_v); static_assert(!std::is_same_v); - if constexpr (FAMILY == compact::MP_INT) { + if constexpr (FAMILY == MP_INT) { if constexpr (tnt::is_integral_constant_v) { using V = typename U::value_type; constexpr tnt::base_enum_t v = U::value; @@ -1091,7 +1091,7 @@ constexpr bool compare_key([[maybe_unused]] K k, W&& w, [[maybe_unused]] BUF& bu return false; } } else { - static_assert(FAMILY == compact::MP_STR); + static_assert(FAMILY == MP_STR); if constexpr (tnt::is_string_constant_v) { return k == u.size && buf.startsWith({u.data, u.size}); } else if constexpr (tnt::is_char_ptr_v) { @@ -1109,13 +1109,13 @@ constexpr bool compare_key([[maybe_unused]] K k, W&& w, [[maybe_unused]] BUF& bu } } -template bool jump_find_key([[maybe_unused]] K k, tnt::iseq<>, BUF& buf, T... t) { static_assert(path_item_type(PATH::last()) == PIT_DYN_KEY); using NEXT_PATH = path_push_t; - if constexpr (FAMILY == compact::MP_STR) + if constexpr (FAMILY == MP_STR) buf.read({k}); return decode_impl(buf, t..., size_t(1)); } @@ -1129,7 +1129,7 @@ auto&& key_path_resolve(DST&& dst) return tnt::get(dst); } -template bool jump_find_key(K k, tnt::iseq, BUF& buf, T... t) { @@ -1142,7 +1142,7 @@ bool jump_find_key(K k, tnt::iseq, BUF& buf, T... t) using NEXT_PATH = std::conditional_t; if (compare_key(k, key, buf)) { - if constexpr (FAMILY == compact::MP_STR) + if constexpr (FAMILY == MP_STR) buf.read({k}); return decode_impl(buf, t...); } @@ -1151,7 +1151,7 @@ bool jump_find_key(K k, tnt::iseq, BUF& buf, T... t) return jump_find_key(k, IS, buf, t...); } -template bool jump_read_key(BUF& buf, T... t) { @@ -1166,24 +1166,24 @@ bool jump_read_key(BUF& buf, T... t) constexpr size_t S = PAIRS ? TS : TS / 2; constexpr auto IS = tnt::make_iseq{}; - static_assert(FAMILY == compact::MP_INT || FAMILY == compact::MP_STR); + static_assert(FAMILY == MP_INT || FAMILY == MP_STR); auto val = read_value(buf); return jump_find_key(val, IS, buf, t...); } -template +template constexpr bool is_object_readable_by_value_v = rule_by_family_t::is_readable_by_value && std::is_assignable_v(std::declval()))>; -template bool jump_read_optional(BUF& buf, T... t) { auto&& dst = unwrap(path_resolve(PATH{}, t...)); using dst_t = std::remove_reference_t; static_assert(tnt::is_optional_v); - if constexpr (FAMILY == compact::MP_NIL) { + if constexpr (FAMILY == MP_NIL) { [[maybe_unused]] auto val = read_value(buf); dst.reset(); return decode_next(buf, t...); @@ -1200,7 +1200,7 @@ bool jump_read_optional(BUF& buf, T... t) } } -template bool jump_read_variant_impl(BUF& buf, T... t) { @@ -1221,7 +1221,7 @@ bool jump_read_variant_impl(BUF& buf, T... t) } } -template bool jump_read_variant(BUF& buf, T... t) { @@ -1236,7 +1236,7 @@ bool jump_read_variant(BUF& buf, T... t) } } -template bool jump_common(BUF& buf, T... t) { diff --git a/src/mpp/Enc.hpp b/src/mpp/Enc.hpp index eaf732cbc..d5ea2623b 100644 --- a/src/mpp/Enc.hpp +++ b/src/mpp/Enc.hpp @@ -68,7 +68,7 @@ namespace encode_details { struct Nothing {}; template -constexpr compact::Family detectFamily() +constexpr Family detectFamily() { using fixed_t = get_fixed_t; constexpr bool is_non_void_fixed = !std::is_same_v; @@ -77,33 +77,33 @@ constexpr compact::Family detectFamily() if constexpr (is_wrapped_family_v) { return T::family; } else if constexpr (std::is_convertible_v) { - return compact::MP_NIL; + return MP_NIL; } else if constexpr (std::is_same_v) { - return compact::MP_BOOL; + return MP_BOOL; } else if constexpr (tnt::is_integer_v) { - return compact::MP_INT; + return MP_INT; } else if constexpr (std::is_floating_point_v) { - return compact::MP_FLT; + return MP_FLT; } else if constexpr (tnt::is_string_constant_v) { - return compact::MP_STR; + return MP_STR; } else if constexpr (tnt::is_char_ptr_v) { - return compact::MP_STR; + return MP_STR; } else if constexpr (tnt::is_contiguous_char_v) { - return compact::MP_STR; + return MP_STR; } else if constexpr (tnt::is_const_pairs_iterable_v) { - return compact::MP_MAP; + return MP_MAP; } else if constexpr (tnt::is_const_iterable_v) { - return compact::MP_ARR; + return MP_ARR; } else if constexpr (tnt::is_tuplish_of_pairish_v) { if constexpr(tnt::tuple_size_v == 0) - return compact::MP_ARR; + return MP_ARR; else - return compact::MP_MAP; + return MP_MAP; } else if constexpr (tnt::is_tuplish_v) { - return compact::MP_ARR; + return MP_ARR; } else { static_assert(tnt::always_false_v, "Failed to recognise type"); - return compact::MP_END; + return MP_END; } } @@ -146,10 +146,10 @@ auto uniSize32([[maybe_unused]]const U& u) } } -template +template auto getValue([[maybe_unused]] const T& t, [[maybe_unused]] const U& u) { - if constexpr (FAMILY == compact::MP_STR) { + if constexpr (FAMILY == MP_STR) { static_assert(tnt::is_char_ptr_v || tnt::is_string_constant_v || tnt::is_contiguous_char_v); @@ -162,8 +162,8 @@ auto getValue([[maybe_unused]] const T& t, [[maybe_unused]] const U& u) tnt::is_contiguous_char_v); return uniLength32(u); } - } else if constexpr (FAMILY == compact::MP_BIN || - FAMILY == compact::MP_EXT) { + } else if constexpr (FAMILY == MP_BIN || + FAMILY == MP_EXT) { if constexpr(tnt::is_string_constant_v || tnt::is_contiguous_v) { return uniLength32(u); @@ -171,9 +171,9 @@ auto getValue([[maybe_unused]] const T& t, [[maybe_unused]] const U& u) static_assert(std::is_standard_layout_v); return std::integral_constant{}; } - } else if constexpr (FAMILY == compact::MP_ARR) { + } else if constexpr (FAMILY == MP_ARR) { return uniSize32(u); - } else if constexpr (FAMILY == compact::MP_MAP) { + } else if constexpr (FAMILY == MP_MAP) { if constexpr(tnt::is_tuplish_of_pairish_v || tnt::is_const_pairs_iterable_v) { return uniSize32(u); @@ -191,10 +191,10 @@ auto getValue([[maybe_unused]] const T& t, [[maybe_unused]] const U& u) } } -template +template auto getExtType([[maybe_unused]] const T& t, [[maybe_unused]] const U& u) { - if constexpr (FAMILY == compact::MP_EXT) { + if constexpr (FAMILY == MP_EXT) { using E = std::remove_cv_t; if constexpr(tnt::is_integral_constant_v) { using V = std::remove_cv_t; @@ -234,11 +234,11 @@ template struct ChildrenPairs : ChildrenPairsTag { explicit ChildrenPairs(const V& u) : v(u) {} }; -template +template auto getData([[maybe_unused]] const T& t, [[maybe_unused]] const U& u, [[maybe_unused]] const V& value) { - if constexpr (FAMILY == compact::MP_STR) { + if constexpr (FAMILY == MP_STR) { if constexpr (tnt::is_char_ptr_v || tnt::is_bounded_array_v) { using check0_t = decltype(u[0]); @@ -258,8 +258,8 @@ auto getData([[maybe_unused]] const T& t, [[maybe_unused]] const U& u, return std::string_view{std::data(u), std::size(u)}; } - } else if constexpr (FAMILY == compact::MP_BIN || - FAMILY == compact::MP_EXT) { + } else if constexpr (FAMILY == MP_BIN || + FAMILY == MP_EXT) { if constexpr(tnt::is_string_constant_v) { return u; } else if constexpr(tnt::is_contiguous_v) { @@ -270,9 +270,9 @@ auto getData([[maybe_unused]] const T& t, [[maybe_unused]] const U& u, auto p = reinterpret_cast(&u); return std::string_view{p, value}; } - } else if constexpr (FAMILY == compact::MP_ARR) { + } else if constexpr (FAMILY == MP_ARR) { return Children{u}; - } else if constexpr (FAMILY == compact::MP_MAP) { + } else if constexpr (FAMILY == MP_MAP) { if constexpr(tnt::is_tuplish_of_pairish_v || tnt::is_const_pairs_iterable_v) return ChildrenPairs{u}; @@ -283,10 +283,10 @@ auto getData([[maybe_unused]] const T& t, [[maybe_unused]] const U& u, } } -template +template auto getIS([[maybe_unused]] const T& t, [[maybe_unused]] const U& u) { - if constexpr (FAMILY == compact::MP_ARR || FAMILY == compact::MP_MAP) { + if constexpr (FAMILY == MP_ARR || FAMILY == MP_MAP) { if constexpr(tnt::is_tuplish_v) { return tnt::tuple_iseq{}; } else { @@ -357,8 +357,8 @@ constexpr bool can_encode_simple() template constexpr auto getTagValSimple([[maybe_unused]] V value) { - if constexpr(RULE::family == compact::MP_NIL || - RULE::family == compact::MP_IGNR) { + if constexpr(RULE::family == MP_NIL || + RULE::family == MP_IGNR) { // That type is completely independent on value static_assert(!IS_FIXED || std::is_same_v); constexpr char t = static_cast(RULE::simplex_tag); @@ -654,7 +654,7 @@ encode(CONT &cont, tnt::CStr prefix, } else { constexpr bool is_fixed = is_wrapped_fixed_v; using fixed_t = get_fixed_t; - constexpr compact::Family family = detectFamily(); + constexpr Family family = detectFamily(); using rule_t = rule_by_family_t; auto value = getValue(t, u); using V = decltype(value); diff --git a/src/mpp/Rules.hpp b/src/mpp/Rules.hpp index f3a55f601..0db252214 100644 --- a/src/mpp/Rules.hpp +++ b/src/mpp/Rules.hpp @@ -108,55 +108,55 @@ struct RuleRange { count(last - first + 1) {} }; -template +template struct BaseRule { // Widest types that can represent the value. using types = std::tuple; // Msgpack family. - static constexpr compact::Family family = FAMILY; + static constexpr Family family = FAMILY; // The rule stores bool values. - static constexpr bool is_bool = FAMILY == compact::MP_BOOL; + static constexpr bool is_bool = FAMILY == MP_BOOL; // The rule stores floating point values. - static constexpr bool is_floating_point = FAMILY == compact::MP_FLT; + static constexpr bool is_floating_point = FAMILY == MP_FLT; // The rule actually doest not store value, only type matters. - static constexpr bool is_valueless = FAMILY == compact::MP_NIL || - FAMILY == compact::MP_IGNR; + static constexpr bool is_valueless = FAMILY == MP_NIL || + FAMILY == MP_IGNR; // The encoded object has data section (of size equal to value). - static constexpr bool has_data = FAMILY == compact::MP_STR || - FAMILY == compact::MP_BIN || - FAMILY == compact::MP_EXT; + static constexpr bool has_data = FAMILY == MP_STR || + FAMILY == MP_BIN || + FAMILY == MP_EXT; // The encoded object has ext type byte. - static constexpr bool has_ext = FAMILY == compact::MP_EXT; + static constexpr bool has_ext = FAMILY == MP_EXT; // The encoded object has children... - static constexpr bool has_children = FAMILY == compact::MP_ARR || - FAMILY == compact::MP_MAP; + static constexpr bool has_children = FAMILY == MP_ARR || + FAMILY == MP_MAP; // ... and the number of children is value * children_multiplier. static constexpr uint32_t children_multiplier = - FAMILY == compact::MP_ARR ? 1 : - FAMILY == compact::MP_MAP ? 2 : 0; + FAMILY == MP_ARR ? 1 : + FAMILY == MP_MAP ? 2 : 0; // The encoded object can be read by value. static constexpr bool is_readable_by_value = !has_data && !has_ext && !has_children; // The rule has simplex form. - static constexpr bool has_simplex = FAMILY == compact::MP_NIL || - FAMILY == compact::MP_IGNR || - FAMILY == compact::MP_BOOL || - FAMILY == compact::MP_INT || - FAMILY == compact::MP_STR || - FAMILY == compact::MP_ARR || - FAMILY == compact::MP_MAP || - FAMILY == compact::MP_EXT; + static constexpr bool has_simplex = FAMILY == MP_NIL || + FAMILY == MP_IGNR || + FAMILY == MP_BOOL || + FAMILY == MP_INT || + FAMILY == MP_STR || + FAMILY == MP_ARR || + FAMILY == MP_MAP || + FAMILY == MP_EXT; // The rule has simplex form. - static constexpr bool has_complex = FAMILY == compact::MP_INT || - FAMILY == compact::MP_FLT || - FAMILY == compact::MP_STR || - FAMILY == compact::MP_BIN || - FAMILY == compact::MP_ARR || - FAMILY == compact::MP_MAP || - FAMILY == compact::MP_EXT; + static constexpr bool has_complex = FAMILY == MP_INT || + FAMILY == MP_FLT || + FAMILY == MP_STR || + FAMILY == MP_BIN || + FAMILY == MP_ARR || + FAMILY == MP_MAP || + FAMILY == MP_EXT; // The rule has signed simplex range. - static constexpr bool is_simplex_signed = FAMILY == compact::MP_INT; + static constexpr bool is_simplex_signed = FAMILY == MP_INT; // The rule has logarithmic simplex range. - static constexpr bool is_simplex_log_range = FAMILY == compact::MP_EXT; + static constexpr bool is_simplex_log_range = FAMILY == MP_EXT; // The type of simplex value in simplex range. using simplex_value_t = std::conditional_t; @@ -164,22 +164,22 @@ struct BaseRule { using simplex_value_range_t = RuleRange; }; -struct NilRule : BaseRule { +struct NilRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {0, 0}; static constexpr uint8_t simplex_tag = 0xc0; }; -struct IgnrRule : BaseRule { +struct IgnrRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {0, 0}; static constexpr uint8_t simplex_tag = 0xc1; }; -struct BoolRule : BaseRule { +struct BoolRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {0, 1}; static constexpr uint8_t simplex_tag = 0xc2; }; -struct IntRule : BaseRule { +struct IntRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {-32, 127}; static constexpr uint8_t simplex_tag = 0x00; using complex_types = std::tuple { static constexpr uint8_t complex_tag = 0xcc; }; -struct FltRule : BaseRule { +struct FltRule : BaseRule { using complex_types = std::tuple; static constexpr uint8_t complex_tag = 0xca; }; -struct StrRule : BaseRule { +struct StrRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {0, 31}; static constexpr uint8_t simplex_tag = 0xa0; using complex_types = std::tuple; static constexpr uint8_t complex_tag = 0xd9; }; -struct BinRule : BaseRule { +struct BinRule : BaseRule { using complex_types = std::tuple; static constexpr uint8_t complex_tag = 0xc4; }; -struct ArrRule : BaseRule { +struct ArrRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {0, 15}; static constexpr uint8_t simplex_tag = 0x90; using complex_types = std::tuple; static constexpr uint8_t complex_tag = 0xdc; }; -struct MapRule : BaseRule { +struct MapRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {0, 15}; static constexpr uint8_t simplex_tag = 0x80; using complex_types = std::tuple; static constexpr uint8_t complex_tag = 0xde; }; -struct ExtRule : BaseRule { +struct ExtRule : BaseRule { static constexpr simplex_value_range_t simplex_value_range = {0, 4}; static constexpr uint8_t simplex_tag = 0xd4; using complex_types = std::tuple; @@ -229,9 +229,9 @@ using all_rules_t = std::tuple; /** - * Find a rule by compact::Family. + * Find a rule by Family. */ -template +template using rule_by_family_t = std::tuple_element_t; /** @@ -397,7 +397,7 @@ constexpr size_t rule_complex_apply(E eval, F &&f) static_assert(RULE::has_complex); auto val = details::rule_unenum(eval); using V = decltype(val); - constexpr bool is_int = RULE::family == compact::MP_INT; + constexpr bool is_int = RULE::family == MP_INT; constexpr size_t N = rule_complex_count_v; if constexpr (is_int && std::is_signed_v) { static_assert(N == 8); diff --git a/src/mpp/Spec.hpp b/src/mpp/Spec.hpp index 82edf624e..b730c56d2 100644 --- a/src/mpp/Spec.hpp +++ b/src/mpp/Spec.hpp @@ -45,18 +45,18 @@ namespace mpp { * use temporary specificators, like encoder.add(mpp::as_map()). */ -template +template constexpr auto as_family(T&& t); -template constexpr auto as_nil (T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_ignr(T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_bool(T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_int (T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_flt (T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_str (T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_bin (T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_arr (T&& t) { return as_family(std::forward(t)); } -template constexpr auto as_map (T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_nil (T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_ignr(T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_bool(T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_int (T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_flt (T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_str (T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_bin (T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_arr (T&& t) { return as_family(std::forward(t)); } +template constexpr auto as_map (T&& t) { return as_family(std::forward(t)); } template constexpr auto as_ext(EXT_TYPE&& e, T&& t); @@ -97,20 +97,20 @@ struct wrapped : wrapped_tag constexpr explicit wrapped(T&& arg) : object(std::forward(arg)) {} }; -template +template struct wrapped_family : wrapped_family_tag, BASE { - static constexpr compact::Family family = FAMILY; + static constexpr Family family = FAMILY; constexpr explicit wrapped_family(BASE&& arg) : BASE(std::forward(arg)) {} }; -template +template constexpr auto as_family(T&& t) { static_assert(!is_wrapped_family_v, "Family is already set"); static_assert(!is_wrapped_raw_v, "Incompatible with raw"); - static_assert(FAMILY < compact::MP_END, "Invalid family"); - static_assert(FAMILY != compact::MP_EXT, "Please use as_ext"); + static_assert(FAMILY < MP_END, "Invalid family"); + static_assert(FAMILY != MP_EXT, "Please use as_ext"); if constexpr(is_wrapped_v) { using WRAP_T = std::remove_reference_t; @@ -123,7 +123,7 @@ constexpr auto as_family(T&& t) template struct wrapped_ext : wrapped_family_tag, BASE { - static constexpr compact::Family family = compact::MP_EXT; + static constexpr Family family = MP_EXT; EXT_TYPE ext_type; constexpr wrapped_ext(EXT_TYPE e, BASE&& arg) : BASE(std::forward(arg)), ext_type(e) {} diff --git a/test/EncDecTest.cpp b/test/EncDecTest.cpp index 1eeb20687..dc1579878 100644 --- a/test/EncDecTest.cpp +++ b/test/EncDecTest.cpp @@ -164,20 +164,11 @@ test_type_visual() { TEST_INIT(0); using namespace mpp; - std::cout << compact::MP_ARR << " " - << compact::MP_MAP << " " - << compact::MP_EXT << "\n"; - std::cout << MP_NIL << " " - << MP_BOOL << " " - << (MP_INT) << " " - << (MP_BIN | MP_STR) << " " - << (MP_INT | MP_FLT) << "\n"; + std::cout << MP_ARR << " " << MP_MAP << " " << MP_EXT << "\n"; std::cout << "(" << family_sequence<>{} << ") " - << "(" << family_sequence{} << ") " - << "(" << family_sequence{} << ")\n"; + << "(" << family_sequence{} << ") " + << "(" << family_sequence{} << ")\n"; } enum E1 { diff --git a/test/RulesUnitTest.cpp b/test/RulesUnitTest.cpp index 3c0d2aded..db83bf24f 100644 --- a/test/RulesUnitTest.cpp +++ b/test/RulesUnitTest.cpp @@ -37,7 +37,7 @@ #include "Utils/Helpers.hpp" namespace test { -using fis_t = std::make_index_sequence; +using fis_t = std::make_index_sequence; template struct has_simplex : std::false_type {}; @@ -86,7 +86,7 @@ constexpr bool rule_has_tag_v = template constexpr auto rule_by_tag_h(std::index_sequence) { - constexpr mpp::compact::Family family{FAMILY}; + constexpr mpp::Family family{FAMILY}; using rule_t = mpp::rule_by_family_t; if constexpr (rule_has_tag_v) { return rule_t{}; @@ -101,7 +101,7 @@ using rule_by_tag_t = decltype(rule_by_tag_h(test::fis_t{})); } // namespace test -template +template void check_family_rule() { @@ -112,14 +112,14 @@ check_family_rule() if constexpr (Rule_t::has_simplex) static_assert(Rule_t::simplex_value_range.first == 0 || (Rule_t::simplex_value_range.first < 0 && - FAMILY == mpp::compact::MP_INT)); + FAMILY == mpp::MP_INT)); } template void check_family_rule(std::index_sequence) { - (check_family_rule(FAMILY)>(), ...); + (check_family_rule(FAMILY)>(), ...); } void @@ -134,8 +134,8 @@ test_basic() // Some selective checks. static_assert(std::is_same_v, BoolRule::types>); static_assert(std::is_same_v, IntRule::types>); - static_assert(NilRule::family == compact::MP_NIL); - static_assert(BinRule::family == compact::MP_BIN); + static_assert(NilRule::family == MP_NIL); + static_assert(BinRule::family == MP_BIN); static_assert(IgnrRule::has_data == false); static_assert(StrRule::has_data == true); static_assert(BinRule::has_data == true); @@ -281,7 +281,7 @@ template constexpr std::array> tuple_sizes = tuple_sizes_h(std::make_index_sequence>{}); -template +template void collectByType(FullInfo& infos) { using Rule = mpp::rule_by_family_t; @@ -323,7 +323,7 @@ template FullInfo collectByType(std::index_sequence) { FullInfo infos; - (collectByType(FAMILY)>(infos), ...); + (collectByType(FAMILY)>(infos), ...); return infos; }