Skip to content

Commit 61803bd

Browse files
committed
Added reflection support for empty derived types and improved serialization handling for empty member sequences
- Introduced `SYSLIB_SERIALIZE_DERIVED_EMPTY` macro for handling empty derived types - Enhanced serialization macros to handle empty member sequences gracefully - Updated CMakeLists to include Boost preprocessor headers in the build process
1 parent 5bd5a15 commit 61803bd

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

libraries/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ if (ENABLE_NATIVE_COMPILER)
2727
add_subdirectory(native)
2828
endif()
2929

30+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/boost/include/boost/preprocessor DESTINATION ${BASE_BINARY_DIR}/include/boost)
3031
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/meta_refl/include/bluegrass DESTINATION ${BASE_BINARY_DIR}/include)

libraries/sysiolib/core/sysio/serialize.hpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
#pragma once
2+
13
#include <bluegrass/meta/preprocessor.hpp>
4+
#include <boost/preprocessor/seq/for_each.hpp>
5+
#include <boost/preprocessor/seq/enum.hpp>
6+
#include <boost/preprocessor/seq/size.hpp>
7+
#include <boost/preprocessor/seq/seq.hpp>
8+
#include <boost/preprocessor/stringize.hpp>
29

310
#define SYSLIB_REFLECT_MEMBER_OP( OP, elem ) \
411
OP t.elem
512

13+
#define SYSLIB_REFLECT_MEMBER_COUNT( OP, elem ) \
14+
OP 1
15+
16+
#define SYSLIB_REFLECT_SEQ_NIL(x) (x)
17+
18+
//#define SYSLIB_REFLECT_ENUM(...) BLUEGRASS_META_SEQ_ENUM(__VA_ARGS__)
619
/**
720
* @defgroup serialize Serialize
821
* @ingroup core
@@ -19,11 +32,13 @@
1932
#define SYSLIB_SERIALIZE( TYPE, MEMBERS ) \
2033
template<typename DataStream> \
2134
friend DataStream& operator << ( DataStream& ds, const TYPE& t ){ \
22-
return ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, <<, MEMBERS );\
35+
uint32_t member_count = 0 BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_COUNT, +, MEMBERS ) ; \
36+
return member_count == 0 ? ds : (ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, <<, MEMBERS ));\
2337
}\
2438
template<typename DataStream> \
2539
friend DataStream& operator >> ( DataStream& ds, TYPE& t ){ \
26-
return ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, >>, MEMBERS );\
40+
uint32_t member_count = 0 BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_COUNT, +, MEMBERS ) ; \
41+
return member_count == 0 ? ds : (ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, >>, MEMBERS ));\
2742
}
2843

2944
/**
@@ -39,10 +54,24 @@
3954
template<typename DataStream> \
4055
friend DataStream& operator << ( DataStream& ds, const TYPE& t ){ \
4156
ds << static_cast<const BASE&>(t); \
42-
return ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, <<, MEMBERS );\
57+
uint32_t member_count = 0 BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_COUNT, +, MEMBERS ); \
58+
return member_count == 0 ? ds : (ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, <<, MEMBERS ));\
4359
}\
4460
template<typename DataStream> \
4561
friend DataStream& operator >> ( DataStream& ds, TYPE& t ){ \
4662
ds >> static_cast<BASE&>(t); \
47-
return ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, >>, MEMBERS );\
63+
uint32_t member_count = 0 BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_COUNT, +, MEMBERS ); \
64+
return member_count == 0 ? ds : (ds BLUEGRASS_META_FOREACH_SEQ( SYSLIB_REFLECT_MEMBER_OP, >>, MEMBERS ));\
4865
}
66+
67+
#define SYSLIB_SERIALIZE_DERIVED_EMPTY( TYPE, BASE ) \
68+
template<typename DataStream> \
69+
friend DataStream& operator << ( DataStream& ds, const TYPE& t ){ \
70+
ds << static_cast<const BASE&>(t); \
71+
return ds;\
72+
}\
73+
template<typename DataStream> \
74+
friend DataStream& operator >> ( DataStream& ds, TYPE& t ){ \
75+
ds >> static_cast<BASE&>(t); \
76+
return ds;\
77+
}

0 commit comments

Comments
 (0)