Skip to content

Commit

Permalink
add restrict for variant
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Apr 20, 2024
1 parent d009665 commit e263221
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 2 additions & 4 deletions iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,11 @@ IGUANA_INLINE bool from_json_variant_impl(U &value, It it, It end, It &temp_it,
}
}

template <size_t Idx, typename T>
using variant_element_t = std::remove_reference_t<decltype(std::get<Idx>(
std::declval<std::remove_reference_t<T>>()))>;

template <typename U, typename It, size_t... Idx>
IGUANA_INLINE void from_json_variant(U &value, It &it, It &end,
std::index_sequence<Idx...>) {
static_assert(!has_same_variant_type_v<std::remove_reference_t<U>>,
"don't allow same type in std::variant");
bool r = false;
It temp_it = it;
It temp_end = end;
Expand Down
2 changes: 2 additions & 0 deletions iguana/json_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ IGUANA_INLINE void to_json_impl(Stream &s, T &&t) {
template <bool Is_writing_escape, typename Stream, typename T,
std::enable_if_t<variant_v<T>, int>>
IGUANA_INLINE void to_json_impl(Stream &s, T &&t) {
static_assert(!has_same_variant_type_v<std::remove_reference_t<T>>,
"don't allow same type in std::variant");
std::visit(
[&s](auto value) {
to_json_impl<Is_writing_escape>(s, value);
Expand Down
14 changes: 14 additions & 0 deletions iguana/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ struct is_variant<std::variant<T...>> : std::true_type {};
template <typename T>
constexpr inline bool variant_v = is_variant<std::remove_cvref_t<T>>::value;

template <typename T>
struct has_same_variant_type : std::false_type {};

template <typename T, typename... Us>
struct has_same_variant_type<std::variant<T, Us...>>
: std::disjunction<std::is_same<T, Us>...> {};

template <typename T>
constexpr bool has_same_variant_type_v = has_same_variant_type<T>::value;

template <size_t Idx, typename T>
using variant_element_t = std::remove_reference_t<decltype(std::get<Idx>(
std::declval<std::remove_reference_t<T>>()))>;

template <typename T>
constexpr inline bool refletable_v = is_reflection_v<std::remove_cvref_t<T>>;

Expand Down

0 comments on commit e263221

Please sign in to comment.