[ox] Make serialize and allocate Writer_c functions take refs
All checks were successful
Build / build (push) Successful in 2m18s

This commit is contained in:
Gary Talent 2024-02-11 19:39:29 -06:00
parent a60cdf0a61
commit 24fda7d589
4 changed files with 35 additions and 47 deletions

View File

@ -174,12 +174,12 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView name, const T *val)
}
oxReturnError(pad(val));
if constexpr(ox::is_integral_v<T>) {
return ox::serialize(&m_writer, PlatSpec::correctEndianness(*val));
return ox::serialize(m_writer, PlatSpec::correctEndianness(*val));
} else if constexpr(ox::is_pointer_v<T>) {
const PtrType a = startAlloc(sizeOf<PlatSpec>(val), alignOf<PlatSpec>(*val), m_writer.tellp()) + PlatSpec::RomStart;
oxReturnError(field(name, *val));
oxReturnError(endAlloc());
return ox::serialize(&m_writer, PlatSpec::correctEndianness(a));
return ox::serialize(m_writer, PlatSpec::correctEndianness(a));
} else if constexpr(ox::isVector_v<T>) {
return fieldVector(name, val);
} else if constexpr(ox::is_same_v<T, ox::ModelValueVector>) {
@ -212,7 +212,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const ox::BasicStri
const auto restore = m_writer.tellp();
std::size_t a = 0;
if (sz && sz >= SmallStringSize) {
oxReturnError(ox::allocate(&m_writer, sz).moveTo(a));
oxReturnError(ox::allocate(m_writer, sz).moveTo(a));
} else {
a = restore;
}
@ -220,7 +220,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const ox::BasicStri
oxReturnError(m_writer.seekp(a));
oxReturnError(m_writer.write(val->data(), sz));
oxReturnError(m_writer.seekp(restore));
oxReturnError(serialize(&m_writer, vecVal));
oxReturnError(serialize(m_writer, vecVal));
m_ptrs.emplace_back(restore + offsetof(VecMap, items), vecVal.items);
return {};
}
@ -266,7 +266,7 @@ constexpr ox::Result<std::size_t> Preloader<PlatSpec>::startAlloc(size_t sz, siz
m_allocStack.emplace_back(static_cast<typename PlatSpec::PtrType>(m_writer.tellp()));
oxReturnError(m_writer.seekp(0, ox::ios_base::end));
const auto padding = m_writer.tellp() % align;
oxRequireM(a, ox::allocate(&m_writer, sz + padding));
oxRequireM(a, ox::allocate(m_writer, sz + padding));
a += padding;
oxReturnError(m_writer.seekp(a));
m_allocStart.push_back(a);
@ -274,11 +274,12 @@ constexpr ox::Result<std::size_t> Preloader<PlatSpec>::startAlloc(size_t sz, siz
}
template<typename PlatSpec>
constexpr ox::Result<std::size_t> Preloader<PlatSpec>::startAlloc(std::size_t sz, size_t align, std::size_t restore) noexcept {
constexpr ox::Result<std::size_t> Preloader<PlatSpec>::startAlloc(
std::size_t sz, size_t align, std::size_t restore) noexcept {
m_allocStack.emplace_back(restore, ox::ios_base::beg);
oxReturnError(m_writer.seekp(0, ox::ios_base::end));
const auto padding = m_writer.tellp() % align;
oxRequireM(a, ox::allocate(&m_writer, sz + padding));
oxRequireM(a, ox::allocate(m_writer, sz + padding));
a += padding;
oxReturnError(m_writer.seekp(a));
m_allocStart.push_back(a);
@ -301,8 +302,9 @@ template<typename PlatSpec>
constexpr ox::Error Preloader<PlatSpec>::offsetPtrs(std::size_t offset) noexcept {
for (const auto &p : m_ptrs) {
oxReturnError(m_writer.seekp(p.loc));
const auto val = PlatSpec::template correctEndianness<typename PlatSpec::PtrType>(static_cast<typename PlatSpec::PtrType>(p.value + offset));
oxReturnError(ox::serialize(&m_writer, val));
const auto val = PlatSpec::template correctEndianness<typename PlatSpec::PtrType>(
static_cast<typename PlatSpec::PtrType>(p.value + offset));
oxReturnError(ox::serialize(m_writer, val));
}
oxReturnError(m_writer.seekp(0, ox::ios_base::end));
return {};
@ -321,7 +323,8 @@ constexpr ox::Error Preloader<PlatSpec>::pad(const T *v) noexcept {
}
template<typename PlatSpec>
constexpr ox::Error Preloader<PlatSpec>::fieldVector(CRStringView name, const ox::ModelValueVector *val) noexcept {
constexpr ox::Error Preloader<PlatSpec>::fieldVector(
CRStringView name, const ox::ModelValueVector *val) noexcept {
// serialize the Vector
ox::VectorMemMap<PlatSpec> vecVal{
.size = PlatSpec::correctEndianness(static_cast<typename PlatSpec::size_t>(val->size())),
@ -356,7 +359,7 @@ constexpr ox::Error Preloader<PlatSpec>::fieldVector(
const auto align = alignOf<PlatSpec>((*val)[0]);
oxReturnError(m_writer.seekp(0, ox::ios_base::end));
const auto padding = m_writer.tellp() % align;
oxRequireM(p, ox::allocate(&m_writer, sz + padding));
oxRequireM(p, ox::allocate(m_writer, sz + padding));
p += padding;
oxReturnError(m_writer.seekp(p));
m_unionIdx.emplace_back(-1);
@ -371,7 +374,7 @@ constexpr ox::Error Preloader<PlatSpec>::fieldVector(
vecVal.items = 0;
}
// serialize the Vector
oxReturnError(serialize(&m_writer, vecVal));
oxReturnError(serialize(m_writer, vecVal));
m_ptrs.emplace_back(m_writer.tellp() - PtrSize, vecVal.items);
return {};
}

View File

@ -78,19 +78,4 @@ class FileReader: public Reader_v {
};
#endif
/**
* Allocates the specified amount of data at the end of the current read stream.
* @param reader
* @param sz
* @return
*/
constexpr ox::Result<std::size_t> allocate(Reader_c auto *reader, std::size_t sz) noexcept {
const auto p = reader->tellg();
oxReturnError(reader->seekg(0, ios_base::end));
const auto out = reader->tellg();
oxReturnError(reader->read(nullptr, sz));
oxReturnError(reader->seekg(p));
return out;
}
}

View File

@ -54,41 +54,41 @@ constexpr auto alignOf(const VectorMemMap<PlatSpec>&) noexcept {
}
template<typename PlatSpec, typename T>
constexpr ox::Error pad(Writer_c auto *w, const T *v) noexcept {
constexpr ox::Error pad(Writer_c auto &w, const T *v) noexcept {
const auto a = PlatSpec::alignOf(*v);
const auto excess = w->tellp() % a;
const auto excess = w.tellp() % a;
if (excess) {
return w->write(nullptr, a - excess);
return w.write(nullptr, a - excess);
} else {
return {};
}
}
template<typename PlatSpec>
constexpr ox::Error serialize(Writer_c auto *buff, const VectorMemMap<PlatSpec> &vm) noexcept {
oxReturnError(buff->write(nullptr, vm.smallVecSize));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.allocator)));
oxReturnError(pad<PlatSpec>(buff, &vm.size));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.size)));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.cap)));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.items)));
constexpr ox::Error serialize(Writer_c auto &w, const VectorMemMap<PlatSpec> &vm) noexcept {
oxReturnError(w.write(nullptr, vm.smallVecSize));
oxReturnError(serialize(w, PlatSpec::correctEndianness(vm.allocator)));
oxReturnError(pad<PlatSpec>(w, &vm.size));
oxReturnError(serialize(w, PlatSpec::correctEndianness(vm.size)));
oxReturnError(serialize(w, PlatSpec::correctEndianness(vm.cap)));
oxReturnError(serialize(w, PlatSpec::correctEndianness(vm.items)));
return {};
}
template<typename T>
constexpr ox::Error serialize(Writer_c auto *buff, T val) noexcept requires(is_integer_v<T>) {
constexpr ox::Error serialize(Writer_c auto &w, T val) noexcept requires(is_integer_v<T>) {
ox::Array<char, sizeof(T)> tmp;
for (auto i = 0u; i < sizeof(T); ++i) {
tmp[i] = static_cast<char>((val >> i * 8) & 255);
}
return buff->write(tmp.data(), tmp.size());
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 = {};
CharBuffWriter w(out);
oxReturnError(serialize(&w, in));
oxReturnError(serialize(w, in));
return out;
};

View File

@ -72,12 +72,12 @@ class WriterT: public Writer_v {
* @param sz
* @return
*/
constexpr ox::Result<std::size_t> allocate(Writer_c auto *writer, std::size_t sz) noexcept {
const auto p = writer->tellp();
oxReturnError(writer->seekp(0, ios_base::end));
const auto out = writer->tellp();
oxReturnError(writer->write(nullptr, sz));
oxReturnError(writer->seekp(p));
constexpr ox::Result<std::size_t> allocate(Writer_c auto &writer, std::size_t sz) noexcept {
const auto p = writer.tellp();
oxReturnError(writer.seekp(0, ios_base::end));
const auto out = writer.tellp();
oxReturnError(writer.write(nullptr, sz));
oxReturnError(writer.seekp(p));
return out;
}