Skip to content

Commit c7c7268

Browse files
author
me
committed
- added source_base::remaining()
- added value::pack() - started value::unpack()
1 parent c4d9ac9 commit c7c7268

File tree

5 files changed

+144
-75
lines changed

5 files changed

+144
-75
lines changed

src/msgpack.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,45 @@ namespace msgpackcpp
121121
return {static_cast<int>(ec), singleton};
122122
}
123123

124+
//----------------------------------------------------------------------------------------------------------------
125+
126+
void value::pack(sink_base& out, const value& jv)
127+
{
128+
std::visit(overloaded{
129+
[&](std::nullptr_t) {
130+
serialize(out, nullptr);
131+
},
132+
[&](const std::vector<value>& v) {
133+
serialize_array_size(out, v.size());
134+
for (const auto& el : v)
135+
value::pack(out, el);
136+
},
137+
[&](const std::map<std::string, value>& m) {
138+
serialize_map_size(out, m.size());
139+
for (const auto& [k,v] : m)
140+
{
141+
serialize(out, k);
142+
value::pack(out, v);
143+
}
144+
},
145+
[&](const auto& v) {
146+
serialize(out, v);
147+
}
148+
}, jv.val);
149+
}
150+
151+
// value value::unpack(source_base& in)
152+
// {
153+
// value jv;
154+
155+
// while (in.remaining())
156+
// {
157+
158+
// }
159+
160+
// return jv;
161+
// }
162+
124163
//----------------------------------------------------------------------------------------------------------------
125164

126165
}

src/msgpack.h

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,45 @@
2121
namespace msgpackcpp
2222
{
2323

24+
//----------------------------------------------------------------------------------------------------------------
25+
26+
struct sink_base
27+
{
28+
virtual void write(const char* data, size_t nbytes) = 0;
29+
};
30+
31+
struct source_base
32+
{
33+
virtual void read(char* buf, size_t nbytes) = 0;
34+
virtual uint8_t peak() = 0;
35+
virtual size_t remaining() const = 0;
36+
};
37+
38+
//----------------------------------------------------------------------------------------------------------------
39+
40+
enum deserialization_error
41+
{
42+
OUT_OF_DATA = 1,
43+
BAD_FORMAT = 2,
44+
BAD_SIZE = 3,
45+
BAD_NAME = 4
46+
};
47+
48+
std::error_code make_error_code(deserialization_error ec);
49+
50+
//----------------------------------------------------------------------------------------------------------------
51+
52+
}
53+
54+
namespace std
55+
{
56+
template <>
57+
struct is_error_code_enum<msgpackcpp::deserialization_error> : true_type {};
58+
}
59+
60+
namespace msgpackcpp
61+
{
62+
2463
//----------------------------------------------------------------------------------------------------------------
2564

2665
class value
@@ -92,48 +131,9 @@ namespace msgpackcpp
92131

93132
const value& operator[](size_t array_index) const;
94133
value& operator[](size_t array_index);
95-
};
96-
97-
//----------------------------------------------------------------------------------------------------------------
98134

99-
enum deserialization_error
100-
{
101-
OUT_OF_DATA = 1,
102-
BAD_FORMAT = 2,
103-
BAD_SIZE = 3,
104-
BAD_NAME = 4
105-
};
106-
107-
std::error_code make_error_code(deserialization_error ec);
108-
109-
//----------------------------------------------------------------------------------------------------------------
110-
111-
}
112-
113-
//----------------------------------------------------------------------------------------------------------------
114-
115-
namespace std
116-
{
117-
template <>
118-
struct is_error_code_enum<msgpackcpp::deserialization_error> : true_type {};
119-
}
120-
121-
//----------------------------------------------------------------------------------------------------------------
122-
123-
namespace msgpackcpp
124-
{
125-
126-
//----------------------------------------------------------------------------------------------------------------
127-
128-
struct sink_base
129-
{
130-
virtual void write(const char* data, size_t nbytes) = 0;
131-
};
132-
133-
struct source_base
134-
{
135-
virtual void read(char* buf, size_t nbytes) = 0;
136-
virtual uint8_t peak() = 0;
135+
static void pack(sink_base& out, const value& jv);
136+
static value unpack(source_base& in);
137137
};
138138

139139
//----------------------------------------------------------------------------------------------------------------
@@ -274,11 +274,6 @@ namespace msgpackcpp
274274
template<class... Args>
275275
void deserialize(source_base& in, std::tuple<Args...>& tpl);
276276

277-
//----------------------------------------------------------------------------------------------------------------
278-
279-
void serialize_from_value(sink_base& out, const value& jv);
280-
auto deserialize_to_value(source_base& in) -> value;
281-
282277
//----------------------------------------------------------------------------------------------------------------
283278
//----------------------------------------------------------------------------------------------------------------
284279
// DEFINITIONS

src/msgpack_sinks.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ namespace msgpackcpp
4646
{
4747
return static_cast<uint8_t>(data[offset]);
4848
}
49+
50+
size_t remaining() const override
51+
{
52+
return data.size() - offset;
53+
}
4954
};
5055

