16namespace wmtk::history::detail {
19template <
typename Put>
20void put_varint(uint64_t n, Put&& put)
31template <
typename Get>
32bool get_varint(Get&& get, uint64_t& n)
35 for (
int shift = 0; shift < 64; shift += 7) {
36 const uint8_t b = get();
37 n |= uint64_t(b & 0x7F) << shift;
38 if (!(b & 0x80))
return true;
45inline uint64_t zigzag(
const int64_t x)
47 return (uint64_t(x) << 1) ^ uint64_t(x >> 63);
50inline int64_t unzigzag(
const uint64_t x)
52 return int64_t(x >> 1) ^ -int64_t(x & 1);
59 explicit Writer(std::vector<uint8_t>& out)
65 const auto* p =
reinterpret_cast<const uint8_t*
>(&x);
66 m_out.insert(m_out.end(), p, p +
sizeof(T));
68 void byte(
const uint8_t b) { m_out.push_back(b); }
69 void varint(
const uint64_t n)
71 put_varint(n, [
this](
const uint8_t b) { m_out.push_back(b); });
73 void position(
const Eigen::Vector3d& p)
75 const size_t at = m_out.size();
76 m_out.resize(at + 3 *
sizeof(
double));
77 std::memcpy(m_out.data() + at, p.data(), 3 *
sizeof(
double));
79 void bytes(
const char* p,
const size_t n)
83 reinterpret_cast<const uint8_t*
>(p),
84 reinterpret_cast<const uint8_t*
>(p) + n);
88 std::vector<uint8_t>& m_out;
99 Reader(
const uint8_t* p,
const uint8_t* end, std::string what, std::string name)
102 , m_what(std::move(what))
103 , m_name(std::move(name))
106 Reader(
const std::vector<uint8_t>& in, std::string name)
107 :
Reader(in.data(), in.data() + in.size(),
"history", std::move(name))
110 uint64_t
remaining()
const {
return uint64_t(m_end - m_p); }
111 const uint8_t* at()
const {
return m_p; }
114 uint64_t
count(
const uint64_t min_bytes = 1)
116 const uint64_t n = varint();
117 if (n >
remaining() / min_bytes) corrupt();
120 [[noreturn]]
void corrupt()
const { log_and_throw_error(
"{}: {} is corrupt", m_what, m_name); }
121 template <
typename T>
125 bytes(
reinterpret_cast<char*
>(&x),
sizeof(T));
128 uint8_t byte() {
return get<uint8_t>(); }
132 if (!get_varint([
this]() {
return byte(); }, n)) {
133 log_and_throw_error(
"{}: {} holds a malformed integer", m_what, m_name);
137 Eigen::Vector3d position()
140 for (
int k = 0; k < 3; ++k) x[k] = get<double>();
143 void bytes(
char* p,
const size_t n)
145 if (n >
remaining()) log_and_throw_error(
"{}: {} is truncated", m_what, m_name);
146 std::memcpy(p, m_p, n);
149 std::string string(
const uint64_t n)
151 if (n >
remaining()) log_and_throw_error(
"{}: {} is truncated", m_what, m_name);
152 std::string s(
reinterpret_cast<const char*
>(m_p),
size_t(n));
156 const std::string& name()
const {
return m_name; }
160 const uint8_t* m_end;
161 std::string m_what, m_name;
Reads the bytes of a history, [p, end). what and name say what they are in messages,...
Definition BinaryIO.hpp:97
uint64_t count(const uint64_t min_bytes=1)
Definition BinaryIO.hpp:114
uint64_t remaining() const
The bytes not read yet, and where they start.
Definition BinaryIO.hpp:110
Reader(const std::vector< uint8_t > &in, std::string name)
All of in, the bytes of a surface's or curves' history.
Definition BinaryIO.hpp:106
Appends to the bytes of a history, as a file holds them.
Definition BinaryIO.hpp:57