[ox/std] Cleanup serialize code
Some checks failed
Build / build (push) Failing after 52s

This commit is contained in:
2026-01-27 23:15:53 -06:00
parent 3e95bc0842
commit 5e1698a321

View File

@@ -18,7 +18,7 @@ namespace ox {
template<typename PlatSpec> template<typename PlatSpec>
struct VectorMemMap { struct VectorMemMap {
const std::size_t smallVecSize = 0; // not a map value size_t const smallVecSize = 0; // not a map value
typename PlatSpec::size_t size = 0; typename PlatSpec::size_t size = 0;
typename PlatSpec::size_t cap = 0; typename PlatSpec::size_t cap = 0;
typename PlatSpec::PtrType items = 0; typename PlatSpec::PtrType items = 0;
@@ -26,11 +26,11 @@ struct VectorMemMap {
template<typename PlatSpec> template<typename PlatSpec>
[[nodiscard]] [[nodiscard]]
constexpr auto sizeOf(const VectorMemMap<PlatSpec> *t) noexcept { constexpr auto sizeOf(VectorMemMap<PlatSpec> const *t) noexcept {
constexpr auto padding = [](std::size_t size, std::size_t al) { constexpr auto padding = [](size_t const size, size_t const al) {
return size % al; return size % al;
}; };
std::size_t size = 0; size_t size = 0;
if (t->smallVecSize) { if (t->smallVecSize) {
size += t->smallVecSize; size += t->smallVecSize;
size += padding(size, PlatSpec::alignOf(t->size)); size += padding(size, PlatSpec::alignOf(t->size));
@@ -43,17 +43,17 @@ constexpr auto sizeOf(const VectorMemMap<PlatSpec> *t) noexcept {
return size; return size;
} }
template<typename PlatSpec, std::size_t SmallVecSize = 0> template<typename PlatSpec, size_t SmallVecSize = 0>
[[nodiscard]] [[nodiscard]]
constexpr auto alignOf(const VectorMemMap<PlatSpec>&) noexcept { constexpr auto alignOf(VectorMemMap<PlatSpec> const&) noexcept {
const typename PlatSpec::size_t i = 0; typename PlatSpec::size_t const i = 0;
return PlatSpec::alignOf(i); return PlatSpec::alignOf(i);
} }
template<typename PlatSpec, typename T> template<typename PlatSpec, typename T>
constexpr ox::Error pad(Writer_c auto &w, const T *v) noexcept { constexpr Error pad(Writer_c auto &w, T const *v) noexcept {
const auto a = PlatSpec::alignOf(*v); auto const a = PlatSpec::alignOf(*v);
const auto excess = w.tellp() % a; auto const excess = w.tellp() % a;
if (excess) { if (excess) {
return w.write(nullptr, a - excess); return w.write(nullptr, a - excess);
} else { } else {
@@ -62,7 +62,7 @@ constexpr ox::Error pad(Writer_c auto &w, const T *v) noexcept {
} }
template<typename PlatSpec> template<typename PlatSpec>
constexpr ox::Error serialize(Writer_c auto &w, const VectorMemMap<PlatSpec> &vm) noexcept { constexpr Error serialize(Writer_c auto &w, VectorMemMap<PlatSpec> const &vm) noexcept {
OX_RETURN_ERROR(w.write(nullptr, vm.smallVecSize)); OX_RETURN_ERROR(w.write(nullptr, vm.smallVecSize));
OX_RETURN_ERROR(serialize(w, PlatSpec::correctEndianness(vm.size))); OX_RETURN_ERROR(serialize(w, PlatSpec::correctEndianness(vm.size)));
OX_RETURN_ERROR(serialize(w, PlatSpec::correctEndianness(vm.cap))); OX_RETURN_ERROR(serialize(w, PlatSpec::correctEndianness(vm.cap)));
@@ -70,21 +70,20 @@ constexpr ox::Error serialize(Writer_c auto &w, const VectorMemMap<PlatSpec> &vm
return {}; return {};
} }
template<typename T> constexpr Error serialize(Writer_c auto &w, Integral_c auto val) noexcept {
constexpr ox::Error serialize(Writer_c auto &w, T val) noexcept requires(is_integral_v<T>) { Array<char, sizeof(val)> tmp;
ox::Array<char, sizeof(T)> tmp; for (auto i = 0u; i < sizeof(val); ++i) {
for (auto i = 0u; i < sizeof(T); ++i) {
tmp[i] = static_cast<char>((val >> i * 8) & 255); tmp[i] = static_cast<char>((val >> i * 8) & 255);
} }
return w.write(tmp.data(), tmp.size()); return w.write(tmp.data(), tmp.size());
}; }
template<typename T> template<typename T>
constexpr ox::Result<ox::Array<char, sizeof(T)>> serialize(const T &in) noexcept { constexpr Result<Array<char, sizeof(T)>> serialize(T const &in) noexcept {
ox::Array<char, sizeof(T)> out = {}; Array<char, sizeof(T)> out = {};
CharBuffWriter w(out); CharBuffWriter w(out);
OX_RETURN_ERROR(serialize(w, in)); OX_RETURN_ERROR(serialize(w, in));
return out; return out;
}; }
} }