Skip to content

Commit

Permalink
support customize class member name
Browse files Browse the repository at this point in the history
  • Loading branch information
zwkno1 committed Mar 21, 2019
1 parent adfe8fe commit 25fce7c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ build*
.vscode
.vscode/.cmaketools.json
.vscode/launch.json
CMakeLists.txt.user
CMakeLists.txt.asc
CMakeLists.txt.asc.gpg
CMakeLists.txt.gpg
Expand Down
22 changes: 22 additions & 0 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ struct AddressBook
SERIALIZATION_DEFINE(people, opt, mapTest, vectorTest)
};

struct Customize
{
int a;
std::string b;

bool operator==(const Customize & other) const
{
return (a == other.a) && (b == other.b);
}

SERIALIZATION_DEFINE_META_DATA("aaa", "bbb")
SERIALIZATION_DEFINE_META_FUNC(a, b)
};

int main(int argc, char * argv[])
{
try
Expand Down Expand Up @@ -86,6 +100,14 @@ int main(int argc, char * argv[])
serialization::unserialize(xml_ia, ab3);
std::cout << "xml unserilize result: " << (ab == ab3 ? "success" : "fail") << std::endl;

//test coustomize
Customize c{123, "456"}, c2;
serialization::serialize(json_oa, c);
std::cout << "customize json serilize result: " << json_oa.data() << std::endl;
json_ia.load_data(json_oa.data());
serialization::unserialize(json_ia, c2);
std::cout << "customize json unserilize result: " << (c == c2 ? "success" : "fail") << std::endl;

}
catch (serialization::serialization_error & err)
{
Expand Down
27 changes: 21 additions & 6 deletions include/serialization/detail/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,27 @@

#define SERIALIZATION_MACRO_WRAPPER(...) __VA_ARGS__

#define SERIALIZATION_MAKE_META_IMPL(N, ...) \
inline static const std::array<const char*, N> & serialization_meta() \
#define SERIALIZATION_DEFINE_META_DATA(...) \
inline static const auto & serialization_meta() \
{ \
static std::array<const char*, N> meta = { SERIALIZATION_MACRO_WRAPPER(SERIALIZATION_CONCAT(SERIALIZATION_EXPAND, N)(__VA_ARGS__)) }; \
static std::array<const char*, serialization::detail::get_args_count(__VA_ARGS__)> meta = { __VA_ARGS__ }; \
return meta; \
} \
} \

#define SERIALIZATION_DEFINE_META_DATA_HELPER(...) \
SERIALIZATION_DEFINE_META_DATA(SERIALIZATION_MACRO_WRAPPER(SERIALIZATION_CONCAT(SERIALIZATION_EXPAND, SERIALIZATION_NARG(__VA_ARGS__))(__VA_ARGS__)))

namespace serialization
{
namespace detail
{

template<typename ...Args>
constexpr size_t get_args_count(Args &&...)
{
return sizeof...(Args);
}

}
}

#define SERIALIZATION_MAKE_META(...) \
SERIALIZATION_MAKE_META_IMPL(SERIALIZATION_NARG(__VA_ARGS__), __VA_ARGS__)
7 changes: 5 additions & 2 deletions include/serialization/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#include <serialization/detail/json_iarchive.h>
#include <serialization/detail/json_oarchive.h>

#define SERIALIZATION_DEFINE(...) \
SERIALIZATION_MAKE_META(__VA_ARGS__) \
#define SERIALIZATION_DEFINE_META_FUNC(...) \
template<typename Archive> \
void serialize_impl(Archive & ar) const \
{ \
Expand All @@ -27,6 +26,10 @@ void unserialize_impl(const Archive & ar) \
serialization::unserialize_unpack(serialization_meta().begin(), ar, ##__VA_ARGS__);\
}

#define SERIALIZATION_DEFINE(...) \
SERIALIZATION_DEFINE_META_DATA_HELPER(__VA_ARGS__) \
SERIALIZATION_DEFINE_META_FUNC(__VA_ARGS__)

namespace serialization
{

Expand Down

0 comments on commit 25fce7c

Please sign in to comment.