Skip to content

Commit 29bc691

Browse files
author
me
committed
iostream sinks/sources
1 parent 4712076 commit 29bc691

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/msgpack.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ 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+
277282
//----------------------------------------------------------------------------------------------------------------
278283
//----------------------------------------------------------------------------------------------------------------
279284
// DEFINITIONS

src/msgpack_sinks.h

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,39 @@ namespace msgpackcpp
6262

6363
//----------------------------------------------------------------------------------------------------------------
6464

65-
// inline auto sink(std::ostream& out)
66-
// {
67-
// return [&](const char* bytes, size_t nbytes) {
68-
// out.write(bytes, nbytes);
69-
// };
70-
// }
71-
72-
// inline auto source(std::istream& in)
73-
// {
74-
// return [&](char* bytes, size_t nbytes) {
75-
// in.read(bytes, nbytes);
76-
// if (in.gcount() != (long)nbytes)
77-
// throw std::system_error(OUT_OF_DATA);
78-
// };
79-
// }
65+
struct ostream_sink : sink_base
66+
{
67+
std::ostream& out;
68+
69+
ostream_sink(std::ostream& out_) : out{out_}{}
70+
void write(const char* data, size_t nbytes) override {out.write(data, nbytes);}
71+
};
72+
73+
struct istream_source : source_base
74+
{
75+
std::istream& in;
76+
77+
istream_source(std::istream& in_) : in{in_}{}
78+
79+
void read(char* buf, size_t nbytes) override
80+
{
81+
in.read(buf, nbytes);
82+
if (in.gcount() != (long)nbytes)
83+
throw std::system_error(OUT_OF_DATA);
84+
}
85+
86+
uint8_t peak() override
87+
{
88+
uint8_t b = static_cast<uint8_t>(in.peek());
89+
if (!in || !in.good() || in.eof())
90+
throw std::system_error(OUT_OF_DATA);
91+
return b;
92+
}
93+
};
94+
95+
inline auto sink(std::ostream& out) { return ostream_sink{out}; }
96+
inline auto source(std::istream& in) { return istream_source{in}; }
97+
98+
//----------------------------------------------------------------------------------------------------------------
99+
80100
}

0 commit comments

Comments
 (0)