[ox/preloader] Cleanup

This commit is contained in:
Gary Talent 2023-02-25 22:25:14 -06:00
parent 1767821161
commit 317e714373
3 changed files with 19 additions and 10 deletions

View File

@ -15,7 +15,7 @@ namespace ox {
template<typename PlatSpec, typename T> template<typename PlatSpec, typename T>
[[nodiscard]] [[nodiscard]]
constexpr std::size_t alignOf(const T &t) noexcept; constexpr std::size_t alignOf(const T &v) noexcept;
template<typename PlatSpec, typename T> template<typename PlatSpec, typename T>
[[nodiscard]] [[nodiscard]]
@ -36,7 +36,7 @@ struct AlignmentCatcher: public ModelHandlerBase<AlignmentCatcher<PlatSpec>> {
template<typename T> template<typename T>
constexpr ox::Error field(CRStringView, const T *val) noexcept { constexpr ox::Error field(CRStringView, const T *val) noexcept {
if constexpr(ox::is_integer_v<T>) { if constexpr(ox::is_pointer_v<T> || ox::is_integer_v<T>) {
biggestAlignment = ox::max(biggestAlignment, PlatSpec::alignOf(*val)); biggestAlignment = ox::max(biggestAlignment, PlatSpec::alignOf(*val));
} else { } else {
biggestAlignment = ox::max(biggestAlignment, alignOf<PlatSpec>(*val)); biggestAlignment = ox::max(biggestAlignment, alignOf<PlatSpec>(*val));

View File

@ -21,11 +21,12 @@ struct NativePlatSpec {
template<typename T> template<typename T>
[[nodiscard]] [[nodiscard]]
static constexpr auto alignOf(const T &v) noexcept { static constexpr std::size_t alignOf(const T &v) noexcept {
if constexpr(ox::is_integral_v<T>) { if constexpr(ox::is_integral_v<T>) {
return alignof(T); return alignof(T);
} else if constexpr(ox::is_pointer_v<T>) { } else if constexpr(ox::is_pointer_v<T>) {
return PtrAlign; PtrType p = 0;
return alignOf(p);
} else { } else {
AlignmentCatcher<NativePlatSpec> c; AlignmentCatcher<NativePlatSpec> c;
oxAssert(model(c.interface(), &v), "Could not get alignment for type"); oxAssert(model(c.interface(), &v), "Could not get alignment for type");
@ -41,11 +42,17 @@ struct NativePlatSpec {
template<typename PlatSpec, typename T> template<typename PlatSpec, typename T>
[[nodiscard]] [[nodiscard]]
constexpr std::size_t alignOf(const T &t) noexcept { constexpr std::size_t alignOf(const T &v) noexcept {
return PlatSpec::alignOf(t); if constexpr(ox::is_integral_v<T>) {
return alignof(T);
} else if constexpr(ox::is_pointer_v<T>) {
typename PlatSpec::PtrType p = 0;
return PlatSpec::alignOf(p);
} else {
AlignmentCatcher<NativePlatSpec> c;
oxAssert(model(c.interface(), &v), "Could not get alignment for type");
return c.biggestAlignment;
}
} }
template<typename PlatSpec, typename T>
constexpr auto alignOf_v = alignOf<PlatSpec>(static_cast<T*>(nullptr));
} }

View File

@ -33,6 +33,7 @@ class Preloader: public ModelHandlerBase<Preloader<PlatSpec>> {
private: private:
using PtrType = typename PlatSpec::PtrType; using PtrType = typename PlatSpec::PtrType;
static constexpr auto PtrSize = sizeof(PtrType);
class UnionIdxTracker { class UnionIdxTracker {
private: private:
int m_unionIdx = -1; int m_unionIdx = -1;
@ -165,6 +166,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView name, const T *val)
} }
oxReturnError(pad(val)); oxReturnError(pad(val));
if constexpr(ox::is_integral_v<T>) { if constexpr(ox::is_integral_v<T>) {
//oxDebugf("Preloader::field(name, val): {}", name);
return ox::serialize(&m_writer, PlatSpec::correctEndianness(*val)); return ox::serialize(&m_writer, PlatSpec::correctEndianness(*val));
} else if constexpr(ox::is_pointer_v<T>) { } else if constexpr(ox::is_pointer_v<T>) {
const PtrType a = startAlloc(sizeOf<PlatSpec>(*val), m_writer.tellp()) + PlatSpec::RomStart; const PtrType a = startAlloc(sizeOf<PlatSpec>(*val), m_writer.tellp()) + PlatSpec::RomStart;
@ -336,7 +338,7 @@ constexpr ox::Error Preloader<PlatSpec>::fieldVector(CRStringView, const auto *v
} }
// serialize the Vector // serialize the Vector
oxReturnError(serialize(&m_writer, vecVal)); oxReturnError(serialize(&m_writer, vecVal));
m_ptrs.emplace_back(m_writer.tellp() - PlatSpec::PtrSize, vecVal.items); m_ptrs.emplace_back(m_writer.tellp() - PtrSize, vecVal.items);
return {}; return {};
} }