From 4e50889b5cb05833e644b98140908253ef5f53c1 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 11 Feb 2024 17:23:38 -0600 Subject: [PATCH] [ox/model,ox/preloader] Add ability to handle inline arrays --- deps/ox/src/ox/model/modelvalue.hpp | 22 +++++++++++++++++++++- deps/ox/src/ox/model/types.hpp | 17 ++++++++++++++++- deps/ox/src/ox/preloader/preloader.hpp | 15 +++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/deps/ox/src/ox/model/modelvalue.hpp b/deps/ox/src/ox/model/modelvalue.hpp index 1108e7c8..ca3a96b6 100644 --- a/deps/ox/src/ox/model/modelvalue.hpp +++ b/deps/ox/src/ox/model/modelvalue.hpp @@ -359,6 +359,12 @@ class ModelValueArray { }; +template<> +[[nodiscard]] +consteval bool isArray(ModelValueArray*) noexcept { + return true; +} + class ModelValueVector { private: Vector m_vec; @@ -817,6 +823,12 @@ class ModelUnion { }; +template +[[nodiscard]] +constexpr std::size_t sizeOf(ModelValueArray const*v) noexcept { + return sizeOf(&(*v)[0]) * v->size(); +} + template [[nodiscard]] constexpr std::size_t sizeOf(const ModelValueVector*) noexcept { @@ -888,6 +900,12 @@ constexpr std::size_t sizeOf(const ModelValue *t) noexcept { return size; } +template +[[nodiscard]] +constexpr std::size_t alignOf(ModelValueArray const&v) noexcept { + return alignOf(v[0]); +} + template [[nodiscard]] constexpr std::size_t alignOf(const ModelValueVector&) noexcept { @@ -1072,12 +1090,14 @@ constexpr Error ModelValue::setType( if (subscript.subscriptType == Subscript::SubscriptType::InlineArray) { m_type = Type::InlineArray; m_data.array = new ModelValueArray; + oxReturnError(m_data.array->setType(type, subscriptLevels - 1, subscriptStack)); oxReturnError(m_data.array->setSize(static_cast(subscript.length))); } else { m_type = Type::Vector; m_data.vec = new ModelValueVector; + oxReturnError(m_data.vec->setType(type, subscriptLevels - 1, subscriptStack)); } - return m_data.vec->setType(type, subscriptLevels - 1, subscriptStack); + return {}; } else if (type->typeName == types::Bool) { m_type = Type::Bool; } else if (type->typeName == types::BasicString || diff --git a/deps/ox/src/ox/model/types.hpp b/deps/ox/src/ox/model/types.hpp index 85581ec2..66c1d8f7 100644 --- a/deps/ox/src/ox/model/types.hpp +++ b/deps/ox/src/ox/model/types.hpp @@ -131,7 +131,12 @@ template constexpr bool isBareArray_v = true; template -constexpr bool isArray_v = false; +consteval bool isArray(T*) noexcept { + return false; +} + +template +constexpr bool isArray_v = isArray(static_cast(nullptr)); template constexpr bool isArray_v = true; @@ -142,6 +147,16 @@ constexpr bool isArray_v = true; template constexpr bool isArray_v> = true; +template +consteval bool isArray(T[sz]) noexcept { + return true; +} + +template +consteval bool isArray(Array) noexcept { + return true; +} + template constexpr bool isSmartPtr_v = false; diff --git a/deps/ox/src/ox/preloader/preloader.hpp b/deps/ox/src/ox/preloader/preloader.hpp index 23b815f4..be23d0d1 100644 --- a/deps/ox/src/ox/preloader/preloader.hpp +++ b/deps/ox/src/ox/preloader/preloader.hpp @@ -64,6 +64,7 @@ class Preloader: public ModelHandlerBase, OpType::Reflect> { restore(pRestore), seekdir(pSeekdir) {} }; ox::Vector m_allocStack; + ox::Vector m_allocStart{}; constexpr Preloader() noexcept: m_writer(&m_buff) {} @@ -173,18 +174,26 @@ constexpr ox::Error Preloader::field(CRStringView name, const T *val) } oxReturnError(pad(val)); if constexpr(ox::is_integral_v) { + //oxDebug(" integral"); + //oxDebugf("Preloader::field(name, val): {}", name); return ox::serialize(&m_writer, PlatSpec::correctEndianness(*val)); } else if constexpr(ox::is_pointer_v) { + //oxDebug(" pointer"); const PtrType a = startAlloc(sizeOf(val), alignOf(*val), m_writer.tellp()) + PlatSpec::RomStart; oxReturnError(field(name, *val)); oxReturnError(endAlloc()); return ox::serialize(&m_writer, PlatSpec::correctEndianness(a)); } else if constexpr(ox::isVector_v) { + //oxDebug(" vector"); return fieldVector(name, val); } else if constexpr(ox::is_same_v) { val->types(); + //oxDebug(" vector"); return fieldVector(name, val); + } else if constexpr(ox::is_same_v) { + return fieldArray(name, val); } else { + //oxDebug(" object"); m_unionIdx.emplace_back(-1); const auto out = preload(this, val); m_unionIdx.pop_back(); @@ -266,6 +275,7 @@ constexpr ox::Result Preloader::startAlloc(size_t sz, siz oxRequireM(a, ox::allocate(&m_writer, sz + padding)); a += padding; oxReturnError(m_writer.seekp(a)); + m_allocStart.push_back(a); return a; } @@ -277,6 +287,7 @@ constexpr ox::Result Preloader::startAlloc(std::size_t sz oxRequireM(a, ox::allocate(&m_writer, sz + padding)); a += padding; oxReturnError(m_writer.seekp(a)); + m_allocStart.push_back(a); return a; } @@ -288,6 +299,7 @@ constexpr ox::Error Preloader::endAlloc() noexcept { const auto &si = *m_allocStack.back().unwrap(); oxReturnError(m_writer.seekp(static_cast(si.restore), si.seekdir)); m_allocStack.pop_back(); + m_allocStart.pop_back(); return {}; } @@ -372,6 +384,9 @@ constexpr ox::Error Preloader::fieldVector( template constexpr ox::Error Preloader::fieldArray(CRStringView, ox::ModelValueArray const*val) noexcept { + oxDebugf("array size: {}", val->size()); + oxDebugf("array sizeOf: {}", sizeOf(val)); + oxReturnError(pad(&(*val)[0])); for (auto const&v : *val) { oxReturnError(this->interface()->field({}, &v)); }