#pragma once #include #include #include #include #include #include #include #include #include #include "define.h" #include "detail/charconv.h" #include "detail/pb_type.hpp" #include "detail/traits.hpp" #include "detail/utf.hpp" #include "error_code.h" #include "field_reflection.hpp" #include "ylt/reflection/member_value.hpp" #include "ylt/reflection/user_reflect_macro.hpp" namespace iguana { template inline constexpr bool char_v = std::is_same_v, char> || std::is_same_v, char16_t> || std::is_same_v, char32_t> || std::is_same_v, wchar_t>; template inline constexpr bool bool_v = std::is_same_v, bool> || std::is_same_v, std::vector::reference>; template inline constexpr bool int_v = std::is_integral_v> && !char_v> && !bool_v; template inline constexpr bool float_v = std::is_floating_point_v>; template inline constexpr bool num_v = float_v || int_v; template inline constexpr bool enum_v = std::is_enum_v>; template constexpr inline bool optional_v = ylt::reflection::optional; template constexpr inline bool pair_v = ylt::reflection::pair; template struct is_container : std::false_type {}; template struct is_container< T, std::void_t().size(), std::declval().begin(), std::declval().end())>> : std::true_type {}; template struct is_map_container : std::false_type {}; template struct is_map_container< T, std::void_t())>> : is_container {}; template constexpr inline bool container_v = is_container>::value; template constexpr inline bool map_container_v = is_map_container>::value; template constexpr inline bool c_array_v = std::is_array_v>&& std::extent_v> > 0; template struct is_array : std::false_type {}; template struct is_array< T, std::void_t().size()), typename std::enable_if_t<(std::tuple_size::value != 0)>>> : std::true_type {}; template constexpr inline bool array_v = is_array>::value; template constexpr inline bool fixed_array_v = c_array_v || #if __cplusplus > 201703L #if __has_include() is_span::value || #endif #endif array_v; template constexpr inline bool string_view_v = is_template_instant_of>::value; template constexpr inline bool string_v = is_template_instant_of>::value; // TODO: type must be char template constexpr inline bool json_view_v = container_v; template constexpr inline bool json_byte_v = std::is_same_v> || std::is_same_v> || std::is_same_v>; template constexpr inline bool sequence_container_v = is_sequence_container>::value; template constexpr inline bool tuple_v = is_tuple>::value; template constexpr inline bool string_container_v = string_v || string_view_v; template constexpr inline bool unique_ptr_v = is_template_instant_of>::value; template constexpr inline bool shared_ptr_v = is_template_instant_of>::value; template constexpr inline bool smart_ptr_v = shared_ptr_v || unique_ptr_v; template constexpr inline bool variant_v = is_variant>::value; template using variant_element_t = std::remove_reference_t( std::declval>()))>; template constexpr void foreach_tuple(F&& f, Tuple& tp, std::index_sequence) { (void(f(std::get(tp), std::integral_constant{})), ...); } template constexpr void foreach_tuple(F&& f, Tuple& tp) { foreach_tuple(std::forward(f), tp, std::make_index_sequence>{}); } template inline constexpr auto for_each_tuple(F&& f, T&& tup) { std::apply( [&f](auto&&... args) { (f(args), ...); }, std::forward(tup)); } template constexpr inline bool reflect26_excluded_v = false; template constexpr inline bool ylt_refletable_v = #ifdef YLT_USE_CXX26_REFLECTION (std::is_class_v> || std::is_aggregate_v>) && !string_container_v && !container_v && !fixed_array_v && !tuple_v && !optional_v && !variant_v && !smart_ptr_v && !reflect26_excluded_v> && !ylt::reflection::is_custom_refl_v && !is_pb_type_v; #else (ylt::reflection::is_ylt_refl_v || std::is_aggregate_v>) && !fixed_array_v && !ylt::reflection::is_custom_refl_v && !is_pb_type_v; #endif template constexpr inline bool non_ylt_refletable_v = !ylt_refletable_v; template constexpr inline bool plain_v = string_container_v || num_v || char_v || bool_v || enum_v; template struct underline_type { using type = T; }; template struct underline_type> { using type = typename underline_type::type; }; template struct underline_type> { using type = typename underline_type::type; }; template struct underline_type> { using type = typename underline_type::type; }; template using underline_type_t = typename underline_type>::type; struct memory_writer { char* buffer; void write(const char* data, std::size_t len) { memcpy(buffer, data, len); buffer += len; } }; template IGUANA_INLINE void match(It&& it, It&& end) { const auto n = static_cast(std::distance(it, end)); if (n < sizeof...(C)) IGUANA_UNLIKELY { // TODO: compile time generate this message, currently borken with // MSVC static constexpr auto error = "Unexpected end of buffer. Expected:"; throw std::runtime_error(error); } if (((... || (*it++ != C)))) IGUANA_UNLIKELY { // TODO: compile time generate this message, currently borken with // MSVC static constexpr char b[] = {C..., '\0'}; throw std::runtime_error(std::string("Expected these: ").append(b)); } } inline constexpr auto has_zero = [](uint64_t chunk) IGUANA__INLINE_LAMBDA { return (((chunk - 0x0101010101010101) & ~chunk) & 0x8080808080808080); }; inline constexpr auto has_qoute = [](uint64_t chunk) IGUANA__INLINE_LAMBDA { return has_zero( chunk ^ 0b0010001000100010001000100010001000100010001000100010001000100010); }; template IGUANA_INLINE void write_unicode_to_string(Ch& it, Stream& ss) { static const char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; unsigned codepoint = 0; if (!decode_utf8(it, codepoint)) IGUANA_UNLIKELY { throw std::runtime_error("illegal unicode character"); } if constexpr (is_xml_serialization) { ss.append("&#x"); } else { ss.push_back('\\'); ss.push_back('u'); } if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { ss.push_back(hexDigits[(codepoint >> 12) & 15]); ss.push_back(hexDigits[(codepoint >> 8) & 15]); ss.push_back(hexDigits[(codepoint >> 4) & 15]); ss.push_back(hexDigits[(codepoint)&15]); } else { if (codepoint < 0x010000 || codepoint > 0x10FFFF) IGUANA_UNLIKELY { throw std::runtime_error("illegal codepoint"); } // Surrogate pair unsigned s = codepoint - 0x010000; unsigned lead = (s >> 10) + 0xD800; unsigned trail = (s & 0x3FF) + 0xDC00; ss.push_back(hexDigits[(lead >> 12) & 15]); ss.push_back(hexDigits[(lead >> 8) & 15]); ss.push_back(hexDigits[(lead >> 4) & 15]); ss.push_back(hexDigits[(lead)&15]); if constexpr (is_xml_serialization) { ss.append(";&#x"); } else { ss.push_back('\\'); ss.push_back('u'); } ss.push_back(hexDigits[(trail >> 12) & 15]); ss.push_back(hexDigits[(trail >> 8) & 15]); ss.push_back(hexDigits[(trail >> 4) & 15]); ss.push_back(hexDigits[(trail)&15]); } if constexpr (is_xml_serialization) { ss.push_back(';'); } } // https://github.com/Tencent/rapidjson/blob/master/include/rapidjson/writer.h template IGUANA_INLINE void write_string_with_escape(const Ch* it, SizeType length, Stream& ss) { static const char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; static const char escape[256] = { #define Z16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 0 1 2 3 4 5 6 7 8 9 A B C D E // F 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 Z16, Z16, // 30~4F 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\\', 0, 0, 0, // 50 Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF #undef Z16 }; auto end = it; std::advance(end, length); while (it < end) { if (static_cast(*it) >= 0x80) IGUANA_UNLIKELY { write_unicode_to_string(it, ss); } else if (escape[static_cast(*it)]) IGUANA_UNLIKELY { ss.push_back('\\'); ss.push_back(escape[static_cast(*it)]); if (escape[static_cast(*it)] == 'u') { // escape other control characters ss.push_back('0'); ss.push_back('0'); ss.push_back(hexDigits[static_cast(*it) >> 4]); ss.push_back(hexDigits[static_cast(*it) & 0xF]); } ++it; } else { ss.push_back(*(it++)); } } } template IGUANA_INLINE constexpr bool has_duplicate(const std::array& arr) { for (size_t i = 0; i < arr.size(); i++) { for (size_t j = i + 1; j < arr.size(); j++) { if (arr[i] == arr[j]) { return true; } } } return false; } #if defined(__clang__) || defined(_MSC_VER) || \ (defined(__GNUC__) && __GNUC__ > 8) template IGUANA_INLINE constexpr bool has_duplicate_type() { std::array arr{ iguana::type_string()...}; return has_duplicate(arr); } template struct has_duplicate_type_in_variant : std::false_type {}; template struct has_duplicate_type_in_variant> { inline constexpr static bool value = has_duplicate_type(); }; template constexpr inline bool has_duplicate_type_v = has_duplicate_type_in_variant::value; #else template constexpr inline bool has_duplicate_type_v = false; #endif } // namespace iguana