1+ #pragma once
2+
3+ #include < cstring>
4+ #include < vector>
5+ #include < ostream>
6+ #include < istream>
7+ #include " msgpack.h"
8+
9+ namespace msgpackcpp
10+ {
11+
12+ // ----------------------------------------------------------------------------------------------------------------
13+
14+ template <class Byte , class Alloc , check_byte<Byte> = true >
15+ auto sink (std::vector<Byte, Alloc>& buf)
16+ {
17+ return [&](const char * bytes, size_t nbytes) {
18+ buf.insert (end (buf), bytes, bytes + nbytes);
19+ };
20+ }
21+
22+ template <class Byte , class Alloc , check_byte<Byte> = true >
23+ auto source (const std::vector<Byte, Alloc>& buf)
24+ {
25+ size_t offset{0 };
26+ return [&buf, offset](char * bytes, size_t nbytes) mutable {
27+ if ((buf.size () - offset) < nbytes)
28+ throw std::system_error (OUT_OF_DATA);
29+ std::memcpy (bytes, buf.data () + offset, nbytes);
30+ offset += nbytes;
31+ };
32+ }
33+
34+ // ----------------------------------------------------------------------------------------------------------------
35+
36+ inline auto sink (std::ostream& out)
37+ {
38+ return [&](const char * bytes, size_t nbytes) {
39+ out.write (bytes, nbytes);
40+ };
41+ }
42+
43+ inline auto source (std::istream& in)
44+ {
45+ return [&](char * bytes, size_t nbytes) {
46+ in.read (bytes, nbytes);
47+ if (in.gcount () != (long )nbytes)
48+ throw std::system_error (OUT_OF_DATA);
49+ };
50+ }
51+
52+ // ----------------------------------------------------------------------------------------------------------------
53+
54+ }
0 commit comments