diff --git a/include/visit_struct/visit_struct.hpp b/include/visit_struct/visit_struct.hpp index 4d4bbc6..1632307 100644 --- a/include/visit_struct/visit_struct.hpp +++ b/include/visit_struct/visit_struct.hpp @@ -275,6 +275,22 @@ VISIT_STRUCT_CONSTEXPR auto get_accessor(S &&) -> decltype(get_accessor( return get_accessor(); } +// Get member offset, by index +template +VISIT_STRUCT_CONSTEXPR auto get_offset() -> + typename std::enable_if< + traits::is_visitable>::value, + decltype(traits::visitable>::get_offset(std::integral_constant{})) + >::type +{ + return traits::visitable>::get_offset(std::integral_constant{}); +} + +template +VISIT_STRUCT_CONSTEXPR auto get_offset(S &&) -> size_t { + return get_offset(); +} + // Get type, by index template struct type_at_s { @@ -285,6 +301,7 @@ struct type_at_s { template using type_at = typename type_at_s::type; + // Get name of structure template VISIT_STRUCT_CONSTEXPR auto get_name() -> @@ -638,6 +655,9 @@ static VISIT_STRUCT_CONSTEXPR const int max_visitable_members = 69; * These macros are used with VISIT_STRUCT_PP_MAP */ + #define VISIT_STRUCT_MEMBER_HELPER_OFFSET(MEMBER_NAME) \ + std::forward(visitor)(#MEMBER_NAME, [](){return offsetof(this_type, MEMBER_NAME);}); + #define VISIT_STRUCT_FIELD_COUNT(MEMBER_NAME) \ + 1 @@ -683,6 +703,12 @@ static VISIT_STRUCT_CONSTEXPR const int max_visitable_members = 69; return {}; \ } \ \ + static VISIT_STRUCT_CONSTEXPR auto \ + get_offset(std::integral_constant) -> \ + size_t { \ + return offsetof(this_type, MEMBER_NAME); \ + } \ + \ static auto \ type_at(std::integral_constant) -> \ visit_struct::type_c; @@ -749,6 +775,12 @@ struct visitable { VISIT_STRUCT_PP_MAP(VISIT_STRUCT_MEMBER_HELPER_ACC, __VA_ARGS__) \ } \ \ + template \ + VISIT_STRUCT_CXX14_CONSTEXPR static void visit_offsets(V && visitor) \ + { \ + VISIT_STRUCT_PP_MAP(VISIT_STRUCT_MEMBER_HELPER_OFFSET, __VA_ARGS__) \ + } \ + \ struct fields_enum { \ enum index { __VA_ARGS__ }; \ }; \ @@ -809,6 +841,12 @@ struct visitable { VISIT_STRUCT_PP_MAP(VISIT_STRUCT_MEMBER_HELPER_ACC, __VA_ARGS__) \ } \ \ + template \ + VISIT_STRUCT_CXX14_CONSTEXPR static void visit_offsets(V && visitor) \ + { \ + VISIT_STRUCT_PP_MAP(VISIT_STRUCT_MEMBER_HELPER_OFFSET, __VA_ARGS__) \ + } \ + \ struct fields_enum { \ enum index { __VA_ARGS__ }; \ }; \