This commit is contained in:
es3n1n
2025-05-07 17:01:09 +09:00
commit a49f027fde
33 changed files with 2609 additions and 0 deletions

52
cxx-shared/shared/ctx.hpp Normal file
View File

@@ -0,0 +1,52 @@
#pragma once
#include <array>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include "shared/util.hpp"
namespace shared {
constexpr std::size_t kMaxNameLength = 128;
constexpr std::string_view kCtxPath = "ctx.bin";
namespace detail {
inline std::string ctx_path() {
auto path = get_this_module_path().parent_path();
path /= kCtxPath;
return path.string();
}
} // namespace detail
enum class State : std::uint8_t {
OFF = 0,
ON,
};
inline constinit struct Context {
public:
State state = State::ON;
bool verbose = false;
std::array<char, kMaxNameLength + 1> name = {0}; // +1 for the nullterm
void serialize() const {
std::ofstream stream(detail::ctx_path(), std::ios::binary);
if (!stream.good()) {
throw std::runtime_error("can not write ctx.bin");
}
stream.write(reinterpret_cast<const char*>(this), sizeof(*this));
}
void deserialize() {
std::ifstream stream(detail::ctx_path(), std::ios::binary);
if (!stream.good()) {
throw std::runtime_error("can not read ctx.bin");
}
stream.read(reinterpret_cast<char*>(this), sizeof(*this));
}
} ctx = {};
static_assert(std::is_trivially_copyable_v<Context>);
} // namespace shared