28 explicit StateWriter(std::span<std::byte> buf) noexcept : buf_(buf) {}
31 requires std::is_trivially_copyable_v<T>
32 void put(
const T& v)
noexcept {
37 requires std::is_trivially_copyable_v<T>
38 void put_array(std::span<const T> v)
noexcept {
39 put(
static_cast<std::uint32_t
>(v.size()));
42 void put_bytes(
const void* p, std::size_t n)
noexcept {
43 if (!ok_ || n > buf_.size() - n_) {
47 if (n != 0) std::memcpy(buf_.data() + n_, p, n);
51 [[nodiscard]]
bool ok() const noexcept {
return ok_; }
52 [[nodiscard]] std::size_t
size() const noexcept {
return n_; }
53 [[nodiscard]] std::size_t
capacity() const noexcept {
return buf_.size(); }
55 [[nodiscard]] std::string_view
view() const noexcept {
57 return {
reinterpret_cast<const char*
>(buf_.data()), n_};
61 std::span<std::byte> buf_;
68 explicit StateReader(std::string_view bytes) noexcept : bytes_(bytes) {}
71 requires std::is_trivially_copyable_v<T>
72 [[nodiscard]]
bool get(T& v)
noexcept {
78 requires std::is_trivially_copyable_v<T>
79 [[nodiscard]]
bool get_array(T* out, std::size_t cap, std::size_t& n)
noexcept {
80 std::uint32_t count = 0;
81 if (!
get(count) || count > cap || !
get_bytes(out, std::size_t{count} *
sizeof(T))) {
88 [[nodiscard]]
bool get_bytes(
void* p, std::size_t n)
noexcept {
89 if (!ok_ || n > bytes_.size() - at_) {
93 if (n != 0) std::memcpy(p, bytes_.data() + at_, n);
98 [[nodiscard]]
bool ok() const noexcept {
return ok_; }
100 [[nodiscard]]
bool done() const noexcept {
return ok_ && at_ == bytes_.size(); }
103 std::string_view bytes_;
Definition state_codec.hpp:68
bool get_array(T *out, std::size_t cap, std::size_t &n) noexcept
What put_array wrote, into out[0, cap): false when it holds more than cap elements.
Definition state_codec.hpp:81
bool done() const noexcept
Every byte was read, and every read fitted.
Definition state_codec.hpp:102
bool get(T &v) noexcept
Definition state_codec.hpp:74
bool ok() const noexcept
Definition state_codec.hpp:100
bool get_bytes(void *p, std::size_t n) noexcept
Definition state_codec.hpp:90
void put(const T &v) noexcept
Definition state_codec.hpp:34
bool ok() const noexcept
Definition state_codec.hpp:53
void put_bytes(const void *p, std::size_t n) noexcept
Definition state_codec.hpp:44
std::string_view view() const noexcept
What was written; empty after a write that did not fit.
Definition state_codec.hpp:57
void put_array(std::span< const T > v) noexcept
A count (u32) and the elements.
Definition state_codec.hpp:40
std::size_t capacity() const noexcept
Definition state_codec.hpp:55
std::size_t size() const noexcept
Definition state_codec.hpp:54
Definition strategy.hpp:23