Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions include/visit_struct/visit_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ VISIT_STRUCT_CONSTEXPR auto get_accessor(S &&) -> decltype(get_accessor<idx, S>(
return get_accessor<idx, S>();
}

// Get member offset, by index
template <int idx, typename S>
VISIT_STRUCT_CONSTEXPR auto get_offset() ->
typename std::enable_if<
traits::is_visitable<traits::clean_t<S>>::value,
decltype(traits::visitable<traits::clean_t<S>>::get_offset(std::integral_constant<int, idx>{}))
>::type
{
return traits::visitable<traits::clean_t<S>>::get_offset(std::integral_constant<int, idx>{});
}

template <int idx, typename S>
VISIT_STRUCT_CONSTEXPR auto get_offset(S &&) -> size_t {
return get_offset<idx, S>();
}

// Get type, by index
template <int idx, typename S>
struct type_at_s {
Expand All @@ -285,6 +301,7 @@ struct type_at_s {
template <int idx, typename S>
using type_at = typename type_at_s<idx, S>::type;


// Get name of structure
template <typename S>
VISIT_STRUCT_CONSTEXPR auto get_name() ->
Expand Down Expand Up @@ -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<V>(visitor)(#MEMBER_NAME, [](){return offsetof(this_type, MEMBER_NAME);});

#define VISIT_STRUCT_FIELD_COUNT(MEMBER_NAME) \
+ 1

Expand Down Expand Up @@ -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<int, fields_enum::MEMBER_NAME>) -> \
size_t { \
return offsetof(this_type, MEMBER_NAME); \
} \
\
static auto \
type_at(std::integral_constant<int, fields_enum::MEMBER_NAME>) -> \
visit_struct::type_c<decltype(this_type::MEMBER_NAME)>;
Expand Down Expand Up @@ -749,6 +775,12 @@ struct visitable<STRUCT_NAME, void> {
VISIT_STRUCT_PP_MAP(VISIT_STRUCT_MEMBER_HELPER_ACC, __VA_ARGS__) \
} \
\
template <typename V> \
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__ }; \
}; \
Expand Down Expand Up @@ -809,6 +841,12 @@ struct visitable<STRUCT_NAME, CONTEXT> {
VISIT_STRUCT_PP_MAP(VISIT_STRUCT_MEMBER_HELPER_ACC, __VA_ARGS__) \
} \
\
template <typename V> \
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__ }; \
}; \
Expand Down