refactor(furvm): improve serialization

Refs: #12
This commit is contained in:
2026-06-19 15:14:25 +02:00
parent 6f023765b5
commit 99666495e7
7 changed files with 113 additions and 122 deletions
+49
View File
@@ -0,0 +1,49 @@
#include "furvm/detail/serialization.hpp"
#include <ostream>
namespace furvm::detail {
std::ostream& serialize(std::ostream& os, std::int8_t value) {
return os << (char)((value >> 0ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, std::int16_t value) {
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, std::int32_t value) {
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
<< (char)((value >> 24ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, std::int64_t value) {
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
<< (char)((value >> 24ULL) & 0xFF) << (char)((value >> 32ULL) & 0xFF) << (char)((value >> 40ULL) & 0xFF)
<< (char)((value >> 48ULL) & 0xFF) << (char)((value >> 56ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, std::uint8_t value) {
return os << (char)((value >> 0ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, std::uint16_t value) {
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, std::uint32_t value) {
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
<< (char)((value >> 24ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, std::uint64_t value) {
return os << (char)((value >> 0ULL) & 0xFF) << (char)((value >> 8ULL) & 0xFF) << (char)((value >> 16ULL) & 0xFF)
<< (char)((value >> 24ULL) & 0xFF) << (char)((value >> 32ULL) & 0xFF) << (char)((value >> 40ULL) & 0xFF)
<< (char)((value >> 48ULL) & 0xFF) << (char)((value >> 56ULL) & 0xFF);
}
std::ostream& serialize(std::ostream& os, const std::string& value) {
return serialize(os, std::uint16_t(value.size())).write(value.data(), static_cast<std::streamsize>(value.size()));
}
} // namespace furvm::detail