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