2121#include < sstream>
2222#include < cassert>
2323
24+ #if 0 // When you want to adapt map instead of array, you can enable these macro definition.
25+ #define MSGPACK_USE_DEFINE_MAP
26+ #define MSGPACK_USE_BOOST
27+ #endif
28+
2429#include < msgpack.hpp>
2530
31+ struct my_base1 {
32+ int a;
33+ MSGPACK_DEFINE (a);
34+ };
35+ inline bool operator ==(my_base1 const & lhs, my_base1 const & rhs) {
36+ return lhs.a == rhs.a ;
37+ }
2638
27- class my_class {
39+ struct my_base2 {
40+ std::string b;
41+ std::string c;
42+ MSGPACK_DEFINE (b, c);
43+ };
44+ inline bool operator ==(my_base2 const & lhs, my_base2 const & rhs) {
45+ return lhs.b == rhs.b && lhs.c == rhs.c ;
46+ }
47+
48+ class my_class : public my_base1 , private my_base2 {
2849public:
2950 my_class () {} // When you want to convert from msgpack::object to my_class,
3051 // my_class should be default constractible.
3152 my_class (std::string const & name, int age):name_(name), age_(age) {}
32-
53+ void set_b (std::string const & str) { b = str; }
54+ void set_c (std::string const & str) { c = str; }
3355 friend bool operator ==(my_class const & lhs, my_class const & rhs) {
34- return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_ ;
56+ return
57+ static_cast <my_base1 const &>(lhs) == static_cast <my_base1 const &>(rhs) &&
58+ static_cast <my_base2 const &>(lhs) == static_cast <my_base2 const &>(rhs) &&
59+ lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_ ;
3560 }
3661
3762private:
3863 std::string name_;
3964 int age_;
4065
4166public:
42- MSGPACK_DEFINE (name_, age_);
67+ MSGPACK_DEFINE (name_, age_, MSGPACK_BASE(my_base1), MSGPACK_BASE(my_base2) );
4368};
4469
4570void print (std::string const & buf) {
@@ -59,6 +84,9 @@ void print(std::string const& buf) {
5984int main () {
6085 { // pack, unpack
6186 my_class my (" John Smith" , 42 );
87+ my.a = 123 ;
88+ my.set_b (" ABC" );
89+ my.set_c (" DEF" );
6290 std::stringstream ss;
6391 msgpack::pack (ss, my);
6492
@@ -72,6 +100,8 @@ int main() {
72100 }
73101 { // create object with zone
74102 my_class my (" John Smith" , 42 );
103+ my.set_b (" ABC" );
104+ my.set_c (" DEF" );
75105 msgpack::zone z;
76106 msgpack::object obj (my, z);
77107 std::cout << obj << std::endl;
0 commit comments