-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtraits.hpp
More file actions
144 lines (112 loc) · 3.71 KB
/
Copy pathtraits.hpp
File metadata and controls
144 lines (112 loc) · 3.71 KB
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
#ifndef COCAINE_DYNAMIC_TYPE_TRAITS_HPP
#define COCAINE_DYNAMIC_TYPE_TRAITS_HPP
#include <cocaine/traits.hpp>
#include "dynamic.hpp"
namespace cocaine { namespace io {
template<>
struct type_traits<dynamic_t>
{
template<class Stream>
struct pack_visitor :
public boost::static_visitor<>
{
pack_visitor(msgpack::packer<Stream>& packer) :
m_packer(packer)
{
// pass
}
void
operator()(const dynamic_t::null_t&) const {
m_packer << msgpack::type::nil();
}
void
operator()(const dynamic_t::bool_t& v) const {
m_packer << v;
}
void
operator()(const dynamic_t::int_t& v) const {
m_packer << v;
}
void
operator()(const dynamic_t::double_t& v) const {
m_packer << v;
}
void
operator()(const dynamic_t::string_t& v) const {
m_packer << v;
}
void
operator()(const dynamic_t::array_t& v) const {
m_packer.pack_array(v.size());
for(size_t i = 0; i < v.size(); ++i) {
v[i].apply(*this);
}
}
void
operator()(const dynamic_t::object_t& v) const {
m_packer.pack_map(v.size());
for(auto it = v.begin(); it != v.end(); ++it) {
m_packer << it->first;
it->second.apply(*this);
}
}
private:
msgpack::packer<Stream>& m_packer;
};
template<class Stream>
static inline
void
pack(msgpack::packer<Stream>& packer, const dynamic_t& source) {
source.apply(pack_visitor<Stream>(packer));
}
static inline
void
unpack(const msgpack::object& object, dynamic_t& target) {
switch(object.type) {
case msgpack::type::MAP: {
dynamic_t::object_t container;
msgpack::object_kv *ptr = object.via.map.ptr,
*const end = ptr + object.via.map.size;
for(; ptr < end; ++ptr) {
if(ptr->key.type != msgpack::type::RAW) {
// NOTE: The keys should be strings.
throw msgpack::type_error();
}
unpack(ptr->val, container[ptr->key.as<std::string>()]);
}
target = std::move(container);
} break;
case msgpack::type::ARRAY: {
dynamic_t::array_t container;
container.reserve(object.via.array.size);
msgpack::object *ptr = object.via.array.ptr,
*const end = ptr + object.via.array.size;
for(unsigned int index = 0; ptr < end; ++ptr, ++index) {
container.push_back(dynamic_t());
unpack(*ptr, container.back());
}
target = std::move(container);
} break;
case msgpack::type::RAW: {
target = object.as<std::string>();
} break;
case msgpack::type::DOUBLE: {
target = object.as<double>();
} break;
case msgpack::type::POSITIVE_INTEGER: {
target = object.as<uint64_t>();
} break;
case msgpack::type::NEGATIVE_INTEGER: {
target = object.as<int64_t>();
} break;
case msgpack::type::BOOLEAN: {
target = object.as<bool>();
} break;
case msgpack::type::NIL: {
target = dynamic_t::null_t();
}
}
}
};
}} // namespace cocaine::io
#endif // COCAINE_DYNAMIC_TYPE_TRAITS_HPP