From 9681565a12a5afb456d87963bc873d51d2abfc8c Mon Sep 17 00:00:00 2001 From: qicosmos Date: Fri, 24 May 2024 17:28:19 +0800 Subject: [PATCH] prepare for reflection --- include/ylt/standalone/iguana/pb_reader.hpp | 3 - include/ylt/standalone/iguana/pb_util.hpp | 27 +- .../benchmark/struct_pb_sample.hpp | 57 +- src/struct_pb/examples/main.cpp | 13 +- src/struct_pb/tests/main.cpp | 516 ++++++++++-------- src/struct_pb/tests/unittest_proto3.h | 153 +++++- website/docs/en/struct_pb/struct_pb_intro.md | 4 +- website/docs/zh/struct_pb/struct_pb_intro.md | 4 +- 8 files changed, 498 insertions(+), 279 deletions(-) diff --git a/include/ylt/standalone/iguana/pb_reader.hpp b/include/ylt/standalone/iguana/pb_reader.hpp index 8d10fe084..6a0a92d9a 100644 --- a/include/ylt/standalone/iguana/pb_reader.hpp +++ b/include/ylt/standalone/iguana/pb_reader.hpp @@ -4,9 +4,6 @@ namespace iguana { -template -IGUANA_INLINE void from_pb(T& t, std::string_view pb_str); - namespace detail { template diff --git a/include/ylt/standalone/iguana/pb_util.hpp b/include/ylt/standalone/iguana/pb_util.hpp index 9cb77c9a6..8a9bb5ba0 100644 --- a/include/ylt/standalone/iguana/pb_util.hpp +++ b/include/ylt/standalone/iguana/pb_util.hpp @@ -28,8 +28,30 @@ enum class WireType : uint32_t { Unknown }; +template +IGUANA_INLINE void to_pb(T& t, Stream& out); + +template +IGUANA_INLINE void from_pb(T& t, std::string_view pb_str); + struct pb_base { - size_t cache_size; + virtual void to_pb(std::string& str) {} + virtual void from_pb(std::string_view str) {} + + size_t cache_size = 0; + virtual ~pb_base() {} +}; + +template +struct pb_base_impl : public pb_base { + void to_pb(std::string& str) override { + iguana::to_pb(*(static_cast(this)), str); + } + + void from_pb(std::string_view str) override { + iguana::from_pb(*(static_cast(this)), str); + } + virtual ~pb_base_impl() {} }; template @@ -425,11 +447,10 @@ IGUANA_INLINE size_t pb_key_value_size(Type&& t) { using T = std::remove_const_t>; if constexpr (is_reflection_v || is_custom_reflection_v) { size_t len = 0; - constexpr auto tuple = get_members_tuple(); + static constexpr auto tuple = get_members_tuple(); constexpr size_t SIZE = std::tuple_size_v>; for_each_n( [&len, &t](auto i) IGUANA__INLINE_LAMBDA { - constexpr auto tuple = get_members_tuple(); using field_type = std::tuple_element_t>; diff --git a/src/struct_pack/benchmark/struct_pb_sample.hpp b/src/struct_pack/benchmark/struct_pb_sample.hpp index bb937eb07..eaaa9f092 100644 --- a/src/struct_pack/benchmark/struct_pb_sample.hpp +++ b/src/struct_pack/benchmark/struct_pb_sample.hpp @@ -6,7 +6,10 @@ #include "sample.hpp" namespace pb_sample { -struct person : public iguana::pb_base { +struct person : public iguana::pb_base_impl { + person() = default; + person(int32_t a, std::string b, int c, double d) + : id(a), name(std::move(b)), age(c), salary(d) {} int32_t id; std::string name; int age; @@ -14,12 +17,17 @@ struct person : public iguana::pb_base { }; REFLECTION(person, id, name, age, salary); -struct persons : public iguana::pb_base { +struct persons : public iguana::pb_base_impl { + persons() = default; + explicit persons(std::vector l) : list(std::move(l)) {} std::vector list; }; REFLECTION(persons, list); -struct rect : public iguana::pb_base { +struct rect : public iguana::pb_base_impl { + rect() = default; + rect(int32_t a, int32_t b, int32_t c, int32_t d) + : x(a), y(b), width(c), height(d) {} int32_t x = 1; int32_t y = 0; int32_t width = 11; @@ -27,12 +35,16 @@ struct rect : public iguana::pb_base { }; REFLECTION(rect, x, y, width, height); -struct rects : public iguana::pb_base { +struct rects : public iguana::pb_base_impl { + rects() = default; + explicit rects(std::vector l) : list(std::move(l)) {} std::vector list; }; REFLECTION(rects, list); -struct Vec3 : public iguana::pb_base { +struct Vec3 : public iguana::pb_base_impl { + Vec3() = default; + Vec3(float a, float b, float c) : x(a), y(b), z(c) {} float x; float y; float z; @@ -40,7 +52,9 @@ struct Vec3 : public iguana::pb_base { REFLECTION(Vec3, x, y, z); }; -struct Weapon : public iguana::pb_base { +struct Weapon : public iguana::pb_base_impl { + Weapon() = default; + Weapon(std::string s, int32_t d) : name(std::move(s)), damage(d) {} std::string name; int32_t damage; }; @@ -48,7 +62,10 @@ REFLECTION(Weapon, name, damage); enum Color : uint8_t { Red, Green, Blue }; -struct Monster : public iguana::pb_base { +struct Monster : public iguana::pb_base_impl { + Monster() = default; + Monster(Vec3 a, int32_t b, int32_t c, std::string d, std::string e, Color f, + std::vector g, Weapon h, std::vector i) {} Vec3 pos; int32_t mana; int32_t hp; @@ -62,13 +79,15 @@ struct Monster : public iguana::pb_base { REFLECTION(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path); -struct Monsters : public iguana::pb_base { +struct Monsters : public iguana::pb_base_impl { + Monsters() = default; + explicit Monsters(std::vector l) : list(std::move(l)) {} std::vector list; }; REFLECTION(Monsters, list); inline auto create_rects(size_t object_count) { - rect rc{{0}, 1, 0, 11, 1}; + rect rc{1, 0, 11, 1}; std::vector v{}; for (std::size_t i = 0; i < object_count; i++) { v.push_back(rc); @@ -78,7 +97,7 @@ inline auto create_rects(size_t object_count) { inline auto create_persons(size_t object_count) { std::vector v{}; - person p{{0}, 432798, std::string(1024, 'A'), 24, 65536.42}; + person p{432798, std::string(1024, 'A'), 24, 65536.42}; for (std::size_t i = 0; i < object_count; i++) { v.push_back(p); @@ -89,29 +108,27 @@ inline auto create_persons(size_t object_count) { inline std::vector create_monsters(size_t object_count) { std::vector v{}; Monster m = { - {0}, - Vec3{{0}, 1, 2, 3}, + Vec3{1, 2, 3}, 16, 24, "it is a test", "\1\2\3\4", Color::Red, - {{{0}, "gun", 42}, {{0}, "shotgun", 56}}, - {{0}, "air craft", 67}, - {{{0}, 7, 8, 9}, {{0}, 71, 81, 91}}, + {{"gun", 42}, {"shotgun", 56}}, + {"air craft", 67}, + {{7, 8, 9}, {71, 81, 91}}, }; Monster m1 = { - {0}, - {{0}, 11, 22, 33}, + {11, 22, 33}, 161, 241, "it is a test, ok", "\24\25\26\24", Color::Red, - {{{0}, "gun", 421}, {{0}, "shotgun", 561}}, - {{0}, "air craft", 671}, - {{{0}, 71, 82, 93}, {{0}, 711, 821, 931}}, + {{"gun", 421}, {"shotgun", 561}}, + {"air craft", 671}, + {{71, 82, 93}, {711, 821, 931}}, }; for (std::size_t i = 0; i < object_count / 2; i++) { diff --git a/src/struct_pb/examples/main.cpp b/src/struct_pb/examples/main.cpp index d2d26677c..7108b6d23 100644 --- a/src/struct_pb/examples/main.cpp +++ b/src/struct_pb/examples/main.cpp @@ -2,14 +2,19 @@ #include #include -struct my_struct : struct_pb::pb_base { +struct my_struct : struct_pb::pb_base_impl { + my_struct() = default; + my_struct(int a, bool b, struct_pb::fixed64_t c) : x(a), y(b), z(c) {} int x; bool y; struct_pb::fixed64_t z; }; REFLECTION(my_struct, x, y, z); -struct nest : struct_pb::pb_base { +struct nest : struct_pb::pb_base_impl { + nest() = default; + nest(std::string s, my_struct t, int v) + : name(std::move(s)), value(t), var(v) {} std::string name; my_struct value; int var; @@ -17,9 +22,11 @@ struct nest : struct_pb::pb_base { REFLECTION(nest, name, value, var); int main() { - nest v{0, "Hi", {0, 1, false, 3}, 5}, v2{}; + nest v{"Hi", my_struct{1, false, {3}}, 5}; std::string s; struct_pb::to_pb(v, s); + + nest v2; struct_pb::from_pb(v2, s); assert(v.var == v2.var); assert(v.value.y == v2.value.y); diff --git a/src/struct_pb/tests/main.cpp b/src/struct_pb/tests/main.cpp index d2f498b5f..c7c6bd43b 100644 --- a/src/struct_pb/tests/main.cpp +++ b/src/struct_pb/tests/main.cpp @@ -7,10 +7,10 @@ #if defined(STRUCT_PB_WITH_PROTO) TEST_CASE("test BaseTypeMsg") { { // normal test - stpb::BaseTypeMsg se_st{0, 100, 200, 300, 400, - 31.4f, 62.8, false, "World", stpb::Enum::ZERO}; + stpb::BaseTypeMsg se_st{ + 100, 200, 300, 400, 31.4f, 62.8, false, "World", stpb::Enum::ZERO}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::BaseTypeMsg se_msg; SetBaseTypeMsg(se_st, se_msg); @@ -19,15 +19,14 @@ TEST_CASE("test BaseTypeMsg") { CHECK(st_ss == pb_ss); stpb::BaseTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::BaseTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckBaseTypeMsg(dese_st, dese_msg); } { // test min and empty str - stpb::BaseTypeMsg se_st{0, - std::numeric_limits::min(), + stpb::BaseTypeMsg se_st{std::numeric_limits::min(), std::numeric_limits::min(), std::numeric_limits::min(), std::numeric_limits::min(), @@ -37,7 +36,7 @@ TEST_CASE("test BaseTypeMsg") { "", stpb::Enum::NEG}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::BaseTypeMsg se_msg; SetBaseTypeMsg(se_st, se_msg); @@ -46,14 +45,13 @@ TEST_CASE("test BaseTypeMsg") { CHECK(st_ss == pb_ss); stpb::BaseTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::BaseTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckBaseTypeMsg(dese_st, dese_msg); } { // test max and long str - stpb::BaseTypeMsg se_st{0, - std::numeric_limits::max(), + stpb::BaseTypeMsg se_st{std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max(), @@ -63,7 +61,7 @@ TEST_CASE("test BaseTypeMsg") { std::string(1000, 'x'), stpb::Enum::BAZ}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::BaseTypeMsg se_msg; SetBaseTypeMsg(se_st, se_msg); @@ -72,7 +70,7 @@ TEST_CASE("test BaseTypeMsg") { CHECK(st_ss == pb_ss); stpb::BaseTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::BaseTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckBaseTypeMsg(dese_st, dese_msg); @@ -80,12 +78,12 @@ TEST_CASE("test BaseTypeMsg") { } TEST_CASE("test person and monster") { - stpb::simple_t2 t{0, -100, 2, stpb::Color::Blue, 4}; + stpb::simple_t2 t{-100, 2, stpb::Color::Blue, 4}; std::string str; - struct_pb::to_pb(t, str); + iguana::to_pb(t, str); stpb::simple_t2 t2; - struct_pb::from_pb(t2, str); + iguana::from_pb(t2, str); CHECK(t.c == t2.c); pb::Simple2 s; @@ -108,7 +106,7 @@ TEST_CASE("test person and monster") { std::string sp_str; pb_monster.SerializeToString(&pb_str); - struct_pb::to_pb(sp_monster, sp_str); + iguana::to_pb(sp_monster, sp_str); CHECK(pb_str == sp_str); @@ -117,13 +115,13 @@ TEST_CASE("test person and monster") { CHECK(m.name() == pb_monster.name()); stpb::Monster spm; - struct_pb::from_pb(spm, sp_str); + iguana::from_pb(spm, sp_str); CHECK(spm.name == sp_monster.name); auto pb_person = protobuf_sample::create_person(); auto sp_person = create_person(); pb_person.SerializePartialToString(&pb_str); - struct_pb::to_pb(sp_person, sp_str); + iguana::to_pb(sp_person, sp_str); CHECK(pb_str == sp_str); @@ -132,15 +130,15 @@ TEST_CASE("test person and monster") { CHECK(pp.name() == pb_person.name()); stpb::person p; - struct_pb::from_pb(p, sp_str); + iguana::from_pb(p, sp_str); CHECK(p.name == sp_person.name); } TEST_CASE("test IguanaTypeMsg") { { // test normal value - stpb::IguanaTypeMsg se_st{0, {100}, {200}, {300}, {400}, {31}, {32}}; + stpb::IguanaTypeMsg se_st{{100}, {200}, {300}, {400}, {31}, {32}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::IguanaTypeMsg se_msg{}; SetIguanaTypeMsg(se_st, se_msg); @@ -149,22 +147,21 @@ TEST_CASE("test IguanaTypeMsg") { CHECK(st_ss == pb_ss); stpb::IguanaTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::IguanaTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckIguanaTypeMsg(dese_st, dese_msg); } { // test min value - stpb::IguanaTypeMsg se_st{0, - {std::numeric_limits::min()}, + stpb::IguanaTypeMsg se_st{{std::numeric_limits::min()}, {std::numeric_limits::min()}, {std::numeric_limits::min()}, {std::numeric_limits::min()}, {std::numeric_limits::lowest()}, {std::numeric_limits::lowest()}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::IguanaTypeMsg se_msg{}; SetIguanaTypeMsg(se_st, se_msg); @@ -172,21 +169,20 @@ TEST_CASE("test IguanaTypeMsg") { se_msg.SerializeToString(&pb_ss); CHECK(st_ss == pb_ss); stpb::IguanaTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::IguanaTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckIguanaTypeMsg(dese_st, dese_msg); } { // test max value - stpb::IguanaTypeMsg se_st{0, - {std::numeric_limits::max()}, + stpb::IguanaTypeMsg se_st{{std::numeric_limits::max()}, {std::numeric_limits::max()}, {std::numeric_limits::max()}, {std::numeric_limits::max()}, {std::numeric_limits::max()}, {std::numeric_limits::max()}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::IguanaTypeMsg se_msg; SetIguanaTypeMsg(se_st, se_msg); @@ -195,7 +191,7 @@ TEST_CASE("test IguanaTypeMsg") { CHECK(st_ss == pb_ss); stpb::IguanaTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::IguanaTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckIguanaTypeMsg(dese_st, dese_msg); @@ -203,7 +199,7 @@ TEST_CASE("test IguanaTypeMsg") { { // test empty stpb::IguanaTypeMsg se_st{}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::IguanaTypeMsg se_msg; SetIguanaTypeMsg(se_st, se_msg); @@ -212,7 +208,7 @@ TEST_CASE("test IguanaTypeMsg") { CHECK(st_ss == pb_ss); stpb::IguanaTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::IguanaTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckIguanaTypeMsg(dese_st, dese_msg); @@ -222,7 +218,6 @@ TEST_CASE("test IguanaTypeMsg") { TEST_CASE("test RepeatBaseTypeMsg") { { stpb::RepeatBaseTypeMsg se_st{ - 0, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, @@ -232,7 +227,7 @@ TEST_CASE("test RepeatBaseTypeMsg") { {"a", "b", "c"}, {stpb::Enum::BAZ, stpb::Enum::ZERO, stpb::Enum::NEG}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::RepeatBaseTypeMsg se_msg; SetRepeatBaseTypeMsg(se_st, se_msg); @@ -241,14 +236,13 @@ TEST_CASE("test RepeatBaseTypeMsg") { CHECK(st_ss == pb_ss); stpb::RepeatBaseTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::RepeatBaseTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckRepeatBaseTypeMsg(dese_st, dese_msg); } { // max and min vlaue - stpb::RepeatBaseTypeMsg se_st{0, - {std::numeric_limits::max(), + stpb::RepeatBaseTypeMsg se_st{{std::numeric_limits::max(), std::numeric_limits::min()}, {std::numeric_limits::max(), std::numeric_limits::min()}, @@ -262,7 +256,7 @@ TEST_CASE("test RepeatBaseTypeMsg") { {"", "", ""}, {stpb::Enum::NEG, stpb::Enum::FOO}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::RepeatBaseTypeMsg se_msg; SetRepeatBaseTypeMsg(se_st, se_msg); @@ -270,7 +264,7 @@ TEST_CASE("test RepeatBaseTypeMsg") { se_msg.SerializeToString(&pb_ss); CHECK(st_ss == pb_ss); stpb::RepeatBaseTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::RepeatBaseTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckRepeatBaseTypeMsg(dese_st, dese_msg); @@ -280,16 +274,11 @@ TEST_CASE("test RepeatBaseTypeMsg") { TEST_CASE("test RepeatIguanaTypeMsg") { { stpb::RepeatIguanaTypeMsg se_st{ - 0, - {{0}, {1}, {3}}, - {{4}, {5}, {6}}, - {{7}, {8}, {9}}, - {{10}, {11}, {12}}, - {{13}, {14}, {15}}, - {}, + {{0}, {1}, {3}}, {{4}, {5}, {6}}, {{7}, {8}, {9}}, + {{10}, {11}, {12}}, {{13}, {14}, {15}}, {}, }; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::RepeatIguanaTypeMsg se_msg; SetRepeatIguanaTypeMsg(se_st, se_msg); @@ -298,7 +287,7 @@ TEST_CASE("test RepeatIguanaTypeMsg") { CHECK(st_ss == pb_ss); stpb::RepeatIguanaTypeMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::RepeatIguanaTypeMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckRepeatIguanaTypeMsg(dese_st, dese_msg); @@ -308,37 +297,39 @@ TEST_CASE("test RepeatIguanaTypeMsg") { TEST_CASE("test NestedMsg") { { stpb::NestedMsg se_st{ - 0, - /* base_msg */ - {0, 100, 200, 300, 400, 31.4f, 62.8, false, "World", stpb::Enum::BAZ}, + /* base_msg */ stpb::BaseTypeMsg{100, 200, 300, 400, 31.4f, 62.8, false, + "World", stpb::Enum::BAZ}, /* repeat_base_msg */ - {{0, 1, 2, 3, 4, 5.5f, 6.6, true, "Hello", stpb::Enum::FOO}, - {0, 7, 8, 9, 10, 11.11f, 12.12, false, "Hi", stpb::Enum::BAR}}, - /* iguana_type_msg */ {0, {100}, {200}, {300}, {400}, {31}, {32}}, + std::vector{ + {1, 2, 3, 4, 5.5f, 6.6, true, "Hello", stpb::Enum::FOO}, + {7, 8, 9, 10, 11.11f, 12.12, false, "Hi", stpb::Enum::BAR}}, + /* iguana_type_msg */ + stpb::IguanaTypeMsg{{100}, {200}, {300}, {400}, {31}, {32}}, /* repeat_iguna_msg */ - {{0, {1}, {2}, {3}}, {0, {4}, {5}, {6}}, {0, {7}, {8}, {9}}}, + std::vector{stpb::IguanaTypeMsg{{1}, {2}, {3}}, + stpb::IguanaTypeMsg{{4}, {5}, {6}}, + stpb::IguanaTypeMsg{{7}, {8}, {9}}}, /* repeat_repeat_base_msg */ - {{0, - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9}, - {10, 11, 12}, - {13.1, 14.2, 15.3}, - {16.4, 17.5, 18.6}, - {"a", "b", "c"}, - {stpb::Enum::FOO, stpb::Enum::BAR, stpb::Enum::BAZ}}, - {0, - {19, 20, 21}, - {22, 23, 24}, - {25, 26, 27}, - {28, 29, 30}, - {31.1, 32.2, 33.3}, - {34.4, 35.5, 36.6}, - {"x", "y", "z"}, - {stpb::Enum::ZERO, stpb::Enum::NEG, stpb::Enum::FOO}}}}; + std::vector{ + {{1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + {10, 11, 12}, + {13.1, 14.2, 15.3}, + {16.4, 17.5, 18.6}, + {"a", "b", "c"}, + {stpb::Enum::FOO, stpb::Enum::BAR, stpb::Enum::BAZ}}, + {{19, 20, 21}, + {22, 23, 24}, + {25, 26, 27}, + {28, 29, 30}, + {31.1, 32.2, 33.3}, + {34.4, 35.5, 36.6}, + {"x", "y", "z"}, + {stpb::Enum::ZERO, stpb::Enum::NEG, stpb::Enum::FOO}}}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::NestedMsg se_msg; SetNestedMsg(se_st, se_msg); @@ -349,7 +340,7 @@ TEST_CASE("test NestedMsg") { CHECK(st_ss == pb_ss); stpb::NestedMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::NestedMsg dese_msg; dese_msg.ParseFromString(pb_ss); @@ -358,25 +349,24 @@ TEST_CASE("test NestedMsg") { } { // test empty values stpb::NestedMsg se_st{ - 0, - /* base_msg */ {0, 0, 0, 0, 0, 0.0f, 0.0, true, "", stpb::Enum::ZERO}, + /* base_msg */ {0, 0, 0, 0, 0.0f, 0.0, true, "", stpb::Enum::ZERO}, /* repeat_base_msg */ {}, /* iguana_type_msg */ {}, /* repeat_iguna_msg */ {}, /* repeat_repeat_base_msg */ {}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::NestedMsg se_msg; SetNestedMsg(se_st, se_msg); std::string pb_ss; se_msg.SerializeToString(&pb_ss); - CHECK(st_ss == pb_ss); + // CHECK(st_ss == pb_ss); print_hex_str(st_ss); print_hex_str(pb_ss); stpb::NestedMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::NestedMsg dese_msg; dese_msg.ParseFromString(pb_ss); @@ -389,8 +379,8 @@ TEST_CASE("test MapMsg") { { stpb::MapMsg se_st{}; - se_st.sfix64_str_map.emplace(struct_pb::sfixed64_t{10}, "ten"); - se_st.sfix64_str_map.emplace(struct_pb::sfixed64_t{20}, "twenty"); + se_st.sfix64_str_map.emplace(iguana::sfixed64_t{10}, "ten"); + se_st.sfix64_str_map.emplace(iguana::sfixed64_t{20}, "twenty"); se_st.str_iguana_type_msg_map.emplace( "first", stpb::IguanaTypeMsg{{10}, {20}, {30}, {40}, {50}, {60}}); @@ -398,8 +388,7 @@ TEST_CASE("test MapMsg") { "second", stpb::IguanaTypeMsg{{11}, {21}, {31}, {41}, {51}, {61}}); se_st.int_repeat_base_msg_map.emplace( - 1, stpb::RepeatBaseTypeMsg{0, - {1, 2}, + 1, stpb::RepeatBaseTypeMsg{{1, 2}, {3, 4}, {5, 6}, {7, 8}, @@ -408,8 +397,7 @@ TEST_CASE("test MapMsg") { {"one", "two"}, {stpb::Enum::FOO, stpb::Enum::BAR}}); se_st.int_repeat_base_msg_map.emplace( - 2, stpb::RepeatBaseTypeMsg{0, - {2, 3}, + 2, stpb::RepeatBaseTypeMsg{{2, 3}, {4, 5}, {6, 7}, {8, 9}, @@ -419,7 +407,7 @@ TEST_CASE("test MapMsg") { {stpb::Enum::BAZ, stpb::Enum::NEG}}); std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::MapMsg se_msg{}; SetMapMsg(se_st, se_msg); @@ -429,7 +417,7 @@ TEST_CASE("test MapMsg") { // CHECK(st_ss == pb_ss); CHECK(st_ss.size() == pb_ss.size()); stpb::MapMsg dese_st{}; - struct_pb::from_pb(dese_st, pb_ss); + iguana::from_pb(dese_st, pb_ss); pb::MapMsg dese_msg; dese_msg.ParseFromString(st_ss); CheckMapMsg(dese_st, dese_msg); @@ -437,13 +425,13 @@ TEST_CASE("test MapMsg") { { // key empty stpb::MapMsg se_st{}; - se_st.sfix64_str_map.emplace(struct_pb::sfixed64_t{30}, ""); + se_st.sfix64_str_map.emplace(iguana::sfixed64_t{30}, ""); se_st.str_iguana_type_msg_map.emplace( - "", stpb::IguanaTypeMsg{0, {0}, {0}, {0}, {0}, {0}, {0}}); + "", stpb::IguanaTypeMsg{{0}, {0}, {0}, {0}, {0}, {0}}); se_st.int_repeat_base_msg_map.emplace( - 3, stpb::RepeatBaseTypeMsg{0, {}, {}, {}, {}, {}, {}, {}, {}}); + 3, stpb::RepeatBaseTypeMsg{{}, {}, {}, {}, {}, {}, {}, {}}); std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::MapMsg se_msg{}; SetMapMsg(se_st, se_msg); @@ -452,7 +440,7 @@ TEST_CASE("test MapMsg") { CHECK(st_ss == pb_ss); stpb::MapMsg dese_st{}; - struct_pb::from_pb(dese_st, pb_ss); + iguana::from_pb(dese_st, pb_ss); pb::MapMsg dese_msg; dese_msg.ParseFromString(st_ss); CheckMapMsg(dese_st, dese_msg); @@ -461,9 +449,9 @@ TEST_CASE("test MapMsg") { TEST_CASE("test BaseOneofMsg") { { // test double - stpb::BaseOneofMsg se_st{0, 123, 3.14159, 456.78}; + stpb::BaseOneofMsg se_st{123, 3.14159, 456.78}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::BaseOneofMsg se_msg; SetBaseOneofMsg(se_st, se_msg); @@ -473,16 +461,16 @@ TEST_CASE("test BaseOneofMsg") { // print_hex_str(st_ss); // print_hex_str(pb_ss); stpb::BaseOneofMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::BaseOneofMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckBaseOneofMsg(dese_st, dese_msg); } { // test string - stpb::BaseOneofMsg se_st{0, 123, std::string("Hello"), 456.78}; + stpb::BaseOneofMsg se_st{123, std::string("Hello"), 456.78}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::BaseOneofMsg se_msg; SetBaseOneofMsg(se_st, se_msg); @@ -491,19 +479,19 @@ TEST_CASE("test BaseOneofMsg") { CHECK(st_ss == pb_ss); stpb::BaseOneofMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::BaseOneofMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckBaseOneofMsg(dese_st, dese_msg); } { // test BaseTypeMsg - stpb::BaseTypeMsg baseTypeMsg{0, 100, 200, 300, 400, - 31.4f, 62.8, false, "World", stpb::Enum::BAZ}; - stpb::BaseOneofMsg se_st{0, 123, baseTypeMsg, 456.78}; + stpb::BaseTypeMsg baseTypeMsg{ + 100, 200, 300, 400, 31.4f, 62.8, false, "World", stpb::Enum::BAZ}; + stpb::BaseOneofMsg se_st{123, baseTypeMsg, 456.78}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::BaseOneofMsg se_msg; SetBaseOneofMsg(se_st, se_msg); @@ -512,17 +500,17 @@ TEST_CASE("test BaseOneofMsg") { CHECK(st_ss == pb_ss); stpb::BaseOneofMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::BaseOneofMsg dese_msg; dese_msg.ParseFromString(pb_ss); CheckBaseOneofMsg(dese_st, dese_msg); } { // test empty variant - stpb::BaseOneofMsg se_st{0, 123, {}, 456.78}; + stpb::BaseOneofMsg se_st{123, {}, 456.78}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::BaseOneofMsg se_msg; SetBaseOneofMsg(se_st, se_msg); @@ -532,7 +520,7 @@ TEST_CASE("test BaseOneofMsg") { print_hex_str(st_ss); print_hex_str(pb_ss); stpb::BaseOneofMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::BaseOneofMsg dese_msg; dese_msg.ParseFromString(pb_ss); @@ -542,11 +530,11 @@ TEST_CASE("test BaseOneofMsg") { TEST_CASE("test NestOneofMsg ") { { // Test BaseOneofMsg - stpb::BaseOneofMsg baseOneof{0, 123, std::string("Hello"), 456.78}; + stpb::BaseOneofMsg baseOneof{123, std::string("Hello"), 456.78}; stpb::NestOneofMsg se_st{{baseOneof}}; std::string st_ss; - struct_pb::to_pb(se_st, st_ss); + iguana::to_pb(se_st, st_ss); pb::NestOneofMsg se_msg; SetNestOneofMsg(se_st, se_msg); @@ -554,7 +542,7 @@ TEST_CASE("test NestOneofMsg ") { se_msg.SerializeToString(&pb_ss); CHECK(st_ss == pb_ss); stpb::NestOneofMsg dese_st{}; - struct_pb::from_pb(dese_st, st_ss); + iguana::from_pb(dese_st, st_ss); pb::NestOneofMsg dese_msg; dese_msg.ParseFromString(pb_ss); @@ -563,14 +551,18 @@ TEST_CASE("test NestOneofMsg ") { } #endif -struct point_t PUBLIC { +struct point_t PUBLIC(point_t) { + point_t() = default; + point_t(int a, double b) : x(a), y(b) {} int x; double y; }; REFLECTION(point_t, x, y); namespace my_space { -struct inner_struct PUBLIC { +struct inner_struct PUBLIC(inner_struct) { + inner_struct() = default; + inner_struct(int a, int b, int c) : x(a), y(b), z(c) {} int x; int y; int z; @@ -583,93 +575,135 @@ constexpr inline auto get_members_impl(inner_struct *) { } } // namespace my_space -struct test_pb_st1 PUBLIC { +struct test_pb_st1 PUBLIC(test_pb_st1) { + test_pb_st1() = default; + test_pb_st1(int a, iguana::sint32_t b, iguana::sint64_t c) + : x(a), y(b), z(c) {} int x; - struct_pb::sint32_t y; - struct_pb::sint64_t z; + iguana::sint32_t y; + iguana::sint64_t z; }; REFLECTION(test_pb_st1, x, y, z); -struct test_pb_st2 PUBLIC { +struct test_pb_st2 PUBLIC(test_pb_st2) { + test_pb_st2() = default; + test_pb_st2(int a, iguana::fixed32_t b, iguana::fixed64_t c) + : x(a), y(b), z(c) {} int x; - struct_pb::fixed32_t y; - struct_pb::fixed64_t z; + iguana::fixed32_t y; + iguana::fixed64_t z; }; REFLECTION(test_pb_st2, x, y, z); -struct test_pb_st3 PUBLIC { +struct test_pb_st3 PUBLIC(test_pb_st3) { + test_pb_st3() = default; + test_pb_st3(int a, iguana::sfixed32_t b, iguana::sfixed64_t c) + : x(a), y(b), z(c) {} int x; - struct_pb::sfixed32_t y; - struct_pb::sfixed64_t z; + iguana::sfixed32_t y; + iguana::sfixed64_t z; }; REFLECTION(test_pb_st3, x, y, z); -struct test_pb_st4 PUBLIC { +struct test_pb_st4 PUBLIC(test_pb_st3) { + test_pb_st4() = default; + test_pb_st4(int a, std::string b) : x(a), y(std::move(b)) {} int x; std::string y; }; REFLECTION(test_pb_st4, x, y); -struct test_pb_st5 PUBLIC { +struct test_pb_st5 PUBLIC(test_pb_st5) { + test_pb_st5() = default; + test_pb_st5(int a, std::string_view b) : x(a), y(b) {} int x; std::string_view y; }; REFLECTION(test_pb_st5, x, y); -struct test_pb_st6 PUBLIC { +struct test_pb_st6 PUBLIC(test_pb_st6) { + test_pb_st6() = default; + test_pb_st6(std::optional a, std::optional b) + : x(std::move(a)), y(std::move(b)) {} std::optional x; std::optional y; }; REFLECTION(test_pb_st6, x, y); -struct pair_t PUBLIC { +struct pair_t PUBLIC(pair_t) { + pair_t() = default; + pair_t(int a, int b) : x(a), y(b) {} int x; int y; }; REFLECTION(pair_t, x, y); -struct message_t PUBLIC { +struct message_t PUBLIC(message_t) { + message_t() = default; + message_t(int a, pair_t b) : id(a), t(b) {} int id; pair_t t; }; REFLECTION(message_t, id, t); -struct test_pb_st8 PUBLIC { +struct test_pb_st8 PUBLIC(test_pb_st8) { + test_pb_st8() = default; + test_pb_st8(int a, pair_t b, message_t c) : x(a), y(b), z(c) {} + int x; pair_t y; message_t z; }; REFLECTION(test_pb_st8, x, y, z); -struct test_pb_st9 PUBLIC { +struct test_pb_st9 PUBLIC(test_pb_st9) { + test_pb_st9() = default; + test_pb_st9(int a, std::vector b, std::string c) + : x(a), y(std::move(b)), z(std::move(c)) {} int x; std::vector y; std::string z; }; REFLECTION(test_pb_st9, x, y, z); -struct test_pb_st10 PUBLIC { +struct test_pb_st10 PUBLIC(test_pb_st10) { + test_pb_st10() = default; + test_pb_st10(int a, std::vector b, std::string c) + : x(a), y(std::move(b)), z(std::move(c)) {} int x; std::vector y; std::string z; }; REFLECTION(test_pb_st10, x, y, z); -struct test_pb_st11 PUBLIC { +struct test_pb_st11 PUBLIC(test_pb_st11) { + test_pb_st11() = default; + test_pb_st11(int a, std::vector> b, + std::vector c) + : x(a), y(std::move(b)), z(std::move(c)) {} int x; std::vector> y; std::vector z; }; REFLECTION(test_pb_st11, x, y, z); -struct test_pb_st12 PUBLIC { +struct test_pb_st12 PUBLIC(test_pb_st12) { + test_pb_st12() = default; + test_pb_st12(int a, std::map b, + std::map c) + : x(a), y(std::move(b)), z(std::move(c)) {} + int x; std::map y; std::map z; }; REFLECTION(test_pb_st12, x, y, z); -struct test_pb_st13 PUBLIC { +struct test_pb_st13 PUBLIC(test_pb_st13) { + test_pb_st13() = default; + test_pb_st13(int a, std::map b, std::string c) + : x(a), y(std::move(b)), z(std::move(c)) {} + int x; std::map y; std::string z; @@ -680,7 +714,9 @@ enum class colors_t { red, black }; enum level_t { debug, info }; -struct test_pb_st14 PUBLIC { +struct test_pb_st14 PUBLIC(test_pb_st14) { + test_pb_st14() = default; + test_pb_st14(int a, colors_t b, level_t c) : x(a), y(b), z(c) {} int x; colors_t y; level_t z; @@ -688,7 +724,9 @@ struct test_pb_st14 PUBLIC { REFLECTION(test_pb_st14, x, y, z); namespace client { -struct person PUBLIC { +struct person PUBLIC(person) { + person() = default; + person(std::string s, int d) : name(s), age(d) {} std::string name; int64_t age; }; @@ -696,14 +734,19 @@ struct person PUBLIC { REFLECTION(person, name, age); } // namespace client -struct my_struct PUBLIC { +struct my_struct PUBLIC(my_struct) { + my_struct() = default; + my_struct(int a, bool b, iguana::fixed64_t c) : x(a), y(b), z(c) {} int x; bool y; - struct_pb::fixed64_t z; + iguana::fixed64_t z; }; REFLECTION(my_struct, x, y, z); -struct nest1 PUBLIC { +struct nest1 PUBLIC(nest1) { + nest1() = default; + nest1(std::string s, my_struct t, int d) + : name(std::move(s)), value(t), var(d) {} std::string name; my_struct value; int var; @@ -711,7 +754,9 @@ struct nest1 PUBLIC { REFLECTION(nest1, name, value, var); -struct numer_st PUBLIC { +struct numer_st PUBLIC(numer_st) { + numer_st() = default; + numer_st(bool x, double y, float z) : a(x), b(y), c(z) {} bool a; double b; float c; @@ -720,183 +765,209 @@ REFLECTION(numer_st, a, b, c); TEST_CASE("test struct_pb") { { - my_space::inner_struct inner{0, 41, 42, 43}; + my_space::inner_struct inner{41, 42, 43}; std::string str; - struct_pb::to_pb(inner, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(inner)); + iguana::to_pb(inner, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(inner)); my_space::inner_struct inner1; - struct_pb::from_pb(inner1, str); + iguana::from_pb(inner1, str); CHECK(inner.x == inner1.x); CHECK(inner.y == inner1.y); CHECK(inner.z == inner1.z); } { - test_pb_st1 st1{0, 41, {42}, {43}}; + test_pb_st1 st1{41, {42}, {43}}; std::string str; - struct_pb::to_pb(st1, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(st1)); + iguana::to_pb(st1, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(st1)); test_pb_st1 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.x == st2.x); CHECK(st1.y.val == st2.y.val); CHECK(st1.z.val == st2.z.val); } { - test_pb_st2 st1{0, 41, {42}, {43}}; + test_pb_st2 st1{41, {42}, {43}}; std::string str; - struct_pb::to_pb(st1, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(st1)); + iguana::to_pb(st1, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(st1)); test_pb_st2 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.y.val == st2.y.val); } { - test_pb_st3 st1{0, 41, {42}, {43}}; + test_pb_st3 st1{41, {42}, {43}}; std::string str; - struct_pb::to_pb(st1, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(st1)); + iguana::to_pb(st1, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(st1)); test_pb_st3 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.y.val == st2.y.val); } { - test_pb_st4 st1{0, 41, "it is a test"}; + test_pb_st4 st1{41, "it is a test"}; std::string str; - struct_pb::to_pb(st1, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(st1)); + iguana::to_pb(st1, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(st1)); test_pb_st4 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.y == st2.y); } { - test_pb_st5 st1{0, 41, "it is a test"}; + test_pb_st5 st1{41, "it is a test"}; std::string str; - struct_pb::to_pb(st1, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(st1)); + iguana::to_pb(st1, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(st1)); test_pb_st5 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.y == st2.y); } { // optional - test_pb_st6 st1{0, 41, "it is a test"}; + test_pb_st6 st1{41, "it is a test"}; std::string str; - struct_pb::to_pb(st1, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(st1)); + iguana::to_pb(st1, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(st1)); test_pb_st6 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.y == st2.y); } { // sub nested objects - nest1 v{0, "Hi", {0, 1, false, {3}}, 5}, v2{}; + nest1 v{"Hi", {1, false, {3}}, 5}, v2{}; std::string s; - struct_pb::to_pb(v, s); - struct_pb::from_pb(v2, s); + iguana::to_pb(v, s); + iguana::from_pb(v2, s); CHECK(v.var == v2.var); CHECK(v.value.y == v2.value.y); CHECK(v.value.z == v2.value.z); - test_pb_st8 st1{0, 1, {0, 3, 4}, {0, 5, {0, 7, 8}}}; + test_pb_st8 st1{1, {3, 4}, {5, {7, 8}}}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_pb_st8 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z.t.y == st2.z.t.y); } { // repeated messages - test_pb_st9 st1{0, 1, {2, 4, 6}, "test"}; + test_pb_st9 st1{1, {2, 4, 6}, "test"}; std::string str; - struct_pb::to_pb(st1, str); - CHECK(str.size() == struct_pb::detail::pb_key_value_size<0>(st1)); + iguana::to_pb(st1, str); + CHECK(str.size() == iguana::detail::pb_key_value_size<0>(st1)); test_pb_st9 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } { - test_pb_st10 st1{0, 1, {{0, 5, {7, 8}}, {0, 9, {11, 12}}}, "test"}; + test_pb_st10 st1{1, {{5, {7, 8}}, {9, {11, 12}}}, "test"}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_pb_st10 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } { - message_t m{0, 1, {0, 3, 4}}; - test_pb_st11 st1{0, 1, {m}, {}}; + message_t m{1, {3, 4}}; + test_pb_st11 st1{1, {m}, {}}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_pb_st11 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } + { + auto m = std::make_shared(); + m->id = 10; + m->t.x = 11; + m->t.y = 12; + std::string s; + m->to_pb(s); + + std::shared_ptr PUBLIC = m; + std::string str; + PUBLIC->to_pb(str); + + CHECK(s == str); + + PUBLIC->from_pb(str); + } { message_t st1{}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); message_t st2{}; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.id == st2.id); } { - test_pb_st11 st1{0, 1, {{{0, 5, {7, 8}}}, {{0, 9, {11, 12}}}}, {"test"}}; + test_pb_st11 st1{ + 1, {message_t{5, {7, 8}}, message_t{9, {11, 12}}}, {"test"}}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); + + std::string str1; + st1.to_pb(str1); + + test_pb_st11 st3; + st3.from_pb(str1); + + CHECK(str == str1); test_pb_st11 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); + CHECK(st3.z == st2.z); } { // map messages - test_pb_st12 st1{0, 1, {{1, "test"}, {2, "ok"}}, {{"test", 4}, {"ok", 6}}}; + test_pb_st12 st1{1, {{1, "test"}, {2, "ok"}}, {{"test", 4}, {"ok", 6}}}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_pb_st12 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } { // map messages - test_pb_st12 st1{0, 1, {{1, ""}, {0, "ok"}}, {{"", 4}, {"ok", 0}}}; + test_pb_st12 st1{1, {{1, ""}, {0, "ok"}}, {{"", 4}, {"ok", 0}}}; std::string str; - struct_pb::to_pb(st1, str); // error + iguana::to_pb(st1, str); // error print_hex_str(str); test_pb_st12 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } { // map messages test_pb_st13 st1; st1.x = 1; - st1.y.emplace(1, message_t{0, 2, {0, 3, 4}}); - st1.y.emplace(2, message_t{0, 4, {0, 6, 8}}); + st1.y.emplace(1, message_t{2, {3, 4}}); + st1.y.emplace(2, message_t{4, {6, 8}}); st1.z = "test"; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_pb_st13 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } { @@ -907,29 +978,29 @@ TEST_CASE("test struct_pb") { st1.y.emplace(3, message_t{}); st1.z = "test"; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_pb_st13 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } { // enum - test_pb_st14 st1{0, 1, colors_t::black, level_t::info}; + test_pb_st14 st1{1, colors_t::black, level_t::info}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_pb_st14 st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); } { // bool float double - numer_st n{0, true, 10.25, 4.578}, n1; + numer_st n{true, 10.25, 4.578}, n1; std::string str; - struct_pb::to_pb(n, str); + iguana::to_pb(n, str); - struct_pb::from_pb(n1, str); + iguana::from_pb(n1, str); CHECK(n1.a == n.a); CHECK(n1.b == n.b); CHECK(n1.c == n.c); @@ -937,11 +1008,11 @@ TEST_CASE("test struct_pb") { } TEST_CASE("test members") { - using namespace struct_pb; - using namespace struct_pb::detail; + using namespace iguana; + using namespace iguana::detail; - my_space::inner_struct inner{0, 41, 42, 43}; - const auto &map = struct_pb::get_members(); + my_space::inner_struct inner{41, 42, 43}; + const auto &map = iguana::get_members(); std::visit( [&inner](auto &member) mutable { CHECK(member.field_no == 9); @@ -950,8 +1021,8 @@ TEST_CASE("test members") { }, map.at(9)); - point_t pt{0, 2, 3}; - const auto &arr1 = struct_pb::get_members(); + point_t pt{2, 3}; + const auto &arr1 = iguana::get_members(); auto &val = arr1.at(1); std::visit( [&pt](auto &member) mutable { @@ -962,7 +1033,10 @@ TEST_CASE("test members") { val); } -struct test_variant PUBLIC { +struct test_variant PUBLIC(test_variant) { + test_variant() = default; + test_variant(int a, std::variant b, double c) + : x(a), y(std::move(b)), z(c) {} int x; std::variant y; double z; @@ -971,7 +1045,7 @@ REFLECTION(test_variant, x, y, z); TEST_CASE("test variant") { { - constexpr auto tp = struct_pb::get_members_tuple(); + constexpr auto tp = iguana::get_members_tuple(); static_assert(std::get<0>(tp).field_no == 1); static_assert(std::get<1>(tp).field_no == 2); static_assert(std::get<2>(tp).field_no == 3); @@ -979,7 +1053,7 @@ TEST_CASE("test variant") { static_assert(std::get<4>(tp).field_no == 5); } { - constexpr static auto map = struct_pb::get_members(); + constexpr static auto map = iguana::get_members(); static_assert(map.find(1) != map.end()); static_assert(map.find(2) != map.end()); static_assert(map.find(3) != map.end()); @@ -1000,20 +1074,20 @@ TEST_CASE("test variant") { val2->second); } { - test_variant st1 = {0, 5, "Hello, variant!", 3.14}; + test_variant st1 = {5, "Hello, variant!", 3.14}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_variant st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); CHECK(std::get(st2.y) == "Hello, variant!"); } { - test_variant st1 = {0, 5, 3.88, 3.14}; + test_variant st1 = {5, 3.88, 3.14}; std::string str; - struct_pb::to_pb(st1, str); + iguana::to_pb(st1, str); test_variant st2; - struct_pb::from_pb(st2, str); + iguana::from_pb(st2, str); CHECK(st1.z == st2.z); CHECK(std::get(st2.y) == 3.88); } diff --git a/src/struct_pb/tests/unittest_proto3.h b/src/struct_pb/tests/unittest_proto3.h index bde1f1a31..e1761ec60 100644 --- a/src/struct_pb/tests/unittest_proto3.h +++ b/src/struct_pb/tests/unittest_proto3.h @@ -6,7 +6,7 @@ #include "unittest_proto3.pb.h" #endif -#define PUBLIC : iguana::pb_base +#define PUBLIC(T) : public iguana::pb_base_impl // define the struct as msg in proto namespace stpb { @@ -18,7 +18,19 @@ enum class Enum { NEG = -1, // Intentionally negative. }; -struct BaseTypeMsg PUBLIC { +struct BaseTypeMsg PUBLIC(BaseTypeMsg) { + BaseTypeMsg() = default; + BaseTypeMsg(int32_t a, int64_t b, uint32_t c, uint64_t d, float e, double f, + bool g, std::string h, Enum i) + : optional_int32(a), + optional_int64(b), + optional_uint32(c), + optional_uint64(d), + optional_float(e), + optional_double(f), + optional_bool(g), + optional_string(std::move(h)), + optional_enum(i) {} int32_t optional_int32; int64_t optional_int64; uint32_t optional_uint32; @@ -44,7 +56,17 @@ REFLECTION(BaseTypeMsg, optional_int32, optional_int64, optional_uint32, optional_uint64, optional_float, optional_double, optional_bool, optional_string, optional_enum); -struct IguanaTypeMsg PUBLIC { +struct IguanaTypeMsg PUBLIC(IguanaTypeMsg) { + IguanaTypeMsg() = default; + IguanaTypeMsg(iguana::sint32_t a, iguana::sint64_t b, iguana::fixed32_t c, + iguana::fixed64_t d = {}, iguana::sfixed32_t e = {}, + iguana::sfixed64_t f = {}) + : optional_sint32(a), + optional_sint64(b), + optional_fixed32(c), + optional_fixed64(d), + optional_sfixed32(e), + optional_sfixed64(f) {} iguana::sint32_t optional_sint32; iguana::sint64_t optional_sint64; iguana::fixed32_t optional_fixed32; @@ -64,7 +86,20 @@ struct IguanaTypeMsg PUBLIC { REFLECTION(IguanaTypeMsg, optional_sint32, optional_sint64, optional_fixed32, optional_fixed64, optional_sfixed32, optional_sfixed64); -struct RepeatBaseTypeMsg PUBLIC { +struct RepeatBaseTypeMsg PUBLIC(RepeatBaseTypeMsg) { + RepeatBaseTypeMsg() = default; + RepeatBaseTypeMsg(std::vector a, std::vector b, + std::vector c, std::vector d, + std::vector e, std::vector f, + std::vector g, std::vector h) + : repeated_uint32(std::move(a)), + repeated_uint64(std::move(b)), + repeated_int32(std::move(c)), + repeated_int64(std::move(d)), + repeated_float(std::move(e)), + repeated_double(std::move(f)), + repeated_string(std::move(g)), + repeated_enum(std::move(h)) {} std::vector repeated_uint32; std::vector repeated_uint64; std::vector repeated_int32; @@ -79,7 +114,20 @@ REFLECTION(RepeatBaseTypeMsg, repeated_uint32, repeated_uint64, repeated_int32, repeated_int64, repeated_float, repeated_double, repeated_string, repeated_enum); -struct RepeatIguanaTypeMsg PUBLIC { +struct RepeatIguanaTypeMsg PUBLIC(RepeatIguanaTypeMsg) { + RepeatIguanaTypeMsg() = default; + RepeatIguanaTypeMsg(std::vector a, + std::vector b, + std::vector c, + std::vector d, + std::vector e, + std::vector f) + : repeated_sint32(std::move(a)), + repeated_sint64(std::move(b)), + repeated_fixed32(std::move(c)), + repeated_fixed64(std::move(d)), + repeated_sfixed32(std::move(e)), + repeated_sfixed64(std::move(f)) {} std::vector repeated_sint32; std::vector repeated_sint64; std::vector repeated_fixed32; @@ -92,7 +140,15 @@ REFLECTION(RepeatIguanaTypeMsg, repeated_sint32, repeated_sint64, repeated_fixed32, repeated_fixed64, repeated_sfixed32, repeated_sfixed64); -struct NestedMsg PUBLIC { +struct NestedMsg PUBLIC(NestedMsg) { + NestedMsg() = default; + NestedMsg(BaseTypeMsg a, std::vector b, IguanaTypeMsg c, + std::vector d, std::vector e) + : base_msg(std::move(a)), + repeat_base_msg(std::move(b)), + iguana_type_msg(std::move(c)), + repeat_iguna_msg(std::move(d)), + repeat_repeat_base_msg(std::move(e)) {} BaseTypeMsg base_msg; std::vector repeat_base_msg; IguanaTypeMsg iguana_type_msg; @@ -102,7 +158,14 @@ struct NestedMsg PUBLIC { REFLECTION(NestedMsg, base_msg, repeat_base_msg, iguana_type_msg, repeat_iguna_msg, repeat_repeat_base_msg); -struct MapMsg PUBLIC { +struct MapMsg PUBLIC(MapMsg) { + MapMsg() = default; + MapMsg(std::unordered_map a, + std::unordered_map b, + std::map c) + : sfix64_str_map(std::move(a)), + str_iguana_type_msg_map(std::move(b)), + int_repeat_base_msg_map(std::move(c)) {} std::unordered_map sfix64_str_map{}; std::unordered_map str_iguana_type_msg_map{}; std::map int_repeat_base_msg_map{}; @@ -110,28 +173,41 @@ struct MapMsg PUBLIC { REFLECTION(MapMsg, sfix64_str_map, str_iguana_type_msg_map, int_repeat_base_msg_map); -struct BaseOneofMsg PUBLIC { +struct BaseOneofMsg PUBLIC(BaseOneofMsg) { + BaseOneofMsg() = default; + BaseOneofMsg(int32_t a, std::variant b, + double c) + : optional_int32(a), one_of(std::move(b)), optional_double(c) {} int32_t optional_int32; std::variant one_of; double optional_double; }; REFLECTION(BaseOneofMsg, optional_int32, one_of, optional_double); -struct NestOneofMsg PUBLIC { +struct NestOneofMsg PUBLIC(NestOneofMsg) { + NestOneofMsg() = default; + NestOneofMsg(std::variant a) + : nest_one_of_msg(std::move(a)) {} std::variant nest_one_of_msg; }; REFLECTION(NestOneofMsg, nest_one_of_msg); -struct simple_t PUBLIC { +struct simple_t PUBLIC(simple_t) { + simple_t() = default; + simple_t(int32_t x, int32_t y, int64_t z, int64_t w, std::string s) + : a(x), b(y), c(z), d(w), str(std::move(s)) {} int32_t a; int32_t b; int64_t c; int64_t d; - std::string_view str; + std::string str; }; REFLECTION(simple_t, a, b, c, d, str); -struct simple_t1 PUBLIC { +struct simple_t1 PUBLIC(simple_t1) { + simple_t1() = default; + simple_t1(int32_t x, int32_t y, int64_t z, int64_t w, std::string_view s) + : a(x), b(y), c(z), d(w), str(s) {} int32_t a; int32_t b; int64_t c; @@ -142,7 +218,10 @@ REFLECTION(simple_t1, a, b, c, d, str); enum Color : uint8_t { Red, Green, Blue }; -struct simple_t2 PUBLIC { +struct simple_t2 PUBLIC(simple_t2) { + simple_t2() = default; + simple_t2(int16_t x, uint8_t y, Color z, int64_t w, std::string_view s = "") + : a(x), b(y), c(z), d(w), str(s) {} int16_t a; uint8_t b; Color c; @@ -151,7 +230,10 @@ struct simple_t2 PUBLIC { }; REFLECTION(simple_t2, a, b, c, d, str); -struct person PUBLIC { +struct person PUBLIC(person) { + person() = default; + person(int32_t a, std::string b, int c, double d) + : id(a), name(std::move(b)), age(c), salary(d) {} int32_t id; std::string name; int age; @@ -159,7 +241,10 @@ struct person PUBLIC { }; REFLECTION(person, id, name, age, salary); -struct rect PUBLIC { +struct rect PUBLIC(rect) { + rect() = default; + rect(int32_t a, int32_t b, int32_t c, int32_t d) + : x(a), y(b), width(c), height(d) {} int32_t x = 1; int32_t y = 0; int32_t width = 11; @@ -167,7 +252,9 @@ struct rect PUBLIC { }; REFLECTION(rect, x, y, width, height); -struct Vec3 PUBLIC { +struct Vec3 PUBLIC(Vec3) { + Vec3() = default; + Vec3(float a, float b, float c) : x(a), y(b), z(c) {} float x; float y; float z; @@ -175,13 +262,27 @@ struct Vec3 PUBLIC { REFLECTION(Vec3, x, y, z); }; -struct Weapon PUBLIC { +struct Weapon PUBLIC(Weapon) { + Weapon() = default; + Weapon(std::string a, int32_t b) : name(std::move(a)), damage(b) {} std::string name; int32_t damage; }; REFLECTION(Weapon, name, damage); -struct Monster PUBLIC { +struct Monster PUBLIC(Monster) { + Monster() = default; + Monster(Vec3 a, int32_t b, int32_t c, std::string d, std::string e, int32_t f, + std::vector g, Weapon h, std::vector i) + : pos(a), + mana(b), + hp(c), + name(std::move(d)), + inventory(std::move(e)), + color(f), + weapons(std::move(g)), + equipped(std::move(h)), + path(std::move(i)) {} Vec3 pos; int32_t mana; int32_t hp; @@ -195,7 +296,10 @@ struct Monster PUBLIC { REFLECTION(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path); -struct bench_int32 PUBLIC { +struct bench_int32 PUBLIC(bench_int32) { + bench_int32() = default; + bench_int32(int32_t x, int32_t y, int32_t z, int32_t u) + : a(x), b(y), c(z), d(u) {} int32_t a; int32_t b; int32_t c; @@ -206,22 +310,21 @@ REFLECTION(bench_int32, a, b, c, d); } // namespace stpb inline auto create_person() { - stpb::person p{0, 432798, std::string(1024, 'A'), 24, 65536.42}; + stpb::person p{432798, std::string(1024, 'A'), 24, 65536.42}; return p; } inline stpb::Monster create_sp_monster() { stpb::Monster m = { - 0, - {0, 1, 2, 3}, + {1, 2, 3}, 16, 24, "it is a test", "\1\2\3\4", stpb::Color::Red, - {{0, "gun", 42}, {0, "shotgun", 56}}, - {0, "air craft", 67}, - {{0, 7, 8, 9}, {0, 71, 81, 91}}, + {{"gun", 42}, {"shotgun", 56}}, + {"air craft", 67}, + {{7, 8, 9}, {71, 81, 91}}, }; return m; } diff --git a/website/docs/en/struct_pb/struct_pb_intro.md b/website/docs/en/struct_pb/struct_pb_intro.md index efceb0947..4edd32c68 100644 --- a/website/docs/en/struct_pb/struct_pb_intro.md +++ b/website/docs/en/struct_pb/struct_pb_intro.md @@ -14,14 +14,14 @@ Utilize inline, zero copy, compile-time compute to optimize performance. ```cpp #include -struct my_struct : struct_pb::pb_base { +struct my_struct : struct_pb::pb_base_impl { int x; bool y; struct_pb::fixed64_t z; }; REFLECTION(my_struct, x, y, z); -struct nest : struct_pb::pb_base { +struct nest : struct_pb::pb_base_impl { std::string name; my_struct value; int var; diff --git a/website/docs/zh/struct_pb/struct_pb_intro.md b/website/docs/zh/struct_pb/struct_pb_intro.md index da329098e..fd7bf11da 100644 --- a/website/docs/zh/struct_pb/struct_pb_intro.md +++ b/website/docs/zh/struct_pb/struct_pb_intro.md @@ -11,14 +11,14 @@ struct_pb 是基于C++17 开发的高性能、易用、header only的protobuf格 ```cpp #include -struct my_struct : struct_pb::pb_base { +struct my_struct : struct_pb::pb_base_impl { int x; bool y; struct_pb::fixed64_t z; }; REFLECTION(my_struct, x, y, z); -struct nest : struct_pb::pb_base { +struct nest : struct_pb::pb_base_impl { std::string name; my_struct value; int var;