FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
state_codec.hpp
Go to the documentation of this file.
1#pragma once
2// StateWriter and StateReader: the bytes of a strategy's state() and restore() without allocating
3// or throwing. The writer fills a buffer the strategy owns and sizes once, so a checkpoint costs a
4// copy of the state and nothing else; a write that does not fit fails the writer instead of
5// growing it. Values are copied in the machine's own layout: trivially copyable types only, read
6// back in the order they were written. The strategy's state_version (strategies/hooks.hpp) names
7// the layout.
8//
9// StateWriter w(buf_); // std::span<std::byte> sized in on_start
10// w.put(avg_);
11// w.put_array(std::span<const Slot>(slots_.data(), n_));
12// return w.view(); // empty when the buffer was too small
13//
14// StateReader r(bytes);
15// return r.get(avg_) && r.get_array(slots_.data(), slots_.size(), n_) && r.done();
16#include <cstddef>
17#include <cstdint>
18#include <cstring>
19#include <span>
20#include <string_view>
21#include <type_traits>
22
23namespace fastmm {
24
25class StateWriter {
26 public:
27 StateWriter() = default;
28 explicit StateWriter(std::span<std::byte> buf) noexcept : buf_(buf) {}
30 template <class T>
31 requires std::is_trivially_copyable_v<T>
32 void put(const T& v) noexcept {
33 put_bytes(&v, sizeof(T));
34 }
35 // A count (u32) and the elements.
36 template <class T>
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()));
40 put_bytes(v.data(), v.size_bytes());
41 }
42 void put_bytes(const void* p, std::size_t n) noexcept {
43 if (!ok_ || n > buf_.size() - n_) {
44 ok_ = false;
45 return;
46 }
47 if (n != 0) std::memcpy(buf_.data() + n_, p, n);
48 n_ += n;
49 }
50
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(); }
54 // What was written; empty after a write that did not fit.
55 [[nodiscard]] std::string_view view() const noexcept {
56 if (!ok_) return {};
57 return {reinterpret_cast<const char*>(buf_.data()), n_};
58 }
59
60 private:
61 std::span<std::byte> buf_;
62 std::size_t n_ = 0;
63 bool ok_ = true;
64};
65
66class StateReader {
67 public:
68 explicit StateReader(std::string_view bytes) noexcept : bytes_(bytes) {}
69
70 template <class T>
71 requires std::is_trivially_copyable_v<T>
72 [[nodiscard]] bool get(T& v) noexcept {
73 return get_bytes(&v, sizeof(T));
74 }
75 // What put_array wrote, into out[0, cap): false when it holds more than cap elements. `n` is
76 // the count read.
77 template <class T>
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))) {
82 ok_ = false;
83 return false;
84 }
85 n = count;
86 return true;
87 }
88 [[nodiscard]] bool get_bytes(void* p, std::size_t n) noexcept {
89 if (!ok_ || n > bytes_.size() - at_) {
90 ok_ = false;
91 return false;
92 }
93 if (n != 0) std::memcpy(p, bytes_.data() + at_, n);
94 at_ += n;
95 return true;
96 }
97
98 [[nodiscard]] bool ok() const noexcept { return ok_; }
99 // Every byte was read, and every read fitted.
100 [[nodiscard]] bool done() const noexcept { return ok_ && at_ == bytes_.size(); }
101
102 private:
103 std::string_view bytes_;
104 std::size_t at_ = 0;
105 bool ok_ = true;
106};
107
108} // namespace fastmm
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