From 9f0d5fc6dbfda8d28eaa444353eea6640515188f Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Mon, 7 Aug 2023 23:24:39 +0800 Subject: [PATCH 1/6] tmp --- include/ylt/struct_pack/reflection.hpp | 236 ++++++++++-------- .../tests/test_non_aggregated_type.cpp | 8 +- test.cpp | 124 +++++++++ test2.cpp | 116 +++++++++ 4 files changed, 379 insertions(+), 105 deletions(-) create mode 100644 test.cpp create mode 100644 test2.cpp diff --git a/include/ylt/struct_pack/reflection.hpp b/include/ylt/struct_pack/reflection.hpp index 630cf730c..efb9cd557 100644 --- a/include/ylt/struct_pack/reflection.hpp +++ b/include/ylt/struct_pack/reflection.hpp @@ -1998,6 +1998,10 @@ constexpr decltype(auto) STRUCT_PACK_INLINE template_switch(std::size_t index, // index shouldn't bigger than 256 } } // namespace detail + +template +using Idx_t = std::integral_constant; + } // namespace detail } // namespace struct_pack @@ -2025,71 +2029,71 @@ constexpr decltype(auto) STRUCT_PACK_INLINE template_switch(std::size_t index, #define STRUCT_PACK_MARCO_EXPAND(...) __VA_ARGS__ -#define STRUCT_PACK_DOARG0(s,f,o) -#define STRUCT_PACK_DOARG1(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG0(s,f,__VA_ARGS__)) s f(0,t) -#define STRUCT_PACK_DOARG2(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG1(s,f,__VA_ARGS__)) s f(1,t) -#define STRUCT_PACK_DOARG3(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG2(s,f,__VA_ARGS__)) s f(2,t) -#define STRUCT_PACK_DOARG4(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG3(s,f,__VA_ARGS__)) s f(3,t) -#define STRUCT_PACK_DOARG5(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG4(s,f,__VA_ARGS__)) s f(4,t) -#define STRUCT_PACK_DOARG6(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG5(s,f,__VA_ARGS__)) s f(5,t) -#define STRUCT_PACK_DOARG7(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG6(s,f,__VA_ARGS__)) s f(6,t) -#define STRUCT_PACK_DOARG8(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG7(s,f,__VA_ARGS__)) s f(7,t) -#define STRUCT_PACK_DOARG9(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG8(s,f,__VA_ARGS__)) s f(8,t) -#define STRUCT_PACK_DOARG10(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG9(s,f,__VA_ARGS__)) s f(9,t) -#define STRUCT_PACK_DOARG11(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG10(s,f,__VA_ARGS__)) s f(10,t) -#define STRUCT_PACK_DOARG12(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG11(s,f,__VA_ARGS__)) s f(11,t) -#define STRUCT_PACK_DOARG13(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG12(s,f,__VA_ARGS__)) s f(12,t) -#define STRUCT_PACK_DOARG14(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG13(s,f,__VA_ARGS__)) s f(13,t) -#define STRUCT_PACK_DOARG15(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG14(s,f,__VA_ARGS__)) s f(14,t) -#define STRUCT_PACK_DOARG16(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG15(s,f,__VA_ARGS__)) s f(15,t) -#define STRUCT_PACK_DOARG17(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG16(s,f,__VA_ARGS__)) s f(16,t) -#define STRUCT_PACK_DOARG18(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG17(s,f,__VA_ARGS__)) s f(17,t) -#define STRUCT_PACK_DOARG19(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG18(s,f,__VA_ARGS__)) s f(18,t) -#define STRUCT_PACK_DOARG20(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG19(s,f,__VA_ARGS__)) s f(19,t) -#define STRUCT_PACK_DOARG21(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG20(s,f,__VA_ARGS__)) s f(20,t) -#define STRUCT_PACK_DOARG22(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG21(s,f,__VA_ARGS__)) s f(21,t) -#define STRUCT_PACK_DOARG23(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG22(s,f,__VA_ARGS__)) s f(22,t) -#define STRUCT_PACK_DOARG24(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG23(s,f,__VA_ARGS__)) s f(23,t) -#define STRUCT_PACK_DOARG25(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG24(s,f,__VA_ARGS__)) s f(24,t) -#define STRUCT_PACK_DOARG26(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG25(s,f,__VA_ARGS__)) s f(25,t) -#define STRUCT_PACK_DOARG27(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG26(s,f,__VA_ARGS__)) s f(26,t) -#define STRUCT_PACK_DOARG28(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG27(s,f,__VA_ARGS__)) s f(27,t) -#define STRUCT_PACK_DOARG29(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG28(s,f,__VA_ARGS__)) s f(28,t) -#define STRUCT_PACK_DOARG30(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG29(s,f,__VA_ARGS__)) s f(29,t) -#define STRUCT_PACK_DOARG31(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG30(s,f,__VA_ARGS__)) s f(30,t) -#define STRUCT_PACK_DOARG32(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG31(s,f,__VA_ARGS__)) s f(31,t) -#define STRUCT_PACK_DOARG33(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG32(s,f,__VA_ARGS__)) s f(32,t) -#define STRUCT_PACK_DOARG34(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG33(s,f,__VA_ARGS__)) s f(33,t) -#define STRUCT_PACK_DOARG35(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG34(s,f,__VA_ARGS__)) s f(34,t) -#define STRUCT_PACK_DOARG36(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG35(s,f,__VA_ARGS__)) s f(35,t) -#define STRUCT_PACK_DOARG37(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG36(s,f,__VA_ARGS__)) s f(36,t) -#define STRUCT_PACK_DOARG38(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG37(s,f,__VA_ARGS__)) s f(37,t) -#define STRUCT_PACK_DOARG39(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG38(s,f,__VA_ARGS__)) s f(38,t) -#define STRUCT_PACK_DOARG40(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG39(s,f,__VA_ARGS__)) s f(39,t) -#define STRUCT_PACK_DOARG41(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG40(s,f,__VA_ARGS__)) s f(40,t) -#define STRUCT_PACK_DOARG42(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG41(s,f,__VA_ARGS__)) s f(41,t) -#define STRUCT_PACK_DOARG43(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG42(s,f,__VA_ARGS__)) s f(42,t) -#define STRUCT_PACK_DOARG44(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG43(s,f,__VA_ARGS__)) s f(43,t) -#define STRUCT_PACK_DOARG45(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG44(s,f,__VA_ARGS__)) s f(44,t) -#define STRUCT_PACK_DOARG46(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG45(s,f,__VA_ARGS__)) s f(45,t) -#define STRUCT_PACK_DOARG47(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG46(s,f,__VA_ARGS__)) s f(46,t) -#define STRUCT_PACK_DOARG48(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG47(s,f,__VA_ARGS__)) s f(47,t) -#define STRUCT_PACK_DOARG49(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG48(s,f,__VA_ARGS__)) s f(48,t) -#define STRUCT_PACK_DOARG50(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG49(s,f,__VA_ARGS__)) s f(49,t) -#define STRUCT_PACK_DOARG51(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG50(s,f,__VA_ARGS__)) s f(50,t) -#define STRUCT_PACK_DOARG52(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG51(s,f,__VA_ARGS__)) s f(51,t) -#define STRUCT_PACK_DOARG53(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG52(s,f,__VA_ARGS__)) s f(52,t) -#define STRUCT_PACK_DOARG54(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG53(s,f,__VA_ARGS__)) s f(53,t) -#define STRUCT_PACK_DOARG55(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG54(s,f,__VA_ARGS__)) s f(54,t) -#define STRUCT_PACK_DOARG56(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG55(s,f,__VA_ARGS__)) s f(55,t) -#define STRUCT_PACK_DOARG57(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG56(s,f,__VA_ARGS__)) s f(56,t) -#define STRUCT_PACK_DOARG58(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG57(s,f,__VA_ARGS__)) s f(57,t) -#define STRUCT_PACK_DOARG59(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG58(s,f,__VA_ARGS__)) s f(58,t) -#define STRUCT_PACK_DOARG60(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG59(s,f,__VA_ARGS__)) s f(59,t) -#define STRUCT_PACK_DOARG61(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG60(s,f,__VA_ARGS__)) s f(60,t) -#define STRUCT_PACK_DOARG62(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG61(s,f,__VA_ARGS__)) s f(61,t) -#define STRUCT_PACK_DOARG63(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG62(s,f,__VA_ARGS__)) s f(62,t) -#define STRUCT_PACK_DOARG64(s,f,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG63(s,f,__VA_ARGS__)) s f(63,t) +#define STRUCT_PACK_DOARG0(s,f,p,o) +#define STRUCT_PACK_DOARG1(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG0(s,f,p,__VA_ARGS__)) s f(0,t,p) +#define STRUCT_PACK_DOARG2(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG1(s,f,p,__VA_ARGS__)) s f(1,t,p) +#define STRUCT_PACK_DOARG3(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG2(s,f,p,__VA_ARGS__)) s f(2,t,p) +#define STRUCT_PACK_DOARG4(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG3(s,f,p,__VA_ARGS__)) s f(3,t,p) +#define STRUCT_PACK_DOARG5(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG4(s,f,p,__VA_ARGS__)) s f(4,t,p) +#define STRUCT_PACK_DOARG6(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG5(s,f,p,__VA_ARGS__)) s f(5,t,p) +#define STRUCT_PACK_DOARG7(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG6(s,f,p,__VA_ARGS__)) s f(6,t,p) +#define STRUCT_PACK_DOARG8(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG7(s,f,p,__VA_ARGS__)) s f(7,t,p) +#define STRUCT_PACK_DOARG9(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG8(s,f,p,__VA_ARGS__)) s f(8,t,p) +#define STRUCT_PACK_DOARG10(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG9(s,f,p,__VA_ARGS__)) s f(9,t,p) +#define STRUCT_PACK_DOARG11(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG10(s,f,p,__VA_ARGS__)) s f(10,t,p) +#define STRUCT_PACK_DOARG12(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG11(s,f,p,__VA_ARGS__)) s f(11,t,p) +#define STRUCT_PACK_DOARG13(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG12(s,f,p,__VA_ARGS__)) s f(12,t,p) +#define STRUCT_PACK_DOARG14(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG13(s,f,p,__VA_ARGS__)) s f(13,t,p) +#define STRUCT_PACK_DOARG15(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG14(s,f,p,__VA_ARGS__)) s f(14,t,p) +#define STRUCT_PACK_DOARG16(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG15(s,f,p,__VA_ARGS__)) s f(15,t,p) +#define STRUCT_PACK_DOARG17(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG16(s,f,p,__VA_ARGS__)) s f(16,t,p) +#define STRUCT_PACK_DOARG18(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG17(s,f,p,__VA_ARGS__)) s f(17,t,p) +#define STRUCT_PACK_DOARG19(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG18(s,f,p,__VA_ARGS__)) s f(18,t,p) +#define STRUCT_PACK_DOARG20(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG19(s,f,p,__VA_ARGS__)) s f(19,t,p) +#define STRUCT_PACK_DOARG21(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG20(s,f,p,__VA_ARGS__)) s f(20,t,p) +#define STRUCT_PACK_DOARG22(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG21(s,f,p,__VA_ARGS__)) s f(21,t,p) +#define STRUCT_PACK_DOARG23(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG22(s,f,p,__VA_ARGS__)) s f(22,t,p) +#define STRUCT_PACK_DOARG24(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG23(s,f,p,__VA_ARGS__)) s f(23,t,p) +#define STRUCT_PACK_DOARG25(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG24(s,f,p,__VA_ARGS__)) s f(24,t,p) +#define STRUCT_PACK_DOARG26(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG25(s,f,p,__VA_ARGS__)) s f(25,t,p) +#define STRUCT_PACK_DOARG27(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG26(s,f,p,__VA_ARGS__)) s f(26,t,p) +#define STRUCT_PACK_DOARG28(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG27(s,f,p,__VA_ARGS__)) s f(27,t,p) +#define STRUCT_PACK_DOARG29(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG28(s,f,p,__VA_ARGS__)) s f(28,t,p) +#define STRUCT_PACK_DOARG30(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG29(s,f,p,__VA_ARGS__)) s f(29,t,p) +#define STRUCT_PACK_DOARG31(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG30(s,f,p,__VA_ARGS__)) s f(30,t,p) +#define STRUCT_PACK_DOARG32(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG31(s,f,p,__VA_ARGS__)) s f(31,t,p) +#define STRUCT_PACK_DOARG33(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG32(s,f,p,__VA_ARGS__)) s f(32,t,p) +#define STRUCT_PACK_DOARG34(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG33(s,f,p,__VA_ARGS__)) s f(33,t,p) +#define STRUCT_PACK_DOARG35(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG34(s,f,p,__VA_ARGS__)) s f(34,t,p) +#define STRUCT_PACK_DOARG36(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG35(s,f,p,__VA_ARGS__)) s f(35,t,p) +#define STRUCT_PACK_DOARG37(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG36(s,f,p,__VA_ARGS__)) s f(36,t,p) +#define STRUCT_PACK_DOARG38(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG37(s,f,p,__VA_ARGS__)) s f(37,t,p) +#define STRUCT_PACK_DOARG39(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG38(s,f,p,__VA_ARGS__)) s f(38,t,p) +#define STRUCT_PACK_DOARG40(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG39(s,f,p,__VA_ARGS__)) s f(39,t,p) +#define STRUCT_PACK_DOARG41(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG40(s,f,p,__VA_ARGS__)) s f(40,t,p) +#define STRUCT_PACK_DOARG42(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG41(s,f,p,__VA_ARGS__)) s f(41,t,p) +#define STRUCT_PACK_DOARG43(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG42(s,f,p,__VA_ARGS__)) s f(42,t,p) +#define STRUCT_PACK_DOARG44(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG43(s,f,p,__VA_ARGS__)) s f(43,t,p) +#define STRUCT_PACK_DOARG45(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG44(s,f,p,__VA_ARGS__)) s f(44,t,p) +#define STRUCT_PACK_DOARG46(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG45(s,f,p,__VA_ARGS__)) s f(45,t,p) +#define STRUCT_PACK_DOARG47(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG46(s,f,p,__VA_ARGS__)) s f(46,t,p) +#define STRUCT_PACK_DOARG48(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG47(s,f,p,__VA_ARGS__)) s f(47,t,p) +#define STRUCT_PACK_DOARG49(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG48(s,f,p,__VA_ARGS__)) s f(48,t,p) +#define STRUCT_PACK_DOARG50(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG49(s,f,p,__VA_ARGS__)) s f(49,t,p) +#define STRUCT_PACK_DOARG51(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG50(s,f,p,__VA_ARGS__)) s f(50,t,p) +#define STRUCT_PACK_DOARG52(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG51(s,f,p,__VA_ARGS__)) s f(51,t,p) +#define STRUCT_PACK_DOARG53(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG52(s,f,p,__VA_ARGS__)) s f(52,t,p) +#define STRUCT_PACK_DOARG54(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG53(s,f,p,__VA_ARGS__)) s f(53,t,p) +#define STRUCT_PACK_DOARG55(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG54(s,f,p,__VA_ARGS__)) s f(54,t,p) +#define STRUCT_PACK_DOARG56(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG55(s,f,p,__VA_ARGS__)) s f(55,t,p) +#define STRUCT_PACK_DOARG57(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG56(s,f,p,__VA_ARGS__)) s f(56,t,p) +#define STRUCT_PACK_DOARG58(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG57(s,f,p,__VA_ARGS__)) s f(57,t,p) +#define STRUCT_PACK_DOARG59(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG58(s,f,p,__VA_ARGS__)) s f(58,t,p) +#define STRUCT_PACK_DOARG60(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG59(s,f,p,__VA_ARGS__)) s f(59,t,p) +#define STRUCT_PACK_DOARG61(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG60(s,f,p,__VA_ARGS__)) s f(60,t,p) +#define STRUCT_PACK_DOARG62(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG61(s,f,p,__VA_ARGS__)) s f(61,t,p) +#define STRUCT_PACK_DOARG63(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG62(s,f,p,__VA_ARGS__)) s f(62,t,p) +#define STRUCT_PACK_DOARG64(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG63(s,f,p,__VA_ARGS__)) s f(63,t,p) #define STRUCT_PACK_MAKE_ARGS0(Type) #define STRUCT_PACK_MAKE_ARGS1(Type) Type @@ -2157,56 +2161,88 @@ constexpr decltype(auto) STRUCT_PACK_INLINE template_switch(std::size_t index, #define STRUCT_PACK_MAKE_ARGS63(Type) STRUCT_PACK_MAKE_ARGS62(Type), Type #define STRUCT_PACK_MAKE_ARGS64(Type) STRUCT_PACK_MAKE_ARGS63(Type), Type +#define STRUCT_PACK_STEALER_DECL(Type)\ +template \ +struct STRUCT_PACK_##Type##_IS_PRIVATE_FIELD : std::true_type {};\ +template \ +struct STRUCT_PACK_##Type##_IS_PRIVATE_FUNCTION : std::true_type {};\ +template \ +constexpr bool STRUCT_PACK_##Type##_IS_PRIVATE = STRUCT_PACK_##Type##_IS_PRIVATE_FIELD::value&&\ + STRUCT_PACK_##Type##_IS_PRIVATE_FUNCTION::value;\ +template \ +struct STRUCT_PACK_STEALER;\ +template \ +struct STRUCT_PACK_STEALER {\ + friend auto& STRUCT_PACK_STEAL(Type& t, struct_pack::detail::Idx_t) {\ + if constexpr (STRUCT_PACK_##Type##_IS_PRIVATE) {\ + if constexpr (std::is_member_function_pointer_v) {\ + return (t.*field)();\ + }\ + else if constexpr (std::is_member_pointer_v) {\ + return t.*field;\ + }\ + else {\ + static_assert(sizeof(Type), "The parameters for STRUCT_PACK_REFL is error");\ + }\ + }\ + else {\ + static int useless;\ + return useless;\ + }\ + }\ + friend const auto& STRUCT_PACK_STEAL(const Type& t, struct_pack::detail::Idx_t) {\ + if constexpr (STRUCT_PACK_##Type##_IS_PRIVATE) {\ + if constexpr (std::is_member_function_pointer_v) {\ + return (t.*field)();\ + }\ + else if constexpr (std::is_member_pointer_v) {\ + return t.*field;\ + }\ + else {\ + static_assert(sizeof(Type), "The parameters for STRUCT_PACK_REFL is error");\ + }\ + }\ + else {\ + static int useless;\ + return useless;\ + }\ + }\ +};\ + +#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X , Bank) \ +auto& STRUCT_PACK_STEAL(Bank& bank, struct_pack::detail::Idx_t);\ +const auto& STRUCT_PACK_STEAL(const Bank& bank, struct_pack::detail::Idx_t);\ +template \ +struct STRUCT_PACK_##Bank##_IS_PRIVATE_FUNCTION >\ + : std::false_type {};\ +template \ +struct STRUCT_PACK_##Bank##_IS_PRIVATE_FIELD >\ + : std::false_type {};\ +template struct STRUCT_PACK_STEALER;\ #define STRUCT_PACK_MAKE_ARGS(Type,Count) \ STRUCT_PACK_CONCAT(STRUCT_PACK_MAKE_ARGS,Count)(Type) -#define STRUCT_PACK_EXPAND_EACH_(sepatator,fun,...) \ - STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_CONCAT(STRUCT_PACK_DOARG,STRUCT_PACK_ARG_COUNT(__VA_ARGS__))(sepatator,fun,__VA_ARGS__)) -#define STRUCT_PACK_EXPAND_EACH(sepatator,fun,...) \ - STRUCT_PACK_EXPAND_EACH_(sepatator,fun,__VA_ARGS__) +#define STRUCT_PACK_EXPAND_EACH_(sepatator,fun,parent,...) \ + STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_CONCAT(STRUCT_PACK_DOARG,STRUCT_PACK_ARG_COUNT(__VA_ARGS__))(sepatator,fun,parent,__VA_ARGS__)) +#define STRUCT_PACK_EXPAND_EACH(sepatator,fun,parent,...) \ + STRUCT_PACK_EXPAND_EACH_(sepatator,fun,parent,__VA_ARGS__) -#define STRUCT_PACK_RETURN_ELEMENT(Idx, X) \ +#define STRUCT_PACK_RETURN_ELEMENT(Idx, X , parent) \ if constexpr (Idx == I) {\ return c.X;\ }\ -#define STRUCT_PACK_GET_INDEX(Idx, Type) \ +#define STRUCT_PACK_GET_INDEX(Idx, Type, parent) \ auto& STRUCT_PACK_GET_##Idx(Type& c) {\ return STRUCT_PACK_GET()-1-Idx>(c);\ }\ -#define STRUCT_PACK_GET_INDEX_CONST(Idx, Type) \ +#define STRUCT_PACK_GET_INDEX_CONST(Idx, Type, parent) \ const auto& STRUCT_PACK_GET_##Idx(const Type& c) {\ return STRUCT_PACK_GET()-1-Idx>(c);\ }\ #define STRUCT_PACK_REFL(Type,...) \ -Type& STRUCT_PACK_REFL_FLAG(Type& t) {return t;} \ -template \ -constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL(); \ -template<> \ -constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL() {return STRUCT_PACK_ARG_COUNT(__VA_ARGS__);} \ -decltype(auto) STRUCT_PACK_FIELD_COUNT(const Type &){ \ - return std::integral_constant{}; \ -} \ -template auto& STRUCT_PACK_GET(Type& c) { \ - STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_RETURN_ELEMENT,__VA_ARGS__) \ - else { \ - static_assert(I < STRUCT_PACK_FIELD_COUNT_IMPL()); \ - } \ -} \ -template const auto& STRUCT_PACK_GET(const Type& c) { \ - STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_RETURN_ELEMENT,__VA_ARGS__) \ - else { \ - static_assert(I < STRUCT_PACK_FIELD_COUNT_IMPL()); \ - } \ -} \ -STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_GET_INDEX,STRUCT_PACK_MAKE_ARGS(Type,STRUCT_PACK_ARG_COUNT(__VA_ARGS__))) \ -STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_GET_INDEX_CONST,STRUCT_PACK_MAKE_ARGS(Type,STRUCT_PACK_ARG_COUNT(__VA_ARGS__))) \ - -#define STRUCT_PACK_FRIEND_DECL(Type) \ -template \ -friend auto& STRUCT_PACK_GET(Type& c); \ -template \ -friend const auto& STRUCT_PACK_GET(const Type& c); +STRUCT_PACK_STEALER_DECL(Type) \ +STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_VISIT_PRIVATE_HELPER,Type,__VA_ARGS__) \ No newline at end of file diff --git a/src/struct_pack/tests/test_non_aggregated_type.cpp b/src/struct_pack/tests/test_non_aggregated_type.cpp index 457aabca4..9a8a07b06 100644 --- a/src/struct_pack/tests/test_non_aggregated_type.cpp +++ b/src/struct_pack/tests/test_non_aggregated_type.cpp @@ -55,7 +55,6 @@ class person { } person() = default; person(int age, const std::string& name) : age(age), name(name) {} - STRUCT_PACK_FRIEND_DECL(person); }; STRUCT_PACK_REFL(person, age, name); } // namespace example2 @@ -77,11 +76,11 @@ class person { person(int age, const std::string& name) : age_(age), name_(name) {} int& age() { return age_; }; - const int& age() const { return age_; }; + // const int& age() const { return age_; }; std::string& name() { return name_; }; - const std::string& name() const { return name_; }; + // const std::string& name() const { return name_; }; }; -STRUCT_PACK_REFL(person, age(), name()); +STRUCT_PACK_REFL(person, age, name); } // namespace example3 // 5. Remember, the STURCT_PACK_REFL marco disable the trivial_serialize @@ -162,7 +161,6 @@ class complicated_object { j == o.j && k == o.k && m == o.m && n[0] == o.n[0] && n[1] == o.n[1] && this->o == o.o; } - STRUCT_PACK_FRIEND_DECL(complicated_object); }; STRUCT_PACK_REFL(complicated_object, a, b, c, d, e, f, g, h, i, j, k, m, n, o); } // namespace example6 diff --git a/test.cpp b/test.cpp new file mode 100644 index 000000000..be2ef4314 --- /dev/null +++ b/test.cpp @@ -0,0 +1,124 @@ +#include +#include + +template +using Idx_t = std::integral_constant; + +template +struct is_private_member_field : std::true_type {}; + +template +struct is_private_member_function : std::true_type {}; + +template +constexpr bool is_private_member = is_private_member_field::value&& + is_private_member_function::value; + +template +struct Thief2; + +template +struct Thief2 { + friend const auto& steal_impl(T& t, Idx_t) { + return (t.*field)(); + } +}; + +template +struct Thief2 { + friend auto& steal_impl(T& t, Idx_t) { + return (t.*field)();\ + } +}; + +template +struct ThiefMemberFunction { + friend auto& steal_impl(T& t, Idx_t) { + if constexpr (std::is_same_v) {\ + static int useless; + return useless; + }\ + else {\ + return (t.*field)();\ + }\ + } +}; + +template +struct ThiefMemberFunctionConst { + friend const auto& steal_impl(const T& t, Idx_t) { + if constexpr (std::is_same_v) {\ + static int useless; + return useless; + }\ + else {\ + return (t.*field)();\ + }\ + } +}; + + +class Bank2 { + float money = 999'999'999'999; + std::string name_; + bool sex_; + bool& sex() {return sex_;} + const bool& sex() const {return sex_;} + public: + std::string& name() { return name_; } + const std::string& name() const { return name_; } + int age; +}; + +template +constexpr inline std::nullptr_t member_function_test(U X) { + return nullptr; +}; + +template +constexpr inline auto member_function_test(U (T::* X)()) { + return X; +}; + +template +constexpr inline auto const_member_function_test(U X) { + return nullptr; +}; + +template +constexpr inline auto const_member_function_test(U (T::* X)() const ) { + return X; +}; + + + +#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X , Bank) \ +auto& steal_impl(Bank& bank, Idx_t);\ +const auto& steal_impl(const Bank& bank, Idx_t);\ +template \ +struct is_private_member_function >\ + : std::false_type {};\ +template \ +struct is_private_member_field >\ + : std::false_type {};\ +template struct ThiefMemberFunction(&Bank::X), Idx>;\ +template struct ThiefMemberFunctionConst(&Bank::X), Idx>;\ + + + + +//STRUCT_PACK_VISIT_PRIVATE_HELPER(0,money,Bank2) +STRUCT_PACK_VISIT_PRIVATE_HELPER(1,name,Bank2) +//STRUCT_PACK_VISIT_PRIVATE_HELPER(2,age,Bank2) +STRUCT_PACK_VISIT_PRIVATE_HELPER(3,sex,Bank2) + +void test(Bank2& bank) { + // steal_impl(bank,Idx_t<0>{}) = 100.04; + //static_assert(is_private_member); + steal_impl(bank,Idx_t<1>{}) = "hi"; + static_assert(!is_private_member); + //static_assert(!is_private_member); + steal_impl(bank,Idx_t<3>{}) = true; + static_assert(is_private_member); +} + diff --git a/test2.cpp b/test2.cpp new file mode 100644 index 000000000..65e15e896 --- /dev/null +++ b/test2.cpp @@ -0,0 +1,116 @@ +#include +#include + +template +using Idx_t = std::integral_constant; + +template +struct is_private_member_field : std::true_type {}; + +template +struct is_private_member_function : std::true_type {}; + +template +constexpr bool is_private_member = is_private_member_field::value&& + is_private_member_function::value; + +template +struct Thief2; + +template +struct Thief2 { + friend const auto& steal_impl(T& t, Idx_t) { return (t.*field)(); } +}; + +template +struct Thief2 { + friend auto& steal_impl(T& t, Idx_t) { return (t.*field)(); } +}; + +template +struct ThiefMemberFunction { + friend auto& steal_impl(T& t, Idx_t) { + if constexpr (std::is_same_v) { + static int useless; + return useless; + } + else { + return (t.*field)(); + } + } +}; + +template +struct ThiefMemberFunctionConst { + friend const auto& steal_impl(const T& t, Idx_t) { + if constexpr (std::is_same_v) { + static int useless; + return useless; + } + else { + return (t.*field)(); + } + } +}; + +class Bank2 { + float money = 999'999'999'999; + std::string name_; + bool sex_; + bool& sex() { return sex_; } + const bool& sex() const { return sex_; } + + public: + std::string& name() { return name_; } + const std::string& name() const { return name_; } + int age; +}; + +template +constexpr inline std::nullptr_t member_function_test(U X) { + return nullptr; +}; + +template +constexpr inline auto member_function_test(U (T::*X)()) { + return X; +}; + +template +constexpr inline auto const_member_function_test(U X) { + return nullptr; +}; + +template +constexpr inline auto const_member_function_test(U (T::*X)() const) { + return X; +}; + +#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X, Bank) \ + auto& steal_impl(Bank& bank, Idx_t); \ + const auto& steal_impl(const Bank& bank, Idx_t); \ + template \ + struct is_private_member_function > \ + : std::false_type {}; \ + template \ + struct is_private_member_field > \ + : std::false_type {}; \ + template struct ThiefMemberFunction< \ + Bank, member_function_test(&Bank::X), Idx>; \ + template struct ThiefMemberFunctionConst< \ + Bank, const_member_function_test(&Bank::X), Idx>; + +// STRUCT_PACK_VISIT_PRIVATE_HELPER(0,money,Bank2) +STRUCT_PACK_VISIT_PRIVATE_HELPER(1, name, Bank2) +// STRUCT_PACK_VISIT_PRIVATE_HELPER(2,age,Bank2) +STRUCT_PACK_VISIT_PRIVATE_HELPER(3, sex, Bank2) + +void test(Bank2& bank) { + // steal_impl(bank,Idx_t<0>{}) = 100.04; + // static_assert(is_private_member); + steal_impl(bank, Idx_t<1>{}) = "hi"; + static_assert(!is_private_member); + // static_assert(!is_private_member); + steal_impl(bank, Idx_t<3>{}) = true; + static_assert(is_private_member); +} From 9ed042fbde56efd9d7d5bf8f0dfbdd4115ead0c0 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 8 Aug 2023 12:02:58 +0800 Subject: [PATCH 2/6] fix --- include/ylt/struct_pack/reflection.hpp | 163 ++++++------------ .../examples/non_aggregated_type.cpp | 74 ++++---- .../tests/test_non_aggregated_type.cpp | 7 +- test.cpp | 162 +++++++---------- .../docs/en/struct_pack/struct_pack_intro.md | 29 +--- .../docs/zh/struct_pack/struct_pack_intro.md | 22 --- 6 files changed, 163 insertions(+), 294 deletions(-) diff --git a/include/ylt/struct_pack/reflection.hpp b/include/ylt/struct_pack/reflection.hpp index efb9cd557..96ff41ead 100644 --- a/include/ylt/struct_pack/reflection.hpp +++ b/include/ylt/struct_pack/reflection.hpp @@ -2002,6 +2002,23 @@ constexpr decltype(auto) STRUCT_PACK_INLINE template_switch(std::size_t index, template using Idx_t = std::integral_constant; +template +constexpr inline decltype(auto) member_function_resolve(U X, Idx_t) { + return X; +}; +template +constexpr inline decltype(auto) member_function_resolve(U (T::*X)(), Idx_t) { + return X; +}; +template +constexpr inline decltype(auto) const_member_function_resolve(U X, Idx_t) { + return X; +}; +template +constexpr inline decltype(auto) const_member_function_resolve(U (T::*X)() const, + Idx_t) { + return X; +}; } // namespace detail } // namespace struct_pack @@ -2095,130 +2112,42 @@ using Idx_t = std::integral_constant; #define STRUCT_PACK_DOARG63(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG62(s,f,p,__VA_ARGS__)) s f(62,t,p) #define STRUCT_PACK_DOARG64(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG63(s,f,p,__VA_ARGS__)) s f(63,t,p) -#define STRUCT_PACK_MAKE_ARGS0(Type) -#define STRUCT_PACK_MAKE_ARGS1(Type) Type -#define STRUCT_PACK_MAKE_ARGS2(Type) STRUCT_PACK_MAKE_ARGS1(Type), Type -#define STRUCT_PACK_MAKE_ARGS3(Type) STRUCT_PACK_MAKE_ARGS2(Type), Type -#define STRUCT_PACK_MAKE_ARGS4(Type) STRUCT_PACK_MAKE_ARGS3(Type), Type -#define STRUCT_PACK_MAKE_ARGS5(Type) STRUCT_PACK_MAKE_ARGS4(Type), Type -#define STRUCT_PACK_MAKE_ARGS6(Type) STRUCT_PACK_MAKE_ARGS5(Type), Type -#define STRUCT_PACK_MAKE_ARGS7(Type) STRUCT_PACK_MAKE_ARGS6(Type), Type -#define STRUCT_PACK_MAKE_ARGS8(Type) STRUCT_PACK_MAKE_ARGS7(Type), Type -#define STRUCT_PACK_MAKE_ARGS9(Type) STRUCT_PACK_MAKE_ARGS8(Type), Type -#define STRUCT_PACK_MAKE_ARGS10(Type) STRUCT_PACK_MAKE_ARGS9(Type), Type -#define STRUCT_PACK_MAKE_ARGS11(Type) STRUCT_PACK_MAKE_ARGS10(Type), Type -#define STRUCT_PACK_MAKE_ARGS12(Type) STRUCT_PACK_MAKE_ARGS11(Type), Type -#define STRUCT_PACK_MAKE_ARGS13(Type) STRUCT_PACK_MAKE_ARGS12(Type), Type -#define STRUCT_PACK_MAKE_ARGS14(Type) STRUCT_PACK_MAKE_ARGS13(Type), Type -#define STRUCT_PACK_MAKE_ARGS15(Type) STRUCT_PACK_MAKE_ARGS14(Type), Type -#define STRUCT_PACK_MAKE_ARGS16(Type) STRUCT_PACK_MAKE_ARGS15(Type), Type -#define STRUCT_PACK_MAKE_ARGS17(Type) STRUCT_PACK_MAKE_ARGS16(Type), Type -#define STRUCT_PACK_MAKE_ARGS18(Type) STRUCT_PACK_MAKE_ARGS17(Type), Type -#define STRUCT_PACK_MAKE_ARGS19(Type) STRUCT_PACK_MAKE_ARGS18(Type), Type -#define STRUCT_PACK_MAKE_ARGS20(Type) STRUCT_PACK_MAKE_ARGS19(Type), Type -#define STRUCT_PACK_MAKE_ARGS21(Type) STRUCT_PACK_MAKE_ARGS20(Type), Type -#define STRUCT_PACK_MAKE_ARGS22(Type) STRUCT_PACK_MAKE_ARGS21(Type), Type -#define STRUCT_PACK_MAKE_ARGS23(Type) STRUCT_PACK_MAKE_ARGS22(Type), Type -#define STRUCT_PACK_MAKE_ARGS24(Type) STRUCT_PACK_MAKE_ARGS23(Type), Type -#define STRUCT_PACK_MAKE_ARGS25(Type) STRUCT_PACK_MAKE_ARGS24(Type), Type -#define STRUCT_PACK_MAKE_ARGS26(Type) STRUCT_PACK_MAKE_ARGS25(Type), Type -#define STRUCT_PACK_MAKE_ARGS27(Type) STRUCT_PACK_MAKE_ARGS26(Type), Type -#define STRUCT_PACK_MAKE_ARGS28(Type) STRUCT_PACK_MAKE_ARGS27(Type), Type -#define STRUCT_PACK_MAKE_ARGS29(Type) STRUCT_PACK_MAKE_ARGS28(Type), Type -#define STRUCT_PACK_MAKE_ARGS30(Type) STRUCT_PACK_MAKE_ARGS29(Type), Type -#define STRUCT_PACK_MAKE_ARGS31(Type) STRUCT_PACK_MAKE_ARGS30(Type), Type -#define STRUCT_PACK_MAKE_ARGS32(Type) STRUCT_PACK_MAKE_ARGS31(Type), Type -#define STRUCT_PACK_MAKE_ARGS33(Type) STRUCT_PACK_MAKE_ARGS32(Type), Type -#define STRUCT_PACK_MAKE_ARGS34(Type) STRUCT_PACK_MAKE_ARGS33(Type), Type -#define STRUCT_PACK_MAKE_ARGS35(Type) STRUCT_PACK_MAKE_ARGS34(Type), Type -#define STRUCT_PACK_MAKE_ARGS36(Type) STRUCT_PACK_MAKE_ARGS35(Type), Type -#define STRUCT_PACK_MAKE_ARGS37(Type) STRUCT_PACK_MAKE_ARGS36(Type), Type -#define STRUCT_PACK_MAKE_ARGS38(Type) STRUCT_PACK_MAKE_ARGS37(Type), Type -#define STRUCT_PACK_MAKE_ARGS39(Type) STRUCT_PACK_MAKE_ARGS38(Type), Type -#define STRUCT_PACK_MAKE_ARGS40(Type) STRUCT_PACK_MAKE_ARGS39(Type), Type -#define STRUCT_PACK_MAKE_ARGS41(Type) STRUCT_PACK_MAKE_ARGS40(Type), Type -#define STRUCT_PACK_MAKE_ARGS42(Type) STRUCT_PACK_MAKE_ARGS41(Type), Type -#define STRUCT_PACK_MAKE_ARGS43(Type) STRUCT_PACK_MAKE_ARGS42(Type), Type -#define STRUCT_PACK_MAKE_ARGS44(Type) STRUCT_PACK_MAKE_ARGS43(Type), Type -#define STRUCT_PACK_MAKE_ARGS45(Type) STRUCT_PACK_MAKE_ARGS44(Type), Type -#define STRUCT_PACK_MAKE_ARGS46(Type) STRUCT_PACK_MAKE_ARGS45(Type), Type -#define STRUCT_PACK_MAKE_ARGS47(Type) STRUCT_PACK_MAKE_ARGS46(Type), Type -#define STRUCT_PACK_MAKE_ARGS48(Type) STRUCT_PACK_MAKE_ARGS47(Type), Type -#define STRUCT_PACK_MAKE_ARGS49(Type) STRUCT_PACK_MAKE_ARGS48(Type), Type -#define STRUCT_PACK_MAKE_ARGS50(Type) STRUCT_PACK_MAKE_ARGS49(Type), Type -#define STRUCT_PACK_MAKE_ARGS51(Type) STRUCT_PACK_MAKE_ARGS50(Type), Type -#define STRUCT_PACK_MAKE_ARGS52(Type) STRUCT_PACK_MAKE_ARGS51(Type), Type -#define STRUCT_PACK_MAKE_ARGS53(Type) STRUCT_PACK_MAKE_ARGS52(Type), Type -#define STRUCT_PACK_MAKE_ARGS54(Type) STRUCT_PACK_MAKE_ARGS53(Type), Type -#define STRUCT_PACK_MAKE_ARGS55(Type) STRUCT_PACK_MAKE_ARGS54(Type), Type -#define STRUCT_PACK_MAKE_ARGS56(Type) STRUCT_PACK_MAKE_ARGS55(Type), Type -#define STRUCT_PACK_MAKE_ARGS57(Type) STRUCT_PACK_MAKE_ARGS56(Type), Type -#define STRUCT_PACK_MAKE_ARGS58(Type) STRUCT_PACK_MAKE_ARGS57(Type), Type -#define STRUCT_PACK_MAKE_ARGS59(Type) STRUCT_PACK_MAKE_ARGS58(Type), Type -#define STRUCT_PACK_MAKE_ARGS60(Type) STRUCT_PACK_MAKE_ARGS59(Type), Type -#define STRUCT_PACK_MAKE_ARGS61(Type) STRUCT_PACK_MAKE_ARGS60(Type), Type -#define STRUCT_PACK_MAKE_ARGS62(Type) STRUCT_PACK_MAKE_ARGS61(Type), Type -#define STRUCT_PACK_MAKE_ARGS63(Type) STRUCT_PACK_MAKE_ARGS62(Type), Type -#define STRUCT_PACK_MAKE_ARGS64(Type) STRUCT_PACK_MAKE_ARGS63(Type), Type #define STRUCT_PACK_STEALER_DECL(Type)\ -template \ -struct STRUCT_PACK_##Type##_IS_PRIVATE_FIELD : std::true_type {};\ -template \ -struct STRUCT_PACK_##Type##_IS_PRIVATE_FUNCTION : std::true_type {};\ -template \ -constexpr bool STRUCT_PACK_##Type##_IS_PRIVATE = STRUCT_PACK_##Type##_IS_PRIVATE_FIELD::value&&\ - STRUCT_PACK_##Type##_IS_PRIVATE_FUNCTION::value;\ template \ struct STRUCT_PACK_STEALER;\ +template \ +struct STRUCT_PACK_STEALER_CONST;\ template \ struct STRUCT_PACK_STEALER {\ friend auto& STRUCT_PACK_STEAL(Type& t, struct_pack::detail::Idx_t) {\ - if constexpr (STRUCT_PACK_##Type##_IS_PRIVATE) {\ - if constexpr (std::is_member_function_pointer_v) {\ - return (t.*field)();\ - }\ - else if constexpr (std::is_member_pointer_v) {\ - return t.*field;\ - }\ - else {\ - static_assert(sizeof(Type), "The parameters for STRUCT_PACK_REFL is error");\ - }\ + if constexpr (std::is_member_function_pointer_v) {\ + return (t.*field)();\ }\ else {\ - static int useless;\ - return useless;\ + return t.*field;\ }\ }\ +};\ +template \ +struct STRUCT_PACK_STEALER_CONST {\ friend const auto& STRUCT_PACK_STEAL(const Type& t, struct_pack::detail::Idx_t) {\ - if constexpr (STRUCT_PACK_##Type##_IS_PRIVATE) {\ - if constexpr (std::is_member_function_pointer_v) {\ - return (t.*field)();\ - }\ - else if constexpr (std::is_member_pointer_v) {\ - return t.*field;\ - }\ - else {\ - static_assert(sizeof(Type), "The parameters for STRUCT_PACK_REFL is error");\ - }\ + if constexpr (std::is_member_function_pointer_v) {\ + return (t.*field)();\ }\ else {\ - static int useless;\ - return useless;\ + return t.*field;\ }\ }\ -};\ +}; #define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X , Bank) \ auto& STRUCT_PACK_STEAL(Bank& bank, struct_pack::detail::Idx_t);\ const auto& STRUCT_PACK_STEAL(const Bank& bank, struct_pack::detail::Idx_t);\ -template \ -struct STRUCT_PACK_##Bank##_IS_PRIVATE_FUNCTION >\ - : std::false_type {};\ -template \ -struct STRUCT_PACK_##Bank##_IS_PRIVATE_FIELD >\ - : std::false_type {};\ -template struct STRUCT_PACK_STEALER;\ +template struct STRUCT_PACK_STEALER<\ + Bank, struct_pack::detail::member_function_resolve(&Bank::X, struct_pack::detail::Idx_t{}), Idx>;\ +template struct STRUCT_PACK_STEALER_CONST<\ + Bank, struct_pack::detail::const_member_function_resolve(&Bank::X, struct_pack::detail::Idx_t{}), Idx>; #define STRUCT_PACK_MAKE_ARGS(Type,Count) \ STRUCT_PACK_CONCAT(STRUCT_PACK_MAKE_ARGS,Count)(Type) @@ -2231,18 +2160,28 @@ template struct STRUCT_PACK_STEALER;\ #define STRUCT_PACK_RETURN_ELEMENT(Idx, X , parent) \ if constexpr (Idx == I) {\ return c.X;\ -}\ +} -#define STRUCT_PACK_GET_INDEX(Idx, Type, parent) \ +#define STRUCT_PACK_GET_INDEX(Idx, _, Type) \ auto& STRUCT_PACK_GET_##Idx(Type& c) {\ - return STRUCT_PACK_GET()-1-Idx>(c);\ -}\ + return STRUCT_PACK_STEAL(c,struct_pack::detail::Idx_t()-1-Idx>{});\ +} -#define STRUCT_PACK_GET_INDEX_CONST(Idx, Type, parent) \ +#define STRUCT_PACK_GET_INDEX_CONST(Idx, _, Type) \ const auto& STRUCT_PACK_GET_##Idx(const Type& c) {\ - return STRUCT_PACK_GET()-1-Idx>(c);\ -}\ + return STRUCT_PACK_STEAL(c,struct_pack::detail::Idx_t()-1-Idx>{});\ +} #define STRUCT_PACK_REFL(Type,...) \ STRUCT_PACK_STEALER_DECL(Type) \ -STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_VISIT_PRIVATE_HELPER,Type,__VA_ARGS__) \ No newline at end of file +STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_VISIT_PRIVATE_HELPER,Type,__VA_ARGS__) \ +Type& STRUCT_PACK_REFL_FLAG(Type& t) {return t;} \ +template \ +constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL(); \ +template<> \ +constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL() {return STRUCT_PACK_ARG_COUNT(__VA_ARGS__);} \ +decltype(auto) STRUCT_PACK_FIELD_COUNT(const Type &){ \ + return std::integral_constant{}; \ +} \ +STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_GET_INDEX,Type,__VA_ARGS__) \ +STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_GET_INDEX_CONST,Type,__VA_ARGS__) diff --git a/src/struct_pack/examples/non_aggregated_type.cpp b/src/struct_pack/examples/non_aggregated_type.cpp index 597d70d56..4b810d966 100644 --- a/src/struct_pack/examples/non_aggregated_type.cpp +++ b/src/struct_pack/examples/non_aggregated_type.cpp @@ -28,13 +28,13 @@ // 1. make sure your type has a default constructor // 2. add marco STRUCT_PACK_REFL(Type, field1, field2...) in the same namespace +// 3. The field can be public or private, it's OK. namespace example { class person : std::vector { private: - std::string mess; + int age; public: - int age; std::string name; auto operator==(const person& rhs) const { return age == rhs.age && name == rhs.name; @@ -46,50 +46,56 @@ class person : std::vector { STRUCT_PACK_REFL(person, age, name); } // namespace example -// 3. if you want to use private field, add friend declartion marco - +// 4. We also support member function in marco STRUCT_PACK_REFL(Type, +// MemFuncName1, MemFuncName2...) +// 5. Make sure the function has const version and non-const version overload. namespace example2 { class person { private: - int age; - std::string name; + int age_; + std::string name_; public: auto operator==(const person& rhs) const { - return age == rhs.age && name == rhs.name; + return age_ == rhs.age_ && name_ == rhs.name_; } person() = default; - person(int age, const std::string& name) : age(age), name(name) {} - STRUCT_PACK_FRIEND_DECL(person); + person(int age, const std::string& name) : age_(age), name_(name) {} + + int& age() { return age_; }; + const int& age() const { return age_; }; + std::string& name() { return name_; }; + const std::string& name() const { return name_; }; }; STRUCT_PACK_REFL(person, age, name); } // namespace example2 -// 4. you can also add function which return class member reference as -// struct_pack field. - +// 6. you can also mix the field and function with any order in marco +// STRUCT_PACK_REFL namespace example3 { class person { private: int age_; std::string name_; + double salary; public: auto operator==(const person& rhs) const { - return age_ == rhs.age_ && name_ == rhs.name_; + return age_ == rhs.age_ && name_ == rhs.name_ && salary == rhs.salary; } person() = default; - person(int age, const std::string& name) : age_(age), name_(name) {} + person(int age, const std::string& name, double salary) + : age_(age), name_(name) {} int& age() { return age_; }; const int& age() const { return age_; }; std::string& name() { return name_; }; const std::string& name() const { return name_; }; }; -STRUCT_PACK_REFL(person, age(), name()); +STRUCT_PACK_REFL(person, name, age, salary); } // namespace example3 -// 5. Remember, the STURCT_PACK_REFL marco disable the trivial_serialize +// 7. Remember, the STURCT_PACK_REFL marco disable the trivial_serialize // optimize. So don't use it for trivial type. namespace example4 { struct point { @@ -99,18 +105,34 @@ STRUCT_PACK_REFL(point, x, y, z); struct point2 { int x, y, z; }; +// The point & point2 are different type +static_assert(struct_pack::get_type_code() != + struct_pack::get_type_code()); } // namespace example4 -// 6. example5::person ,example::person, example2::person, example3:person are -// same type in struct_pack type system. +// 8. The order of field in STRUCT_PACK_REFL will change the type of struct in +// struct_pack type system namespace example5 { -struct person { +class person { int age; std::string name; auto operator==(const person& rhs) const { return age == rhs.age && name == rhs.name; } }; + +STRUCT_PACK_REFL(person, age, name); +class person2 { + int age; + std::string name; + auto operator==(const person2& rhs) const { + return age == rhs.age && name == rhs.name; + } +}; +STRUCT_PACK_REFL(person2, name, age); +// person & person 2 are different type +static_assert(struct_pack::get_type_code() != + struct_pack::get_type_code()); } // namespace example5 //clang-format off @@ -130,22 +152,10 @@ void non_aggregated_type() { assert(p == p2.value()); } { - example2::person p{20, "tom"}; + example3::person p{20, "tom", 114.514}; auto buffer = struct_pack::serialize(p); auto p3 = struct_pack::deserialize(buffer); assert(p3); assert(p == p3.value()); } - { - assert(struct_pack::get_type_code() != - struct_pack::get_type_code()); - } - { - assert(struct_pack::get_type_code() == - struct_pack::get_type_code()); - assert(struct_pack::get_type_code() == - struct_pack::get_type_code()); - assert(struct_pack::get_type_code() == - struct_pack::get_type_code()); - } } \ No newline at end of file diff --git a/src/struct_pack/tests/test_non_aggregated_type.cpp b/src/struct_pack/tests/test_non_aggregated_type.cpp index 9a8a07b06..60fbdd515 100644 --- a/src/struct_pack/tests/test_non_aggregated_type.cpp +++ b/src/struct_pack/tests/test_non_aggregated_type.cpp @@ -67,6 +67,7 @@ class person { private: std::string name_; int age_; + public: auto operator==(const person& rhs) const { @@ -76,9 +77,9 @@ class person { person(int age, const std::string& name) : age_(age), name_(name) {} int& age() { return age_; }; - // const int& age() const { return age_; }; + const int& age() const { return age_; }; std::string& name() { return name_; }; - // const std::string& name() const { return name_; }; + const std::string& name() const { return name_; }; }; STRUCT_PACK_REFL(person, age, name); } // namespace example3 @@ -267,3 +268,5 @@ TEST_CASE("test complicated_object") { CHECK(result); CHECK(result.value() == o); } + + diff --git a/test.cpp b/test.cpp index be2ef4314..53c651085 100644 --- a/test.cpp +++ b/test.cpp @@ -1,124 +1,86 @@ -#include +#include #include - template using Idx_t = std::integral_constant; - -template -struct is_private_member_field : std::true_type {}; - -template -struct is_private_member_function : std::true_type {}; - -template -constexpr bool is_private_member = is_private_member_field::value&& - is_private_member_function::value; - template -struct Thief2; - -template -struct Thief2 { - friend const auto& steal_impl(T& t, Idx_t) { +struct ThiefMember { + friend auto& steal_impl(T& t, Idx_t) { + if constexpr (std::is_member_function_pointer_v) { return (t.*field)(); } -}; - -template -struct Thief2 { - friend auto& steal_impl(T& t, Idx_t) { - return (t.*field)();\ - } -}; - -template -struct ThiefMemberFunction { - friend auto& steal_impl(T& t, Idx_t) { - if constexpr (std::is_same_v) {\ - static int useless; - return useless; - }\ - else {\ - return (t.*field)();\ - }\ + else { + return t.*field; + } } }; - template -struct ThiefMemberFunctionConst { +struct ThiefMemberConst { friend const auto& steal_impl(const T& t, Idx_t) { - if constexpr (std::is_same_v) {\ - static int useless; - return useless; - }\ - else {\ - return (t.*field)();\ - }\ + if constexpr (std::is_member_function_pointer_v) { + return (t.*field)(); + } + else { + return t.*field; + } } }; - - class Bank2 { float money = 999'999'999'999; - std::string name_; + std::string_view name_; bool sex_; - bool& sex() {return sex_;} - const bool& sex() const {return sex_;} + bool& sex() { return sex_; } + const bool& sex() const { return sex_; } + public: - std::string& name() { return name_; } - const std::string& name() const { return name_; } + std::string_view& name() { return name_; } + const std::string_view& name() const { return name_; } int age; }; - -template -constexpr inline std::nullptr_t member_function_test(U X) { - return nullptr; +template +constexpr inline decltype(auto) member_function_test(U X, Idx_t) { + return X; }; - -template -constexpr inline auto member_function_test(U (T::* X)()) { - return X; +template +constexpr inline decltype(auto) member_function_test(U (T::*X)(), Idx_t) { + return X; }; - -template -constexpr inline auto const_member_function_test(U X) { - return nullptr; +template +constexpr inline decltype(auto) const_member_function_test(U X, Idx_t) { + return X; }; - -template -constexpr inline auto const_member_function_test(U (T::* X)() const ) { - return X; +template +constexpr inline decltype(auto) const_member_function_test(U (T::*X)() const, + Idx_t) { + return X; }; - - - -#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X , Bank) \ -auto& steal_impl(Bank& bank, Idx_t);\ -const auto& steal_impl(const Bank& bank, Idx_t);\ -template \ -struct is_private_member_function >\ - : std::false_type {};\ -template \ -struct is_private_member_field >\ - : std::false_type {};\ -template struct ThiefMemberFunction(&Bank::X), Idx>;\ -template struct ThiefMemberFunctionConst(&Bank::X), Idx>;\ - - - - -//STRUCT_PACK_VISIT_PRIVATE_HELPER(0,money,Bank2) -STRUCT_PACK_VISIT_PRIVATE_HELPER(1,name,Bank2) -//STRUCT_PACK_VISIT_PRIVATE_HELPER(2,age,Bank2) -STRUCT_PACK_VISIT_PRIVATE_HELPER(3,sex,Bank2) +#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X, Bank) \ + auto& steal_impl(Bank& bank, Idx_t); \ + const auto& steal_impl(const Bank& bank, Idx_t); \ + template \ + constexpr inline decltype(auto) member_function_test( \ + auto (T::*x)()->decltype(std::declval().X()), Idx_t) { \ + return x; \ + }; \ + template \ + constexpr inline decltype(auto) const_member_function_test( \ + auto (T::*x)() const->decltype(std::declval().X()), \ + Idx_t) { \ + return x; \ + }; \ + template struct ThiefMember< \ + Bank, member_function_test(&Bank::X, Idx_t{}), Idx>; \ + template struct ThiefMemberConst< \ + Bank, const_member_function_test(&Bank::X, Idx_t{}), Idx>; + +STRUCT_PACK_VISIT_PRIVATE_HELPER(0, money, Bank2) +STRUCT_PACK_VISIT_PRIVATE_HELPER(1, name, Bank2) +STRUCT_PACK_VISIT_PRIVATE_HELPER(2, age, Bank2) +STRUCT_PACK_VISIT_PRIVATE_HELPER(3, sex, Bank2) void test(Bank2& bank) { - // steal_impl(bank,Idx_t<0>{}) = 100.04; - //static_assert(is_private_member); - steal_impl(bank,Idx_t<1>{}) = "hi"; - static_assert(!is_private_member); - //static_assert(!is_private_member); - steal_impl(bank,Idx_t<3>{}) = true; - static_assert(is_private_member); -} - + steal_impl(bank, Idx_t<0>{}) = 100.04; + steal_impl(bank, Idx_t<1>{}) = "hi"; + steal_impl(bank, Idx_t<2>{}) = 0; + // not ok in msvc + steal_impl(bank, Idx_t<3>{}) = true; +} \ No newline at end of file diff --git a/website/docs/en/struct_pack/struct_pack_intro.md b/website/docs/en/struct_pack/struct_pack_intro.md index 47935e4de..28beec5fe 100644 --- a/website/docs/en/struct_pack/struct_pack_intro.md +++ b/website/docs/en/struct_pack/struct_pack_intro.md @@ -175,16 +175,15 @@ assert(nested2==nested1); ### custom reflection -Sometimes user need support non-aggregated type, or adjust the order of each field, which can be supported by macro function `STRUCT_PACK_REFL(typename, fieldname1, fieldname2 ...)`. +Sometimes user need support non-aggregated type, or adjust the order of each field, which can be supported by macro function `STRUCT_PACK_REFL(typename, fieldname1, fieldname2 ...)`. The field can be private or public. ```cpp namespace test { class person : std::vector { private: - std::string mess; + int age; public: - int age; std::string name; auto operator==(const person& rhs) const { return age == rhs.age && name == rhs.name; @@ -197,31 +196,9 @@ STRUCT_PACK_REFL(person, name, age); The first argument of `STRUCT_PACK_REFL(typename, fieldname1, fieldname2 ...)` is the name of the type reflected, the others are the field names. The macro must be defined in the same namespace of the reflected type. -The macro allow struct_pack support non-aggregated type, allow user define constructor, derived from other type or add some field which don't serialize. -``` - -Sometimes, user want to serialize/deserialize some private fields, which can be supported by macro function `STRUCT_PACK_FRIEND_DECL(typename)`. -```cpp -namespace example2 { -class person { - private: - int age; - std::string name; - - public: - auto operator==(const person& rhs) const { - return age == rhs.age && name == rhs.name; - } - person() = default; - person(int age, const std::string& name) : age(age), name(name) {} - STRUCT_PACK_FRIEND_DECL(person); -}; -STRUCT_PACK_REFL(person, age, name); -} // namespace example2 +The macro allow struct_pack support non-aggregated type, allow user define constructor, derived from other type or ignore some field. ``` -This macro must declared inside the struct, it regist struct_pack reflection function as friend function. - User can even register member function in macro function `STRUCT_PACK_REFL`, which greatly expands the flexibility of struct_pack. diff --git a/website/docs/zh/struct_pack/struct_pack_intro.md b/website/docs/zh/struct_pack/struct_pack_intro.md index f468f3648..d7b5db910 100644 --- a/website/docs/zh/struct_pack/struct_pack_intro.md +++ b/website/docs/zh/struct_pack/struct_pack_intro.md @@ -196,28 +196,6 @@ STRUCT_PACK_REFL(person, name, age); 它使得struct_pack可以支持那些非聚合的结构体类型,允许用户自定义构造函数,继承其他类型,添加不序列化的字段等等。 ``` -有时,用户需要序列化/反序列化那些private字段,这可以通过函数`STRUCT_PACK_FRIEND_DECL(typenmae)`;来支持。 -```cpp -namespace example2 { -class person { - private: - int age; - std::string name; - - public: - auto operator==(const person& rhs) const { - return age == rhs.age && name == rhs.name; - } - person() = default; - person(int age, const std::string& name) : age(age), name(name) {} - STRUCT_PACK_FRIEND_DECL(person); -}; -STRUCT_PACK_REFL(person, age, name); -} // namespace example2 -``` - -该宏必须声明在结构体内部,其原理是将struct_pack与反射有关的函数注册为友元函数。 - 用户甚至可以在`STRUCT_PACK_REFL`中注册成员函数,这极大的扩展了struct_pack的灵活性。 ```cpp From c2bafebb5a9893fc310dcf81cae5c1ef8c74ef63 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 8 Aug 2023 14:09:48 +0800 Subject: [PATCH 3/6] fix doc --- website/docs/en/struct_pack/struct_pack_intro.md | 2 +- website/docs/zh/struct_pack/struct_pack_intro.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/en/struct_pack/struct_pack_intro.md b/website/docs/en/struct_pack/struct_pack_intro.md index 28beec5fe..3ab448f0e 100644 --- a/website/docs/en/struct_pack/struct_pack_intro.md +++ b/website/docs/en/struct_pack/struct_pack_intro.md @@ -221,7 +221,7 @@ class person { std::string& name() { return name_; }; const std::string& name() const { return name_; }; }; -STRUCT_PACK_REFL(person, age(), name()); +STRUCT_PACK_REFL(person, age, name); } // namespace example3 ``` The member function registed must return a reference, and this function must have a const version overload & non-const overload. diff --git a/website/docs/zh/struct_pack/struct_pack_intro.md b/website/docs/zh/struct_pack/struct_pack_intro.md index d7b5db910..416187446 100644 --- a/website/docs/zh/struct_pack/struct_pack_intro.md +++ b/website/docs/zh/struct_pack/struct_pack_intro.md @@ -217,7 +217,7 @@ class person { std::string& name() { return name_; }; const std::string& name() const { return name_; }; }; -STRUCT_PACK_REFL(person, age(), name()); +STRUCT_PACK_REFL(person, age, name); } // namespace example3 注册的成员函数必须返回一个引用,并且该函数具有常量和非常量的重载。 From a8289faf717207ff76a7d6b4ee2c5eb872417d53 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 8 Aug 2023 14:11:08 +0800 Subject: [PATCH 4/6] fix doc --- website/docs/zh/struct_pack/struct_pack_intro.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/website/docs/zh/struct_pack/struct_pack_intro.md b/website/docs/zh/struct_pack/struct_pack_intro.md index 416187446..2aa3ced4b 100644 --- a/website/docs/zh/struct_pack/struct_pack_intro.md +++ b/website/docs/zh/struct_pack/struct_pack_intro.md @@ -176,10 +176,9 @@ assert(nested2==nested1); namespace test { class person : std::vector { private: - std::string mess; + int age; public: - int age; std::string name; auto operator==(const person& rhs) const { return age == rhs.age && name == rhs.name; From 0ab5963ab19148b94fa76c66d46c3d9d00dfc79c Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 8 Aug 2023 14:17:49 +0800 Subject: [PATCH 5/6] fix --- src/struct_pack/examples/non_aggregated_type.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/struct_pack/examples/non_aggregated_type.cpp b/src/struct_pack/examples/non_aggregated_type.cpp index 4b810d966..99c7f6d1c 100644 --- a/src/struct_pack/examples/non_aggregated_type.cpp +++ b/src/struct_pack/examples/non_aggregated_type.cpp @@ -154,7 +154,7 @@ void non_aggregated_type() { { example3::person p{20, "tom", 114.514}; auto buffer = struct_pack::serialize(p); - auto p3 = struct_pack::deserialize(buffer); + auto p3 = struct_pack::deserialize(buffer); assert(p3); assert(p == p3.value()); } From ea71709171cd043411fab61ca12f95a45fd6a927 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 8 Aug 2023 15:43:59 +0800 Subject: [PATCH 6/6] fix msvc --- include/ylt/struct_pack/reflection.hpp | 189 +++++++++++------- .../examples/non_aggregated_type.cpp | 4 +- .../tests/test_non_aggregated_type.cpp | 3 - test.cpp | 86 -------- test2.cpp | 116 ----------- 5 files changed, 122 insertions(+), 276 deletions(-) delete mode 100644 test.cpp delete mode 100644 test2.cpp diff --git a/include/ylt/struct_pack/reflection.hpp b/include/ylt/struct_pack/reflection.hpp index 96ff41ead..e0be4ac99 100644 --- a/include/ylt/struct_pack/reflection.hpp +++ b/include/ylt/struct_pack/reflection.hpp @@ -2112,76 +2112,127 @@ constexpr inline decltype(auto) const_member_function_resolve(U (T::*X)() const, #define STRUCT_PACK_DOARG63(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG62(s,f,p,__VA_ARGS__)) s f(62,t,p) #define STRUCT_PACK_DOARG64(s,f,p,t,...) STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_DOARG63(s,f,p,__VA_ARGS__)) s f(63,t,p) +// clang-format on + +#define STRUCT_PACK_STEALER_DECL(Type) \ + template \ + constexpr inline decltype(auto) STRUCT_PACK_MEMBER_FUNCTION_RESOLVE( \ + Type *, U X, struct_pack::detail::Idx_t) { \ + return X; \ + }; \ + template \ + constexpr inline decltype(auto) STRUCT_PACK_MEMBER_FUNCTION_RESOLVE( \ + Type *, U (T::*X)(), struct_pack::detail::Idx_t) { \ + return X; \ + }; \ + template \ + constexpr inline decltype(auto) STRUCT_PACK_CONST_MEMBER_FUNCTION_RESOLVE( \ + Type *, U X, struct_pack::detail::Idx_t) { \ + return X; \ + }; \ + template \ + constexpr inline decltype(auto) STRUCT_PACK_CONST_MEMBER_FUNCTION_RESOLVE( \ + Type *, U (T::*X)() const, struct_pack::detail::Idx_t) { \ + return X; \ + }; \ + template \ + struct STRUCT_PACK_STEALER; \ + template \ + struct STRUCT_PACK_STEALER_CONST; \ + template \ + struct STRUCT_PACK_STEALER { \ + friend auto &STRUCT_PACK_STEAL(Type &t, struct_pack::detail::Idx_t) { \ + if constexpr (std::is_member_function_pointer_v) { \ + return (t.*field)(); \ + } \ + else { \ + return t.*field; \ + } \ + } \ + }; \ + template \ + struct STRUCT_PACK_STEALER_CONST { \ + friend const auto &STRUCT_PACK_STEAL(const Type &t, \ + struct_pack::detail::Idx_t) { \ + if constexpr (std::is_member_function_pointer_v) { \ + return (t.*field)(); \ + } \ + else { \ + return t.*field; \ + } \ + } \ + }; -#define STRUCT_PACK_STEALER_DECL(Type)\ -template \ -struct STRUCT_PACK_STEALER;\ -template \ -struct STRUCT_PACK_STEALER_CONST;\ -template \ -struct STRUCT_PACK_STEALER {\ - friend auto& STRUCT_PACK_STEAL(Type& t, struct_pack::detail::Idx_t) {\ - if constexpr (std::is_member_function_pointer_v) {\ - return (t.*field)();\ - }\ - else {\ - return t.*field;\ - }\ - }\ -};\ -template \ -struct STRUCT_PACK_STEALER_CONST {\ - friend const auto& STRUCT_PACK_STEAL(const Type& t, struct_pack::detail::Idx_t) {\ - if constexpr (std::is_member_function_pointer_v) {\ - return (t.*field)();\ - }\ - else {\ - return t.*field;\ - }\ - }\ -}; - -#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X , Bank) \ -auto& STRUCT_PACK_STEAL(Bank& bank, struct_pack::detail::Idx_t);\ -const auto& STRUCT_PACK_STEAL(const Bank& bank, struct_pack::detail::Idx_t);\ -template struct STRUCT_PACK_STEALER<\ - Bank, struct_pack::detail::member_function_resolve(&Bank::X, struct_pack::detail::Idx_t{}), Idx>;\ -template struct STRUCT_PACK_STEALER_CONST<\ - Bank, struct_pack::detail::const_member_function_resolve(&Bank::X, struct_pack::detail::Idx_t{}), Idx>; - -#define STRUCT_PACK_MAKE_ARGS(Type,Count) \ - STRUCT_PACK_CONCAT(STRUCT_PACK_MAKE_ARGS,Count)(Type) - -#define STRUCT_PACK_EXPAND_EACH_(sepatator,fun,parent,...) \ - STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_CONCAT(STRUCT_PACK_DOARG,STRUCT_PACK_ARG_COUNT(__VA_ARGS__))(sepatator,fun,parent,__VA_ARGS__)) -#define STRUCT_PACK_EXPAND_EACH(sepatator,fun,parent,...) \ - STRUCT_PACK_EXPAND_EACH_(sepatator,fun,parent,__VA_ARGS__) - -#define STRUCT_PACK_RETURN_ELEMENT(Idx, X , parent) \ -if constexpr (Idx == I) {\ - return c.X;\ -} +#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X, Bank) \ + auto &STRUCT_PACK_STEAL(Bank &bank, struct_pack::detail::Idx_t); \ + const auto &STRUCT_PACK_STEAL(const Bank &bank, \ + struct_pack::detail::Idx_t); \ + template \ + constexpr inline decltype(auto) STRUCT_PACK_MEMBER_FUNCTION_RESOLVE( \ + Bank *, auto (T::*x)()->decltype(std::declval().X()), \ + struct_pack::detail::Idx_t) { \ + return x; \ + }; \ + template \ + constexpr inline decltype(auto) STRUCT_PACK_CONST_MEMBER_FUNCTION_RESOLVE( \ + Bank *, auto (T::*x)() const->decltype(std::declval().X()), \ + struct_pack::detail::Idx_t) { \ + return x; \ + }; \ + template struct STRUCT_PACK_STEALER< \ + Bank, \ + STRUCT_PACK_MEMBER_FUNCTION_RESOLVE( \ + (Bank *)nullptr, &Bank::X, struct_pack::detail::Idx_t{}), \ + Idx>; \ + template struct STRUCT_PACK_STEALER_CONST< \ + Bank, \ + STRUCT_PACK_CONST_MEMBER_FUNCTION_RESOLVE( \ + (Bank *)nullptr, &Bank::X, struct_pack::detail::Idx_t{}), \ + Idx>; + +#define STRUCT_PACK_MAKE_ARGS(Type, Count) \ + STRUCT_PACK_CONCAT(STRUCT_PACK_MAKE_ARGS, Count)(Type) + +#define STRUCT_PACK_EXPAND_EACH_(sepatator, fun, parent, ...) \ + STRUCT_PACK_MARCO_EXPAND(STRUCT_PACK_CONCAT( \ + STRUCT_PACK_DOARG, STRUCT_PACK_ARG_COUNT(__VA_ARGS__))( \ + sepatator, fun, parent, __VA_ARGS__)) +#define STRUCT_PACK_EXPAND_EACH(sepatator, fun, parent, ...) \ + STRUCT_PACK_EXPAND_EACH_(sepatator, fun, parent, __VA_ARGS__) + +#define STRUCT_PACK_RETURN_ELEMENT(Idx, X, parent) \ + if constexpr (Idx == I) { \ + return c.X; \ + } -#define STRUCT_PACK_GET_INDEX(Idx, _, Type) \ -auto& STRUCT_PACK_GET_##Idx(Type& c) {\ - return STRUCT_PACK_STEAL(c,struct_pack::detail::Idx_t()-1-Idx>{});\ -} +#define STRUCT_PACK_GET_INDEX(Idx, _, Type) \ + auto &STRUCT_PACK_GET_##Idx(Type &c) { \ + return STRUCT_PACK_STEAL( \ + c, struct_pack::detail::Idx_t() - \ + 1 - Idx>{}); \ + } -#define STRUCT_PACK_GET_INDEX_CONST(Idx, _, Type) \ -const auto& STRUCT_PACK_GET_##Idx(const Type& c) {\ - return STRUCT_PACK_STEAL(c,struct_pack::detail::Idx_t()-1-Idx>{});\ -} +#define STRUCT_PACK_GET_INDEX_CONST(Idx, _, Type) \ + const auto &STRUCT_PACK_GET_##Idx(const Type &c) { \ + return STRUCT_PACK_STEAL( \ + c, struct_pack::detail::Idx_t() - \ + 1 - Idx>{}); \ + } -#define STRUCT_PACK_REFL(Type,...) \ -STRUCT_PACK_STEALER_DECL(Type) \ -STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_VISIT_PRIVATE_HELPER,Type,__VA_ARGS__) \ -Type& STRUCT_PACK_REFL_FLAG(Type& t) {return t;} \ -template \ -constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL(); \ -template<> \ -constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL() {return STRUCT_PACK_ARG_COUNT(__VA_ARGS__);} \ -decltype(auto) STRUCT_PACK_FIELD_COUNT(const Type &){ \ - return std::integral_constant{}; \ -} \ -STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_GET_INDEX,Type,__VA_ARGS__) \ -STRUCT_PACK_EXPAND_EACH(,STRUCT_PACK_GET_INDEX_CONST,Type,__VA_ARGS__) +#define STRUCT_PACK_REFL(Type, ...) \ + STRUCT_PACK_STEALER_DECL(Type) \ + STRUCT_PACK_EXPAND_EACH(, STRUCT_PACK_VISIT_PRIVATE_HELPER, Type, \ + __VA_ARGS__) \ + Type &STRUCT_PACK_REFL_FLAG(Type &t) { return t; } \ + template \ + constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL(); \ + template <> \ + constexpr std::size_t STRUCT_PACK_FIELD_COUNT_IMPL() { \ + return STRUCT_PACK_ARG_COUNT(__VA_ARGS__); \ + } \ + decltype(auto) STRUCT_PACK_FIELD_COUNT(const Type &) { \ + return std::integral_constant{}; \ + } \ + STRUCT_PACK_EXPAND_EACH(, STRUCT_PACK_GET_INDEX, Type, __VA_ARGS__) \ + STRUCT_PACK_EXPAND_EACH(, STRUCT_PACK_GET_INDEX_CONST, Type, __VA_ARGS__) diff --git a/src/struct_pack/examples/non_aggregated_type.cpp b/src/struct_pack/examples/non_aggregated_type.cpp index 99c7f6d1c..517519896 100644 --- a/src/struct_pack/examples/non_aggregated_type.cpp +++ b/src/struct_pack/examples/non_aggregated_type.cpp @@ -106,8 +106,8 @@ struct point2 { int x, y, z; }; // The point & point2 are different type -static_assert(struct_pack::get_type_code() != - struct_pack::get_type_code()); +static_assert(struct_pack::get_type_code() != + struct_pack::get_type_code()); } // namespace example4 // 8. The order of field in STRUCT_PACK_REFL will change the type of struct in diff --git a/src/struct_pack/tests/test_non_aggregated_type.cpp b/src/struct_pack/tests/test_non_aggregated_type.cpp index 60fbdd515..a5a0d3436 100644 --- a/src/struct_pack/tests/test_non_aggregated_type.cpp +++ b/src/struct_pack/tests/test_non_aggregated_type.cpp @@ -67,7 +67,6 @@ class person { private: std::string name_; int age_; - public: auto operator==(const person& rhs) const { @@ -268,5 +267,3 @@ TEST_CASE("test complicated_object") { CHECK(result); CHECK(result.value() == o); } - - diff --git a/test.cpp b/test.cpp deleted file mode 100644 index 53c651085..000000000 --- a/test.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -template -using Idx_t = std::integral_constant; -template -struct ThiefMember { - friend auto& steal_impl(T& t, Idx_t) { - if constexpr (std::is_member_function_pointer_v) { - return (t.*field)(); - } - else { - return t.*field; - } - } -}; -template -struct ThiefMemberConst { - friend const auto& steal_impl(const T& t, Idx_t) { - if constexpr (std::is_member_function_pointer_v) { - return (t.*field)(); - } - else { - return t.*field; - } - } -}; -class Bank2 { - float money = 999'999'999'999; - std::string_view name_; - bool sex_; - bool& sex() { return sex_; } - const bool& sex() const { return sex_; } - - public: - std::string_view& name() { return name_; } - const std::string_view& name() const { return name_; } - int age; -}; -template -constexpr inline decltype(auto) member_function_test(U X, Idx_t) { - return X; -}; -template -constexpr inline decltype(auto) member_function_test(U (T::*X)(), Idx_t) { - return X; -}; -template -constexpr inline decltype(auto) const_member_function_test(U X, Idx_t) { - return X; -}; -template -constexpr inline decltype(auto) const_member_function_test(U (T::*X)() const, - Idx_t) { - return X; -}; -#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X, Bank) \ - auto& steal_impl(Bank& bank, Idx_t); \ - const auto& steal_impl(const Bank& bank, Idx_t); \ - template \ - constexpr inline decltype(auto) member_function_test( \ - auto (T::*x)()->decltype(std::declval().X()), Idx_t) { \ - return x; \ - }; \ - template \ - constexpr inline decltype(auto) const_member_function_test( \ - auto (T::*x)() const->decltype(std::declval().X()), \ - Idx_t) { \ - return x; \ - }; \ - template struct ThiefMember< \ - Bank, member_function_test(&Bank::X, Idx_t{}), Idx>; \ - template struct ThiefMemberConst< \ - Bank, const_member_function_test(&Bank::X, Idx_t{}), Idx>; - -STRUCT_PACK_VISIT_PRIVATE_HELPER(0, money, Bank2) -STRUCT_PACK_VISIT_PRIVATE_HELPER(1, name, Bank2) -STRUCT_PACK_VISIT_PRIVATE_HELPER(2, age, Bank2) -STRUCT_PACK_VISIT_PRIVATE_HELPER(3, sex, Bank2) - -void test(Bank2& bank) { - steal_impl(bank, Idx_t<0>{}) = 100.04; - steal_impl(bank, Idx_t<1>{}) = "hi"; - steal_impl(bank, Idx_t<2>{}) = 0; - // not ok in msvc - steal_impl(bank, Idx_t<3>{}) = true; -} \ No newline at end of file diff --git a/test2.cpp b/test2.cpp deleted file mode 100644 index 65e15e896..000000000 --- a/test2.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include -#include - -template -using Idx_t = std::integral_constant; - -template -struct is_private_member_field : std::true_type {}; - -template -struct is_private_member_function : std::true_type {}; - -template -constexpr bool is_private_member = is_private_member_field::value&& - is_private_member_function::value; - -template -struct Thief2; - -template -struct Thief2 { - friend const auto& steal_impl(T& t, Idx_t) { return (t.*field)(); } -}; - -template -struct Thief2 { - friend auto& steal_impl(T& t, Idx_t) { return (t.*field)(); } -}; - -template -struct ThiefMemberFunction { - friend auto& steal_impl(T& t, Idx_t) { - if constexpr (std::is_same_v) { - static int useless; - return useless; - } - else { - return (t.*field)(); - } - } -}; - -template -struct ThiefMemberFunctionConst { - friend const auto& steal_impl(const T& t, Idx_t) { - if constexpr (std::is_same_v) { - static int useless; - return useless; - } - else { - return (t.*field)(); - } - } -}; - -class Bank2 { - float money = 999'999'999'999; - std::string name_; - bool sex_; - bool& sex() { return sex_; } - const bool& sex() const { return sex_; } - - public: - std::string& name() { return name_; } - const std::string& name() const { return name_; } - int age; -}; - -template -constexpr inline std::nullptr_t member_function_test(U X) { - return nullptr; -}; - -template -constexpr inline auto member_function_test(U (T::*X)()) { - return X; -}; - -template -constexpr inline auto const_member_function_test(U X) { - return nullptr; -}; - -template -constexpr inline auto const_member_function_test(U (T::*X)() const) { - return X; -}; - -#define STRUCT_PACK_VISIT_PRIVATE_HELPER(Idx, X, Bank) \ - auto& steal_impl(Bank& bank, Idx_t); \ - const auto& steal_impl(const Bank& bank, Idx_t); \ - template \ - struct is_private_member_function > \ - : std::false_type {}; \ - template \ - struct is_private_member_field > \ - : std::false_type {}; \ - template struct ThiefMemberFunction< \ - Bank, member_function_test(&Bank::X), Idx>; \ - template struct ThiefMemberFunctionConst< \ - Bank, const_member_function_test(&Bank::X), Idx>; - -// STRUCT_PACK_VISIT_PRIVATE_HELPER(0,money,Bank2) -STRUCT_PACK_VISIT_PRIVATE_HELPER(1, name, Bank2) -// STRUCT_PACK_VISIT_PRIVATE_HELPER(2,age,Bank2) -STRUCT_PACK_VISIT_PRIVATE_HELPER(3, sex, Bank2) - -void test(Bank2& bank) { - // steal_impl(bank,Idx_t<0>{}) = 100.04; - // static_assert(is_private_member); - steal_impl(bank, Idx_t<1>{}) = "hi"; - static_assert(!is_private_member); - // static_assert(!is_private_member); - steal_impl(bank, Idx_t<3>{}) = true; - static_assert(is_private_member); -}