File: map_based_versionup.cpp

package info (click to toggle)
msgpack-cxx 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,520 kB
  • sloc: cpp: 87,413; ansic: 3,571; sh: 56; makefile: 39
file content (112 lines) | stat: -rw-r--r-- 2,751 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
//    Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//    http://www.boost.org/LICENSE_1_0.txt)
//

#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>

#include <msgpack.hpp>

struct base1 {
    base1():a("default") {}
    std::string a;
    MSGPACK_DEFINE_MAP(a);
};

struct v1 : base1 {
    v1():name("default"), age(0) {}
    std::string name;
    int age;
    MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base1), name, age);
};

struct base2 {
    base2():a("default") {}
    std::string a;
    MSGPACK_DEFINE_MAP(a);
};

// Removed: base1, name
// Added  : base2, address
struct v2 : base2 {
    v2(): age(0), address("default") {}
    int age;
    std::string address;
    MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base2), age, address);
};

// The member variable "age" is in common between v1 and v2.

void print(std::string const& buf) {
    for (std::string::const_iterator it = buf.begin(), end = buf.end();
         it != end;
         ++it) {
        std::cout
            << std::setw(2)
            << std::hex
            << std::setfill('0')
            << (static_cast<int>(*it) & 0xff)
            << ' ';
    }
    std::cout << std::dec << std::endl;
}

int main() {
    { // pack v1, unpack, convert to v2
        v1 v;
        v.a = "ABC";
        v.name = "John Smith";
        v.age = 35;

        std::stringstream ss;
        msgpack::pack(ss, v);

        print(ss.str());

        std::string const& str = ss.str();
        msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());

        msgpack::object obj = oh.get();
        std::cout << obj << std::endl;

        v2 newv = obj.as<v2>();

        std::cout << "v2::a       " << newv.a << std::endl;
        std::cout << "v2::age     " << newv.age << std::endl;
        std::cout << "v2::address " << newv.address << std::endl;

        // "age" is set from v1
        assert(newv.a == "default");
        assert(newv.age == 35);
        assert(newv.address == "default");
    }
    { // create v2 object with zone, convert to v1
        v2 v;
        v.a = "DEF";
        v.age = 42;
        v.address = "Tokyo";

        msgpack::zone z;
        msgpack::object obj(v, z);
        std::cout << obj << std::endl;

        v1 newv = obj.as<v1>();

        std::cout << "v1::a       " << newv.a << std::endl;
        std::cout << "v1::name    " << newv.name << std::endl;
        std::cout << "v1::age     " << newv.age << std::endl;

        // "age" is set from v2
        assert(newv.a == "default");
        assert(newv.name == "default");
        assert(newv.age == 42);
    }
}