5156
template<class Byte, class Alloc, std::enable_if_t<is_byte<Byte>, bool> = true>
@@ -90,6 +95,15 @@ namespace msgpackcpp
9095
throw std::system_error(OUT_OF_DATA);
9196
return b;
9297
}
98+
99+
size_t remaining() const override
100+
{
101+
const auto pos = in.tellg();
102+
in.seekg(0, std::ios::end );
103+
const auto len = in.tellg() - pos;
104+
in.seekg( pos );
105+
return len;
106+
}
93107
};
94108

95109
inline auto sink(std::ostream& out) { return ostream_sink{out}; }

tests/pack.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ TEST_SUITE("[PACK]")
208208
REQUIRE(h == hh);
209209
REQUIRE(i == ii);
210210
REQUIRE(j == jj);
211+
REQUIRE(in.remaining() == 0);
211212
}
212213

213214
buf1.clear();
@@ -349,6 +350,7 @@ TEST_SUITE("[PACK]")
349350
REQUIRE(num_errors(w, ww) == 0);
350351
REQUIRE(num_errors(x, xx) == 0);
351352
REQUIRE(num_errors(y, yy) == 0);
353+
REQUIRE(in.remaining() == 0);
352354
}
353355
}
354356

tests/value.cpp

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
#include "doctest.h"
22
#include "msgpack.h"
3+
#include "msgpack_sinks.h"
34

45
using namespace std;
56
using namespace std::literals::string_view_literals;
67
using namespace msgpackcpp;
78

9+
value niels_data()
10+
{
11+
return {
12+
{"pi", 3.141},
13+
{"happy", true},
14+
{"name", "Niels"},
15+
{"nothing", nullptr},
16+
{"answer", {
17+
{"everything", 42}
18+
}},
19+
{"list", {1, 0, 2}},
20+
{"object", {
21+
{"currency", "USD"},
22+
{"value", 42.99}
23+
}}
24+
};
25+
}
26+
27+
void check_niels(value& jv)
28+
{
29+
REQUIRE(jv.is_object());
30+
REQUIRE(jv.size() == 7);
31+
REQUIRE(jv.at("pi").as_real() == 3.141);
32+
REQUIRE(jv.at("happy").as_bool() == true);
33+
REQUIRE(jv.at("name").as_str() == "Niels");
34+
REQUIRE(jv.at("nothing").is_null());
35+
REQUIRE(jv.at("answer").at("everything").as_int64() == 42);
36+
REQUIRE(jv.at("list").as_array().size() == 3);
37+
REQUIRE(jv.at("object").at("currency").as_str() == "USD");
38+
REQUIRE(jv.at("object").at("value").as_real() == 42.99);
39+
}
40+
841
TEST_SUITE("[VALUE]")
942
{
1043
TEST_CASE("basic")
@@ -74,35 +107,21 @@ TEST_SUITE("[VALUE]")
74107
REQUIRE(el.size() == 2);
75108
}
76109

77-
jv = {
78-
{"pi", 3.141},
79-
{"happy", true},
80-
{"name", "Niels"},
81-
{"nothing", nullptr},
82-
{"answer", {
83-
{"everything", 42}
84-
}},
85-
{"list", {1, 0, 2}},
86-
{"object", {
87-
{"currency", "USD"},
88-
{"value", 42.99}
89-
}}
90-
};
91-
92-
const auto check_niels = [](value& jv)
93-
{
94-
REQUIRE(jv.is_object());
95-
REQUIRE(jv.size() == 7);
96-
REQUIRE(jv.at("pi").as_real() == 3.141);
97-
REQUIRE(jv.at("happy").as_bool() == true);
98-
REQUIRE(jv.at("name").as_str() == "Niels");
99-
REQUIRE(jv.at("nothing").is_null());
100-
REQUIRE(jv.at("answer").at("everything").as_int64() == 42);
101-
REQUIRE(jv.at("list").as_array().size() == 3);
102-
REQUIRE(jv.at("object").at("currency").as_str() == "USD");
103-
REQUIRE(jv.at("object").at("value").as_real() == 42.99);
104-
};
105-
110+
jv = niels_data();
106111
check_niels(jv);
107112
}
113+
114+
TEST_CASE("serialize")
115+
{
116+
value jv1 = niels_data();
117+
check_niels(jv1);
118+
119+
std::vector<char> buf0;
120+
auto out0 = sink(buf0);
121+
value::pack(out0, jv1);
122+
// auto in0 = source(buf0);
123+
124+
// value jv2 = value::unpack(in0);
125+
// check_niels(jv2);
126+
}
108127
}

0 commit comments

Comments
 (0)