Skip to content

Commit 039347f

Browse files
author
me
committed
moved back to templated functors. everything is header only again
1 parent b0fa974 commit 039347f

File tree

8 files changed

+623
-588
lines changed

8 files changed

+623
-588
lines changed

src/msgpack.h renamed to include/msgpack.h

Lines changed: 540 additions & 246 deletions
Large diffs are not rendered by default.

include/msgpack_sinks.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

src/msgpack.cpp

Lines changed: 0 additions & 238 deletions
This file was deleted.

src/msgpack_sinks.h

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)