From f043b1583b46932724d7df8b7bf88ea16396aaf2 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Thu, 12 Sep 2024 12:01:53 +0800 Subject: [PATCH] [struct_pack] fix reflection --- include/ylt/reflection/member_count.hpp | 2 +- include/ylt/struct_pack/calculate_size.hpp | 4 +-- include/ylt/struct_pack/packer.hpp | 8 +++--- include/ylt/struct_pack/reflection.hpp | 31 ++-------------------- include/ylt/struct_pack/type_calculate.hpp | 5 ++-- include/ylt/struct_pack/type_id.hpp | 6 ++--- include/ylt/struct_pack/unpacker.hpp | 14 +++++----- 7 files changed, 23 insertions(+), 47 deletions(-) diff --git a/include/ylt/reflection/member_count.hpp b/include/ylt/reflection/member_count.hpp index e323c7ee0..3932c580d 100644 --- a/include/ylt/reflection/member_count.hpp +++ b/include/ylt/reflection/member_count.hpp @@ -47,7 +47,7 @@ constexpr bool expected = expected_impl::value; #if __cpp_concepts >= 201907L template -concept optional = requires(Type optional) { +concept optional = !expected && requires(Type optional) { optional.value(); optional.has_value(); optional.operator*(); diff --git a/include/ylt/struct_pack/calculate_size.hpp b/include/ylt/struct_pack/calculate_size.hpp index 1990685a7..ea58f2f5d 100644 --- a/include/ylt/struct_pack/calculate_size.hpp +++ b/include/ylt/struct_pack/calculate_size.hpp @@ -97,7 +97,7 @@ constexpr size_info inline calculate_one_size(const T &item) { }, item); } - else if constexpr (optional) { + else if constexpr (ylt::reflection::optional) { ret.total = sizeof(char); if (item) { ret += calculate_one_size(*item); @@ -133,7 +133,7 @@ constexpr size_info inline calculate_one_size(const T &item) { }, item); } - else if constexpr (expected) { + else if constexpr (ylt::reflection::expected) { ret.total = sizeof(bool); if (item.has_value()) { if constexpr (!std::is_same_v) diff --git a/include/ylt/struct_pack/packer.hpp b/include/ylt/struct_pack/packer.hpp index a1238b674..ccb14925c 100644 --- a/include/ylt/struct_pack/packer.hpp +++ b/include/ylt/struct_pack/packer.hpp @@ -383,7 +383,7 @@ class packer { }, item); } - else if constexpr (optional) { + else if constexpr (ylt::reflection::optional) { bool has_value = item.has_value(); write_wrapper(writer_, (char *)&has_value); if (has_value) { @@ -401,7 +401,7 @@ class packer { }, item); } - else if constexpr (expected) { + else if constexpr (ylt::reflection::expected) { bool has_value = item.has_value(); write_wrapper(writer_, (char *)&has_value); if (has_value) { @@ -500,7 +500,7 @@ class packer { }, item); } - else if constexpr (optional) { + else if constexpr (ylt::reflection::optional) { if (item.has_value()) { serialize_one(*item); } @@ -512,7 +512,7 @@ class packer { }, item); } - else if constexpr (expected) { + else if constexpr (ylt::reflection::expected) { if (item.has_value()) { if constexpr (!std::is_same_v) serialize_one(item.value()); diff --git a/include/ylt/struct_pack/reflection.hpp b/include/ylt/struct_pack/reflection.hpp index ed48ca983..e60abcb9b 100644 --- a/include/ylt/struct_pack/reflection.hpp +++ b/include/ylt/struct_pack/reflection.hpp @@ -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 @@ -763,32 +762,6 @@ struct memory_reader; #endif -#if __cpp_concepts >= 201907L - template - concept optional = !expected && requires(Type optional) { - optional.value(); - optional.has_value(); - optional.operator*(); - typename remove_cvref_t::value_type; - }; -#else - template - struct optional_impl : std::false_type {}; - - template - struct optional_impl().value()), - decltype(std::declval().has_value()), - decltype(std::declval().operator*()), - typename remove_cvref_t::value_type>> - : std::true_type {}; - - template - constexpr bool optional = !expected && optional_impl::value; -#endif - - - template @@ -875,8 +848,8 @@ struct memory_reader; else if constexpr (user_defined_refl) { return false; } - else if constexpr (container || optional || is_variant_v || - unique_ptr || expected || container_adapter) { + else if constexpr (container || ylt::reflection::optional || is_variant_v || + unique_ptr || ylt::reflection::expected || container_adapter) { return false; } else if constexpr (pair) { diff --git a/include/ylt/struct_pack/type_calculate.hpp b/include/ylt/struct_pack/type_calculate.hpp index 1d7cbc343..8fc295b8d 100644 --- a/include/ylt/struct_pack/type_calculate.hpp +++ b/include/ylt/struct_pack/type_calculate.hpp @@ -861,8 +861,9 @@ constexpr auto get_types() { return declval>(); } else if constexpr (std::is_fundamental_v || std::is_enum_v || - varint_t || string || container || optional || - unique_ptr || is_variant_v || expected || + varint_t || string || container || + ylt::reflection::optional || unique_ptr || + is_variant_v || ylt::reflection::expected || array || c_array || std::is_same_v || bitset #if (__GNUC__ || __clang__) && defined(STRUCT_PACK_ENABLE_INT128) diff --git a/include/ylt/struct_pack/type_id.hpp b/include/ylt/struct_pack/type_id.hpp index 4e815a8aa..d84c0b7e0 100644 --- a/include/ylt/struct_pack/type_id.hpp +++ b/include/ylt/struct_pack/type_id.hpp @@ -290,7 +290,7 @@ constexpr type_id get_type_id() { if constexpr (user_defined_serialization) { return type_id::user_defined_type; } - else if constexpr (optional && is_compatible_v) { + else if constexpr (ylt::reflection::optional && is_compatible_v) { return type_id::compatible_t; } else if constexpr (detail::varint_t) { @@ -334,7 +334,7 @@ constexpr type_id get_type_id() { else if constexpr (container) { return type_id::container_t; } - else if constexpr (optional) { + else if constexpr (ylt::reflection::optional) { return type_id::optional_t; } else if constexpr (unique_ptr) { @@ -354,7 +354,7 @@ constexpr type_id get_type_id() { static_assert(std::variant_size_v < 256, "The variant is too complex!"); return type_id::variant_t; } - else if constexpr (expected) { + else if constexpr (ylt::reflection::expected) { return type_id::expected_t; } else if constexpr (is_trivial_tuple || pair || std::is_class_v) { diff --git a/include/ylt/struct_pack/unpacker.hpp b/include/ylt/struct_pack/unpacker.hpp index 0fe195698..36848edc4 100644 --- a/include/ylt/struct_pack/unpacker.hpp +++ b/include/ylt/struct_pack/unpacker.hpp @@ -1131,14 +1131,15 @@ class unpacker { }, item); } - else if constexpr (optional || expected) { + else if constexpr (ylt::reflection::optional || + ylt::reflection::expected) { bool has_value{}; if SP_UNLIKELY (!read_wrapper(reader_, (char *)&has_value)) { return struct_pack::errc::no_buffer_space; } if SP_UNLIKELY (!has_value) { - if constexpr (expected) { + if constexpr (ylt::reflection::expected) { item = typename type::unexpected_type{typename type::error_type{}}; deserialize_one(item.error()); } @@ -1147,7 +1148,7 @@ class unpacker { } } else { - if constexpr (expected) { + if constexpr (ylt::reflection::expected) { if constexpr (!std::is_same_v) deserialize_one(item.value()); } @@ -1338,15 +1339,16 @@ class unpacker { }, item); } - else if constexpr (optional || expected) { + else if constexpr (ylt::reflection::optional || + ylt::reflection::expected) { bool has_value = item.has_value(); if (!has_value) { - if constexpr (expected) { + if constexpr (ylt::reflection::expected) { deserialize_one(item.error()); } } else { - if constexpr (expected) { + if constexpr (ylt::reflection::expected) { if constexpr (!std::is_same_v) deserialize_one(item.value()); }