From 58dacce85385244e7ab8e47a9912f11c8e74249c Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 30 Nov 2024 15:53:51 +0800 Subject: [PATCH] format code --- iguana/json_reader.hpp | 29 +++++++++++++++------------- iguana/util.hpp | 44 +++++++++++++++++++++++------------------- test/test_some.cpp | 16 +++++++-------- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/iguana/json_reader.hpp b/iguana/json_reader.hpp index 60401d00..3e5e8a2f 100644 --- a/iguana/json_reader.hpp +++ b/iguana/json_reader.hpp @@ -560,8 +560,8 @@ IGUANA_INLINE void from_json_variant(U &value, It &it, It &end, } template -IGUANA_INLINE bool try_read_variant_type(T& type_value, std::string_view target, It it, It end) { - +IGUANA_INLINE bool try_read_variant_type(T &type_value, std::string_view target, + It it, It end) { skip_ws(it, end); match<'{'>(it, end); if (*it == '}') @@ -580,7 +580,7 @@ IGUANA_INLINE bool try_read_variant_type(T& type_value, std::string_view target, skip_ws(it, end); match<':'>(it, end); if (key != target) { - // discard left + // discard left detail::skip_object_value(it, end); } else { @@ -598,7 +598,6 @@ IGUANA_INLINE bool try_read_variant_type(T& type_value, std::string_view target, IGUANA_LIKELY { match<','>(it, end); } key = detail::get_key(it, end); } - } template @@ -606,15 +605,18 @@ IGUANA_INLINE void from_json_variant_by_type(U &value, It &it, It &end) { using variant_t = std::remove_reference_t; const auto target = variant_type_field_name; variant_type_field_t type; - + if (!try_read_variant_type(type, target, it, end)) { - throw std::runtime_error ("variant expected type field: " + std::string{target}); + throw std::runtime_error("variant expected type field: " + + std::string{target}); } variant_cast_helper{}(value, type); - std::visit([&](auto& val) { - from_json_impl(val, it, end); - }, value); + std::visit( + [&](auto &val) { + from_json_impl(val, it, end); + }, + value); } template , int>> @@ -624,10 +626,11 @@ IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) { "don't allow same type in std::variant"); if constexpr (has_variant_type_field_helper_v) { from_json_variant_by_type(value, it, end); - } else { - from_json_variant(value, it, end, - std::make_index_sequence< - std::variant_size_v>{}); + } + else { + from_json_variant( + value, it, end, + std::make_index_sequence>{}); } } } // namespace detail diff --git a/iguana/util.hpp b/iguana/util.hpp index dc00b7f2..2cbdeed6 100644 --- a/iguana/util.hpp +++ b/iguana/util.hpp @@ -353,35 +353,41 @@ struct variant_type_field_helper : std::false_type { template IGUANA_INLINE constexpr auto get_variant_type_field() { - using element_t = variant_element_t; - return variant_type_field_helper{}((element_t*)nullptr); + using element_t = variant_element_t; + return variant_type_field_helper{}((element_t*)nullptr); } template using variant_type_field_t = decltype(get_variant_type_field<0, variant_t>()); template -constexpr inline auto variant_type_field_v = get_variant_type_field(); +constexpr inline auto variant_type_field_v = + get_variant_type_field(); template -IGUANA_INLINE constexpr size_t get_variant_element_field_index(field_t target_field) { - size_t find_index = size_t(-1); - ylt::reflection::for_each(element_t{}, [&](auto& field, auto name, auto index) { - if constexpr (std::is_same_v, field_t>) { +IGUANA_INLINE constexpr size_t get_variant_element_field_index( + field_t target_field) { + size_t find_index = size_t(-1); + ylt::reflection::for_each( + element_t{}, [&](auto& field, auto name, auto index) { + if constexpr (std::is_same_v< + ylt::reflection::remove_cvref_t, + field_t>) { if (field == target_field) { find_index = index; } } - }); - return find_index; + }); + return find_index; } template IGUANA_INLINE constexpr std::string_view get_variant_type_field_name() { - using element_t = variant_element_t<0, variant_t>; - constexpr size_t find_index = get_variant_element_field_index(variant_type_field_v<0, variant_t>); - static_assert(find_index != size_t(-1)); - return ylt::reflection::name_of(); + using element_t = variant_element_t<0, variant_t>; + constexpr size_t find_index = get_variant_element_field_index( + variant_type_field_v<0, variant_t>); + static_assert(find_index != size_t(-1)); + return ylt::reflection::name_of(); } template @@ -409,15 +415,12 @@ struct variant_cast_helper> { template void init_impl(variant_t& v, field_type tt, std::index_sequence) { bool r = false; - ((void)(!r && (r = init_impl0( - v, tt), - true)), - ...); + ((void)(!r && (r = init_impl0(v, tt), true)), ...); } void operator()(variant_t& v, field_type tt) { - return init_impl(v, tt, std::make_index_sequence< - std::variant_size_v>{}); + return init_impl( + v, tt, std::make_index_sequence>{}); } }; @@ -427,7 +430,8 @@ struct has_variant_type_field_helper { }; template -constexpr inline bool has_variant_type_field_helper_v = has_variant_type_field_helper::value; +constexpr inline bool has_variant_type_field_helper_v = + has_variant_type_field_helper::value; #if defined(__clang__) || defined(_MSC_VER) || \ (defined(__GNUC__) && __GNUC__ > 8) diff --git a/test/test_some.cpp b/test/test_some.cpp index 18345147..b47d67be 100644 --- a/test/test_some.cpp +++ b/test/test_some.cpp @@ -258,15 +258,15 @@ struct my_variant_type1_t { }; YLT_REFL(my_variant_type1_t, value, tt); namespace iguana { - template <> - struct variant_type_field_helper> : std::true_type { - template - constexpr auto operator()(T*){ - return T{}.tt; - }; +template <> +struct variant_type_field_helper< + std::variant> : std::true_type { + template + constexpr auto operator()(T *) { + return T{}.tt; }; -} - +}; +} // namespace iguana TEST_CASE("test variant type") { std::variant var, var1;