File: container.cpp

package info (click to toggle)
msgpack-cxx 7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,520 kB
  • sloc: cpp: 87,413; ansic: 3,571; sh: 56; makefile: 39
file content (159 lines) | stat: -rw-r--r-- 4,190 bytes parent folder | download | duplicates (3)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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 <iostream>
#include <sstream>
#include <cassert>

#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <forward_list>
#include <string>

#include <msgpack.hpp>

void array() {
    std::array<int, 5> a { { 1, 2, 3, 4, 5 } };
    std::stringstream ss;
    msgpack::pack(ss, a);

    auto const& str = ss.str();
    auto oh = msgpack::unpack(str.data(), str.size());
    auto obj = oh.get();

    std::cout << obj << std::endl;
    assert((obj.as<std::array<int, 5>>()) == a);
}

void tuple() {
    std::tuple<bool, std::string, int> t(true, "ABC", 42);
    std::stringstream ss;
    msgpack::pack(ss, t);

    auto const& str = ss.str();
    auto oh = msgpack::unpack(str.data(), str.size());
    auto obj = oh.get();

    std::cout << obj << std::endl;
    assert(obj.as<decltype(t)>() == t);
}

void unordered_map() {
    std::unordered_map<std::string, int> m { {"ABC", 1}, {"DEF", 3} };
    std::stringstream ss;
    msgpack::pack(ss, m);

    auto const& str = ss.str();
    auto oh = msgpack::unpack(str.data(), str.size());
    msgpack::object obj = oh.get();

    std::cout << obj << std::endl;
    assert(obj.as<decltype(m)>() == m);
}

void unordered_set() {
    std::unordered_set<std::string> s { "ABC", "DEF" };
    std::stringstream ss;
    msgpack::pack(ss, s);

    auto const& str = ss.str();
    auto oh = msgpack::unpack(str.data(), str.size());
    auto obj = oh.get();

    std::cout << obj << std::endl;
    assert(obj.as<decltype(s)>() == s);
}

void forward_list() {
    using type = std::forward_list<std::string>;
    type f { "ABC", "DEF" };
    std::stringstream ss;
    msgpack::pack(ss, f);

    auto const& str = ss.str();
    auto oh = msgpack::unpack(str.data(), str.size());
    auto obj = oh.get();

    std::cout << obj << std::endl;
    assert(obj.as<type>() == f);
}

void combi() {
    std::array<int, 5>                   a { { 1, 2, 3, 4, 5 } };
    std::tuple<bool, std::string, int>   t {true, "ABC", 42};
    std::unordered_map<std::string, int> m { {"ABC", 1}, {"DEF", 3} };
    std::unordered_set<std::string>      s { "ABC", "DEF" };
    std::forward_list<std::string>       f { "ABC", "DEF" };

    std::stringstream ss;
    msgpack::pack(ss, a);
    msgpack::pack(ss, t);
    msgpack::pack(ss, m);
    msgpack::pack(ss, s);
    msgpack::pack(ss, f);

    std::size_t offset = 0;
    std::cout << "offset: " << offset << std::endl;
    {
        auto const& str = ss.str();
        auto oh = msgpack::unpack(str.data(), str.size(), offset);
        auto obj = oh.get();

        std::cout << obj << std::endl;
        assert(obj.as<decltype(a)>() == a);
    }
    std::cout << "offset: " << offset << std::endl;
    {
        auto const& str = ss.str();
        auto oh = msgpack::unpack(str.data(), str.size(), offset);
        auto obj = oh.get();

        std::cout << obj << std::endl;
        assert(obj.as<decltype(t)>() == t);
    }
    std::cout << "offset: " << offset << std::endl;
    {
        auto const& str = ss.str();
        auto oh = msgpack::unpack(str.data(), str.size(), offset);
        auto obj = oh.get();

        std::cout << obj << std::endl;
        assert(obj.as<decltype(m)>() == m);
    }
    std::cout << "offset: " << offset << std::endl;
    {
        auto const& str = ss.str();
        auto oh = msgpack::unpack(str.data(), str.size(), offset);
        auto obj = oh.get();

        std::cout << obj << std::endl;
        assert(obj.as<decltype(s)>() == s);
    }
    std::cout << "offset: " << offset << std::endl;
    {
        auto const& str = ss.str();
        auto oh = msgpack::unpack(str.data(), str.size(), offset);
        auto obj = oh.get();

        std::cout << obj << std::endl;
        assert(obj.as<decltype(f)>() == f);
    }
    std::cout << "offset: " << offset << std::endl;
}

int main() {
    array();
    tuple();
    unordered_map();
    unordered_set();
    forward_list();
    combi();
}