53 lines
1.3 KiB
C
Raw Normal View History

2025-12-03 22:00:29 +11:00
#pragma once
2025-12-22 21:43:01 +11:00
#include <FS.h>
2025-12-03 22:00:29 +11:00
#include <iostream>
namespace serialization {
template <typename T>
static void writePod(std::ostream& os, const T& value) {
os.write(reinterpret_cast<const char*>(&value), sizeof(T));
}
2025-12-22 20:41:09 +11:00
template <typename T>
static void writePod(File& file, const T& value) {
file.write(reinterpret_cast<const uint8_t*>(&value), sizeof(T));
}
2025-12-03 22:00:29 +11:00
template <typename T>
static void readPod(std::istream& is, T& value) {
is.read(reinterpret_cast<char*>(&value), sizeof(T));
}
2025-12-22 21:43:01 +11:00
template <typename T>
2025-12-22 20:41:09 +11:00
static void readPod(File& file, T& value) {
file.read(reinterpret_cast<uint8_t*>(&value), sizeof(T));
}
2025-12-03 22:00:29 +11:00
static void writeString(std::ostream& os, const std::string& s) {
const uint32_t len = s.size();
writePod(os, len);
os.write(s.data(), len);
}
2025-12-22 20:41:09 +11:00
static void writeString(File& file, const std::string& s) {
const uint32_t len = s.size();
writePod(file, len);
2025-12-22 21:43:01 +11:00
file.write(reinterpret_cast<const uint8_t*>(s.data()), len);
2025-12-22 20:41:09 +11:00
}
2025-12-03 22:00:29 +11:00
static void readString(std::istream& is, std::string& s) {
uint32_t len;
readPod(is, len);
s.resize(len);
is.read(&s[0], len);
}
2025-12-22 21:43:01 +11:00
static void readString(File& file, std::string& s) {
uint32_t len;
readPod(file, len);
s.resize(len);
file.read(reinterpret_cast<uint8_t*>(&s[0]), len);
}
2025-12-03 22:00:29 +11:00
} // namespace serialization