Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
fesily committed Nov 30, 2024
1 parent 950c472 commit 58dacce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
29 changes: 16 additions & 13 deletions iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ IGUANA_INLINE void from_json_variant(U &value, It &it, It &end,
}

template <typename T, typename It>
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 == '}')
Expand All @@ -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 {
Expand All @@ -598,23 +598,25 @@ 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 <typename U, typename It>
IGUANA_INLINE void from_json_variant_by_type(U &value, It &it, It &end) {
using variant_t = std::remove_reference_t<U>;
const auto target = variant_type_field_name<variant_t>;
variant_type_field_t<variant_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<variant_t>{}(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 <typename U, typename It, std::enable_if_t<variant_v<U>, int>>
Expand All @@ -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<variant_t>) {
from_json_variant_by_type(value, it, end);
} else {
from_json_variant(value, it, end,
std::make_index_sequence<
std::variant_size_v<variant_t>>{});
}
else {
from_json_variant(
value, it, end,
std::make_index_sequence<std::variant_size_v<variant_t>>{});
}
}
} // namespace detail
Expand Down
44 changes: 24 additions & 20 deletions iguana/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,35 +353,41 @@ struct variant_type_field_helper : std::false_type {

template <size_t Idx, typename variant_t>
IGUANA_INLINE constexpr auto get_variant_type_field() {
using element_t = variant_element_t<Idx, variant_t>;
return variant_type_field_helper<variant_t>{}((element_t*)nullptr);
using element_t = variant_element_t<Idx, variant_t>;
return variant_type_field_helper<variant_t>{}((element_t*)nullptr);
}

template <typename variant_t>
using variant_type_field_t = decltype(get_variant_type_field<0, variant_t>());

template <size_t Idx, typename variant_t>
constexpr inline auto variant_type_field_v = get_variant_type_field<Idx, variant_t>();
constexpr inline auto variant_type_field_v =
get_variant_type_field<Idx, variant_t>();

template <typename element_t, typename 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<decltype(field)>, 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<decltype(field)>,
field_t>) {
if (field == target_field) {
find_index = index;
}
}
});
return find_index;
});
return find_index;
}

template <typename variant_t>
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<element_t>(variant_type_field_v<0, variant_t>);
static_assert(find_index != size_t(-1));
return ylt::reflection::name_of<element_t, find_index>();
using element_t = variant_element_t<0, variant_t>;
constexpr size_t find_index = get_variant_element_field_index<element_t>(
variant_type_field_v<0, variant_t>);
static_assert(find_index != size_t(-1));
return ylt::reflection::name_of<element_t, find_index>();
}

template <typename variant_t>
Expand Down Expand Up @@ -409,15 +415,12 @@ struct variant_cast_helper<std::variant<Us...>> {
template <size_t... Idx>
void init_impl(variant_t& v, field_type tt, std::index_sequence<Idx...>) {
bool r = false;
((void)(!r && (r = init_impl0<Idx>(
v, tt),
true)),
...);
((void)(!r && (r = init_impl0<Idx>(v, tt), true)), ...);
}

void operator()(variant_t& v, field_type tt) {
return init_impl(v, tt, std::make_index_sequence<
std::variant_size_v<variant_t>>{});
return init_impl(
v, tt, std::make_index_sequence<std::variant_size_v<variant_t>>{});
}
};

Expand All @@ -427,7 +430,8 @@ struct has_variant_type_field_helper {
};

template <typename T>
constexpr inline bool has_variant_type_field_helper_v = has_variant_type_field_helper<T>::value;
constexpr inline bool has_variant_type_field_helper_v =
has_variant_type_field_helper<T>::value;

#if defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 8)
Expand Down
16 changes: 8 additions & 8 deletions test/test_some.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::variant<my_variant_type0_t, my_variant_type1_t>> : std::true_type {
template<typename T>
constexpr auto operator()(T*){
return T{}.tt;
};
template <>
struct variant_type_field_helper<
std::variant<my_variant_type0_t, my_variant_type1_t>> : std::true_type {
template <typename T>
constexpr auto operator()(T *) {
return T{}.tt;
};
}

};
} // namespace iguana

TEST_CASE("test variant type") {
std::variant<my_variant_type0_t, my_variant_type1_t> var, var1;
Expand Down

0 comments on commit 58dacce

Please sign in to comment.