-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpacker.hpp
67 lines (53 loc) · 1.08 KB
/
packer.hpp
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
#ifndef _TSD_PACKER_HPP_
#define _TSD_PACKER_HPP_
#include <ostream>
namespace tsd
{
class packer
{
public:
packer(std::ostream& ost) :
ost_(ost)
{}
void pack8(uint8_t d) {
pack(d);
}
void pack16(uint16_t d) {
pack(d);
}
void pack32(uint32_t d) {
pack(d);
}
void pack64(uint64_t d) {
pack(d);
}
void pack(uint8_t d) {
ost_.write(reinterpret_cast<const char*>(&d), 1);
}
void pack(uint16_t d) {
pack(static_cast<uint8_t>(d >> 8));
pack(static_cast<uint8_t>(d & 0xFF));
}
void pack(uint32_t d) {
pack(static_cast<uint16_t>(d >> 16));
pack(static_cast<uint16_t>(d & 0xFFFF));
}
void pack(uint64_t d) {
pack(static_cast<uint32_t>(d >> 32));
pack(static_cast<uint32_t>(d & 0xFFFFFFFF));
}
void pack_bytes(const char* data, size_t size) {
ost_.write(data, size);
}
void pack_bytes(const uint8_t* data, size_t size) {
pack_bytes(reinterpret_cast<const char*>(data), size);
}
void pack_fill(size_t n, uint8_t d) {
for(; n > 0; --n)
pack(d);
}
private:
std::ostream& ost_;
};
}
#endif