Skip to content

Commit

Permalink
[struct_pack] fix reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Sep 12, 2024
1 parent be8da74 commit f043b15
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 47 deletions.
2 changes: 1 addition & 1 deletion include/ylt/reflection/member_count.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ constexpr bool expected = expected_impl<T>::value;

#if __cpp_concepts >= 201907L
template <typename Type>
concept optional = requires(Type optional) {
concept optional = !expected<Type> && requires(Type optional) {
optional.value();
optional.has_value();
optional.operator*();
Expand Down
4 changes: 2 additions & 2 deletions include/ylt/struct_pack/calculate_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ constexpr size_info inline calculate_one_size(const T &item) {
},
item);
}
else if constexpr (optional<type>) {
else if constexpr (ylt::reflection::optional<type>) {
ret.total = sizeof(char);
if (item) {
ret += calculate_one_size(*item);
Expand Down Expand Up @@ -133,7 +133,7 @@ constexpr size_info inline calculate_one_size(const T &item) {
},
item);
}
else if constexpr (expected<type>) {
else if constexpr (ylt::reflection::expected<type>) {
ret.total = sizeof(bool);
if (item.has_value()) {
if constexpr (!std::is_same_v<typename type::value_type, void>)
Expand Down
8 changes: 4 additions & 4 deletions include/ylt/struct_pack/packer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class packer {
},
item);
}
else if constexpr (optional<type>) {
else if constexpr (ylt::reflection::optional<type>) {
bool has_value = item.has_value();
write_wrapper<sizeof(bool)>(writer_, (char *)&has_value);
if (has_value) {
Expand All @@ -401,7 +401,7 @@ class packer {
},
item);
}
else if constexpr (expected<type>) {
else if constexpr (ylt::reflection::expected<type>) {
bool has_value = item.has_value();
write_wrapper<sizeof(bool)>(writer_, (char *)&has_value);
if (has_value) {
Expand Down Expand Up @@ -500,7 +500,7 @@ class packer {
},
item);
}
else if constexpr (optional<type>) {
else if constexpr (ylt::reflection::optional<type>) {
if (item.has_value()) {
serialize_one<size_type, version>(*item);
}
Expand All @@ -512,7 +512,7 @@ class packer {
},
item);
}
else if constexpr (expected<type>) {
else if constexpr (ylt::reflection::expected<type>) {
if (item.has_value()) {
if constexpr (!std::is_same_v<typename type::value_type, void>)
serialize_one<size_type, version>(item.value());
Expand Down
31 changes: 2 additions & 29 deletions include/ylt/struct_pack/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "ylt/reflection/template_switch.hpp"
#include "ylt/reflection/member_ptr.hpp"

using namespace ylt::reflection;

#if __cpp_concepts >= 201907L
#include <concepts>
Expand Down Expand Up @@ -763,32 +762,6 @@ struct memory_reader;
#endif


#if __cpp_concepts >= 201907L
template <typename Type>
concept optional = !expected<Type> && requires(Type optional) {
optional.value();
optional.has_value();
optional.operator*();
typename remove_cvref_t<Type>::value_type;
};
#else
template <typename T, typename = void>
struct optional_impl : std::false_type {};

template <typename T>
struct optional_impl<T, std::void_t<
decltype(std::declval<T>().value()),
decltype(std::declval<T>().has_value()),
decltype(std::declval<T>().operator*()),
typename remove_cvref_t<T>::value_type>>
: std::true_type {};

template <typename T>
constexpr bool optional = !expected<T> && optional_impl<T>::value;
#endif





template <typename Type>
Expand Down Expand Up @@ -875,8 +848,8 @@ struct memory_reader;
else if constexpr (user_defined_refl<T>) {
return false;
}
else if constexpr (container<T> || optional<T> || is_variant_v<T> ||
unique_ptr<T> || expected<T> || container_adapter<T>) {
else if constexpr (container<T> || ylt::reflection::optional<T> || is_variant_v<T> ||
unique_ptr<T> || ylt::reflection::expected<T> || container_adapter<T>) {
return false;
}
else if constexpr (pair<T>) {
Expand Down
5 changes: 3 additions & 2 deletions include/ylt/struct_pack/type_calculate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,9 @@ constexpr auto get_types() {
return declval<std::tuple<T>>();
}
else if constexpr (std::is_fundamental_v<T> || std::is_enum_v<T> ||
varint_t<T> || string<T> || container<T> || optional<T> ||
unique_ptr<T> || is_variant_v<T> || expected<T> ||
varint_t<T> || string<T> || container<T> ||
ylt::reflection::optional<T> || unique_ptr<T> ||
is_variant_v<T> || ylt::reflection::expected<T> ||
array<T> || c_array<T> ||
std::is_same_v<std::monostate, T> || bitset<T>
#if (__GNUC__ || __clang__) && defined(STRUCT_PACK_ENABLE_INT128)
Expand Down
6 changes: 3 additions & 3 deletions include/ylt/struct_pack/type_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ constexpr type_id get_type_id() {
if constexpr (user_defined_serialization<T>) {
return type_id::user_defined_type;
}
else if constexpr (optional<T> && is_compatible_v<T>) {
else if constexpr (ylt::reflection::optional<T> && is_compatible_v<T>) {
return type_id::compatible_t;
}
else if constexpr (detail::varint_t<T, parent_tag>) {
Expand Down Expand Up @@ -334,7 +334,7 @@ constexpr type_id get_type_id() {
else if constexpr (container<T>) {
return type_id::container_t;
}
else if constexpr (optional<T>) {
else if constexpr (ylt::reflection::optional<T>) {
return type_id::optional_t;
}
else if constexpr (unique_ptr<T>) {
Expand All @@ -354,7 +354,7 @@ constexpr type_id get_type_id() {
static_assert(std::variant_size_v<T> < 256, "The variant is too complex!");
return type_id::variant_t;
}
else if constexpr (expected<T>) {
else if constexpr (ylt::reflection::expected<T>) {
return type_id::expected_t;
}
else if constexpr (is_trivial_tuple<T> || pair<T> || std::is_class_v<T>) {
Expand Down
14 changes: 8 additions & 6 deletions include/ylt/struct_pack/unpacker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,14 +1131,15 @@ class unpacker {
},
item);
}
else if constexpr (optional<type> || expected<type>) {
else if constexpr (ylt::reflection::optional<type> ||
ylt::reflection::expected<type>) {
bool has_value{};
if SP_UNLIKELY (!read_wrapper<sizeof(bool)>(reader_,
(char *)&has_value)) {
return struct_pack::errc::no_buffer_space;
}
if SP_UNLIKELY (!has_value) {
if constexpr (expected<type>) {
if constexpr (ylt::reflection::expected<type>) {
item = typename type::unexpected_type{typename type::error_type{}};
deserialize_one<size_type, version, NotSkip>(item.error());
}
Expand All @@ -1147,7 +1148,7 @@ class unpacker {
}
}
else {
if constexpr (expected<type>) {
if constexpr (ylt::reflection::expected<type>) {
if constexpr (!std::is_same_v<typename type::value_type, void>)
deserialize_one<size_type, version, NotSkip>(item.value());
}
Expand Down Expand Up @@ -1338,15 +1339,16 @@ class unpacker {
},
item);
}
else if constexpr (optional<type> || expected<type>) {
else if constexpr (ylt::reflection::optional<type> ||
ylt::reflection::expected<type>) {
bool has_value = item.has_value();
if (!has_value) {
if constexpr (expected<type>) {
if constexpr (ylt::reflection::expected<type>) {
deserialize_one<size_type, version, NotSkip>(item.error());
}
}
else {
if constexpr (expected<type>) {
if constexpr (ylt::reflection::expected<type>) {
if constexpr (!std::is_same_v<typename type::value_type, void>)
deserialize_one<size_type, version, NotSkip>(item.value());
}
Expand Down

0 comments on commit f043b15

Please sign in to comment.