25 const std::array<char, IDENTITY_SIZE>& identity, std::string_view identity_text,
size_t slot_size,
26 size_t max_slots,
const void* fresh_state,
size_t state_size)
30 , slot_size_(slot_size)
32 auto* header =
reinterpret_cast<FileHeader*
>(file_.data());
34 const bool blank = std::all_of(file_.data(), file_.data() +
HEADER_SIZE, [](uint8_t b) { return b == 0; });
39 fresh.slot_size =
static_cast<uint32_t
>(slot_size);
41 std::memcpy(fresh.state, fresh_state, state_size);
42 std::memcpy(fresh.magic, format.
magic,
sizeof(fresh.magic));
44 }
else if (std::memcmp(header->magic, format.
magic,
sizeof(header->magic)) != 0) {
45 throw std::runtime_error(std::string(format.
name) +
": " +
path +
" is not a session store");
46 }
else if (header->version != format.
version || header->slot_size != slot_size) {
47 throw std::runtime_error(std::string(format.
name) +
": " +
path +
48 " was written with a different layout (version " +
49 std::to_string(header->version) +
")");
50 }
else if (std::memcmp(header->identity, identity.data(),
IDENTITY_SIZE) != 0) {
51 throw std::runtime_error(std::string(format.
name) +
": " +
path +
" belongs to another session, not " +
52 std::string(identity_text));
54 capacity_ =
static_cast<uint32_t
>((file_.size() -
HEADER_SIZE) / slot_size_);
55 LOG_INFO(
"{}: {} has room for {} record(s) of {}", name_, path_, capacity_, identity_text);
SlotFile(const std::string &path, const SlotStoreFormat &format, const std::array< char, IDENTITY_SIZE > &identity, std::string_view identity_text, size_t slot_size, size_t max_slots, const void *fresh_state, size_t state_size)