-
Notifications
You must be signed in to change notification settings - Fork 1
/
Writer.cpp
218 lines (173 loc) · 6.04 KB
/
Writer.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Stéphane Caron
/*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* MessagePackBuilder from https://github.com/jrl-umi3218/mc_rtc/
* Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "palimpsest/mpack/Writer.h"
#include <mpack.h>
#include <string>
#include <vector>
#if not EIGEN_VERSION_AT_LEAST(3, 2, 90)
namespace Eigen {
using Index = Eigen::DenseIndex;
}
#endif
/*! Intrusive flush function for std::vector.
*
* This is an intrusive flush function which modifies the writer's buffer in
* response to a flush instead of emptying it in order to add more capacity for
* data. This removes the need to copy data from a fixed buffer into a growable
* one, improving performance.
*
* There are three ways flush can be called:
* - flushing the buffer during writing (used is zero, count is all data, data
* is buffer)
* - flushing extra data during writing (used is all flushed data, count is
* extra data, data is not buffer)
* - flushing during teardown (used and count are both all flushed data, data
* is buffer)
*
* In the first two cases, we grow the buffer by at least double, enough
* to ensure that new data will fit. We ignore the teardown flush.
*
* @note Inspired by mpack.c @ version 1.0
*/
static void mpack_std_vector_writer_flush(mpack_writer_t *writer,
const char *data, size_t count) {
auto &buffer = *static_cast<std::vector<char> *>(writer->context);
if (data == writer->buffer) {
if (mpack_writer_buffer_used(writer) == count) {
// teardown, do nothing
return;
}
// otherwise leave the data in the buffer and just grow
writer->current = writer->buffer + count;
count = 0;
}
size_t used = mpack_writer_buffer_used(writer);
size_t size = mpack_writer_buffer_size(writer);
mpack_log("flush size %i used %i data %p buffer %p\n",
static_cast<int>(count), static_cast<int>(used), data,
writer->buffer);
mpack_assert(
data == writer->buffer || used + count > size,
"extra flush for %i but there is %i space left in the buffer! (%i/%i)",
static_cast<int>(count),
static_cast<int>(mpack_writer_buffer_left(writer)),
static_cast<int>(used), static_cast<int>(size));
// grow to fit the data
// TODO(gergondet): this really needs to correctly test for overflow
size_t new_size = size * 2;
while (new_size < used + count) {
new_size *= 2;
}
mpack_log("flush growing buffer size from %i to %i\n", static_cast<int>(size),
static_cast<int>(new_size));
// grow the buffer
buffer.resize(new_size);
char *new_buffer = buffer.data();
if (new_buffer == NULL) {
mpack_writer_flag_error(writer, mpack_error_memory);
return;
}
writer->current = new_buffer + used;
writer->buffer = new_buffer;
writer->end = writer->buffer + new_size;
// append the extra data
if (count > 0) {
mpack_memcpy(writer->current, data, count);
writer->current += count;
}
mpack_log("new buffer %p, used %i\n", new_buffer,
static_cast<int>(mpack_writer_buffer_used(writer)));
}
namespace palimpsest::mpack {
Writer::Writer(std::vector<char> &buffer) {
if (buffer.size() == 0) {
buffer.resize(MPACK_BUFFER_SIZE);
}
mpack_writer_init(&writer_, buffer.data(), buffer.size());
mpack_writer_set_context(&writer_, &buffer); // used in flush function
mpack_writer_set_flush(&writer_, mpack_std_vector_writer_flush);
}
Writer::~Writer() {}
void Writer::write(bool b) { mpack_write_bool(&writer_, b); }
void Writer::write(int8_t i) { mpack_write_i8(&writer_, i); }
void Writer::write(int16_t i) { mpack_write_i16(&writer_, i); }
void Writer::write(int32_t i) { mpack_write_i32(&writer_, i); }
void Writer::write(int64_t i) { mpack_write_i64(&writer_, i); }
void Writer::write(uint8_t i) { mpack_write_u8(&writer_, i); }
void Writer::write(uint16_t i) { mpack_write_u16(&writer_, i); }
void Writer::write(uint32_t i) { mpack_write_u32(&writer_, i); }
void Writer::write(uint64_t i) { mpack_write_u64(&writer_, i); }
void Writer::write(float f) { mpack_write_float(&writer_, f); }
void Writer::write(double d) { mpack_write_double(&writer_, d); }
void Writer::write(const std::string &s) {
mpack_write_str(&writer_, s.c_str(), static_cast<uint32_t>(s.size()));
}
void Writer::write(const char *s) { mpack_write_cstr(&writer_, s); }
namespace {
template <typename T>
inline void write_vector(mpack_writer_t *writer, const T &v) {
for (Eigen::Index i = 0; i < v.size(); ++i) {
mpack_write_double(writer, v(i));
}
}
template <typename T>
inline void write_matrix(mpack_writer_t *writer, const T &m) {
for (Eigen::Index i = 0; i < m.rows(); ++i) {
for (Eigen::Index j = 0; j < m.cols(); ++j) {
mpack_write_double(writer, m(i, j));
}
}
}
} // namespace
void Writer::write(const Eigen::Vector2d &v) {
start_array(2);
write_vector(&writer_, v);
finish_array();
}
void Writer::write(const Eigen::Vector3d &v) {
start_array(3);
write_vector(&writer_, v);
finish_array();
}
void Writer::write(const Eigen::VectorXd &v) {
start_array(static_cast<size_t>(v.size()));
write_vector(&writer_, v);
finish_array();
}
void Writer::write(const Eigen::Quaterniond &q) {
start_array(4);
write(q.w());
write(q.x());
write(q.y());
write(q.z());
finish_array();
}
void Writer::write(const Eigen::Matrix3d &m) {
start_array(9);
write_matrix(&writer_, m);
finish_array();
}
void Writer::start_array(size_t s) {
mpack_start_array(&writer_, static_cast<uint32_t>(s));
}
void Writer::finish_array() { mpack_finish_array(&writer_); }
void Writer::start_map(size_t s) {
mpack_start_map(&writer_, static_cast<uint32_t>(s));
}
void Writer::finish_map() { mpack_finish_map(&writer_); }
size_t Writer::finish() {
if (mpack_writer_destroy(&writer_) != mpack_ok) {
mpack_log("Failed to write to MessagePack");
return 0;
}
return mpack_writer_buffer_used(&writer_);
}
} // namespace palimpsest::mpack