diff --git a/deps/ox/src/ox/mc/test/tests.cpp b/deps/ox/src/ox/mc/test/tests.cpp index a41dc5c..8100366 100644 --- a/deps/ox/src/ox/mc/test/tests.cpp +++ b/deps/ox/src/ox/mc/test/tests.cpp @@ -320,27 +320,27 @@ std::map tests = { ox::ModelObject testOut; oxReturnError(testOut.setType(type)); oxAssert(ox::readMC(dataBuff.data(), dataBuff.size(), &testOut), "Data read failed"); - oxAssert(testOut["Int"].get() == testIn.Int, "testOut.Int failed"); - oxAssert(testOut["Bool"].get() == testIn.Bool, "testOut.Bool failed"); - oxAssert(testOut["BString"].get() == testIn.BString.c_str(), "testOut.String failed"); - oxAssert(testOut["String"].get() == testIn.String, "testOut.String failed"); - auto &testOutStruct = testOut["Struct"].get(); - auto &testOutUnion = testOut["Union"].get(); - auto &testOutList = testOut["List"].get(); - auto testOutStructCopy = testOut["Struct"].get(); - auto testOutUnionCopy = testOut["Union"].get(); - auto testOutListCopy = testOut["List"].get(); + oxAssert(testOut.at("Int").unwrap()->get() == testIn.Int, "testOut.Int failed"); + oxAssert(testOut.at("Bool").unwrap()->get() == testIn.Bool, "testOut.Bool failed"); + oxAssert(testOut.at("BString").unwrap()->get() == testIn.BString.c_str(), "testOut.String failed"); + oxAssert(testOut.at("String").unwrap()->get() == testIn.String, "testOut.String failed"); + auto &testOutStruct = testOut.at("Struct").unwrap()->get(); + auto &testOutUnion = testOut.at("Union").unwrap()->get(); + auto &testOutList = testOut.at("List").unwrap()->get(); + auto testOutStructCopy = testOut.at("Struct").unwrap()->get(); + auto testOutUnionCopy = testOut.at("Union").unwrap()->get(); + auto testOutListCopy = testOut.at("List").unwrap()->get(); oxAssert(testOutStruct.typeName() == TestStructNest::TypeName, "ModelObject TypeName failed"); oxAssert(testOutStruct.typeVersion() == TestStructNest::TypeVersion, "ModelObject TypeVersion failed"); - oxAssert(testOutStruct["Bool"].get() == testIn.Struct.Bool, "testOut.Struct.Bool failed"); - oxAssert(testOutStruct["BString"].get() == testIn.Struct.BString.c_str(), "testOut.Struct.BString failed"); - oxAssert(testOut["unionIdx"].get() == testIn.unionIdx, "testOut.unionIdx failed"); + oxAssert(testOutStruct.at("Bool").unwrap()->get() == testIn.Struct.Bool, "testOut.Struct.Bool failed"); + oxAssert(testOutStruct.at("BString").unwrap()->get() == testIn.Struct.BString.c_str(), "testOut.Struct.BString failed"); + oxAssert(testOut.at("unionIdx").unwrap()->get() == testIn.unionIdx, "testOut.unionIdx failed"); oxAssert(testOutUnion.unionIdx() == testIn.unionIdx, "testOut.Union idx wrong"); - oxAssert(testOutUnion["Int"].get() == testIn.Union.Int, "testOut.Union.Int failed"); + oxAssert(testOutUnion.at("Int").unwrap()->get() == testIn.Union.Int, "testOut.Union.Int failed"); oxAssert(testOutList[0].get() == testIn.List[0], "testOut.List[0] failed"); oxAssert(testOutList[1].get() == testIn.List[1], "testOut.Struct.List[1] failed"); - oxAssert(testOutStructCopy["Bool"].get() == testIn.Struct.Bool, "testOut.Struct.Bool (copy) failed"); - oxAssert(testOutStructCopy["BString"].get() == testIn.Struct.BString.c_str(), "testOut.Struct.BString (copy) failed"); + oxAssert(testOutStructCopy.at("Bool").unwrap()->get() == testIn.Struct.Bool, "testOut.Struct.Bool (copy) failed"); + oxAssert(testOutStructCopy.at("BString").unwrap()->get() == testIn.Struct.BString.c_str(), "testOut.Struct.BString (copy) failed"); oxAssert(testOutListCopy[0].get() == testIn.List[0], "testOut.Struct.List[0] (copy) failed"); oxAssert(testOutListCopy[1].get() == testIn.List[1], "testOut.Struct.List[1] (copy) failed"); return OxError(0); diff --git a/deps/ox/src/ox/model/modelvalue.hpp b/deps/ox/src/ox/model/modelvalue.hpp index ca3a96b..5313033 100644 --- a/deps/ox/src/ox/model/modelvalue.hpp +++ b/deps/ox/src/ox/model/modelvalue.hpp @@ -642,12 +642,9 @@ class ModelObject { return {}; } - constexpr auto &operator[](StringView const&k) noexcept { - auto [v, err] = m_fields.at(k); - if (err) [[unlikely]] { - oxPanic(err, ox::sfmt("field {} does not exist in type {}", k, buildTypeId(*m_type)).c_str()); - } - return **v; + constexpr ox::Result at(StringView const&k) noexcept { + oxRequire(v, m_fields.at(k)); + return *v; } constexpr auto &operator[](const std::size_t i) noexcept { @@ -731,12 +728,9 @@ class ModelUnion { return UniquePtr(new ModelUnion(other)); } - constexpr auto &operator[](StringView const&k) noexcept { - const auto [v, err] = m_fields.at(k); - if (err) [[unlikely]] { - oxPanic(err, ox::sfmt("field {} does not exist in type {}", k, buildTypeId(*m_type)).c_str()); - } - return (*v)->value; + constexpr ox::Result at(StringView const&k) noexcept { + oxRequire(v, m_fields.at(k)); + return &(*v)->value; } constexpr auto &operator[](const std::size_t i) noexcept { diff --git a/deps/ox/src/ox/preloader/preloader.hpp b/deps/ox/src/ox/preloader/preloader.hpp index 1f1df9f..59b761b 100644 --- a/deps/ox/src/ox/preloader/preloader.hpp +++ b/deps/ox/src/ox/preloader/preloader.hpp @@ -174,12 +174,12 @@ constexpr ox::Error Preloader::field(CRStringView name, const T *val) } oxReturnError(pad(val)); if constexpr(ox::is_integral_v) { - return ox::serialize(&m_writer, PlatSpec::correctEndianness(*val)); + return ox::serialize(m_writer, PlatSpec::correctEndianness(*val)); } else if constexpr(ox::is_pointer_v) { 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)); + return ox::serialize(m_writer, PlatSpec::correctEndianness(a)); } else if constexpr(ox::isVector_v) { return fieldVector(name, val); } else if constexpr(ox::is_same_v) { @@ -212,7 +212,7 @@ constexpr ox::Error Preloader::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::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 Preloader::startAlloc(size_t sz, siz m_allocStack.emplace_back(static_cast(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 Preloader::startAlloc(size_t sz, siz } template -constexpr ox::Result Preloader::startAlloc(std::size_t sz, size_t align, std::size_t restore) noexcept { +constexpr ox::Result Preloader::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 constexpr ox::Error Preloader::offsetPtrs(std::size_t offset) noexcept { for (const auto &p : m_ptrs) { oxReturnError(m_writer.seekp(p.loc)); - const auto val = PlatSpec::template correctEndianness(static_cast(p.value + offset)); - oxReturnError(ox::serialize(&m_writer, val)); + const auto val = PlatSpec::template correctEndianness( + static_cast(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::pad(const T *v) noexcept { } template -constexpr ox::Error Preloader::fieldVector(CRStringView name, const ox::ModelValueVector *val) noexcept { +constexpr ox::Error Preloader::fieldVector( + CRStringView name, const ox::ModelValueVector *val) noexcept { // serialize the Vector ox::VectorMemMap vecVal{ .size = PlatSpec::correctEndianness(static_cast(val->size())), @@ -356,7 +359,7 @@ constexpr ox::Error Preloader::fieldVector( const auto align = alignOf((*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::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 {}; } diff --git a/deps/ox/src/ox/std/reader.hpp b/deps/ox/src/ox/std/reader.hpp index 3eacb8f..8a67a37 100644 --- a/deps/ox/src/ox/std/reader.hpp +++ b/deps/ox/src/ox/std/reader.hpp @@ -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 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; -} - } diff --git a/deps/ox/src/ox/std/serialize.hpp b/deps/ox/src/ox/std/serialize.hpp index ca68ba0..23e3481 100644 --- a/deps/ox/src/ox/std/serialize.hpp +++ b/deps/ox/src/ox/std/serialize.hpp @@ -54,42 +54,42 @@ constexpr auto alignOf(const VectorMemMap&) noexcept { } template -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 -constexpr ox::Error serialize(Writer_c auto *buff, const VectorMemMap &vm) noexcept { - oxReturnError(buff->write(nullptr, vm.smallVecSize)); - oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.allocator))); - oxReturnError(pad(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 &vm) noexcept { + oxReturnError(w.write(nullptr, vm.smallVecSize)); + oxReturnError(serialize(w, PlatSpec::correctEndianness(vm.allocator))); + oxReturnError(pad(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 -constexpr ox::Error serialize(Writer_c auto *buff, T val) noexcept requires(is_integer_v) { +constexpr ox::Error serialize(Writer_c auto &w, T val) noexcept requires(is_integer_v) { ox::Array tmp; for (auto i = 0u; i < sizeof(T); ++i) { tmp[i] = static_cast((val >> i * 8) & 255); } - return buff->write(tmp.data(), tmp.size()); + return w.write(tmp.data(), tmp.size()); }; template constexpr ox::Result> serialize(const T &in) noexcept { ox::Array out = {}; CharBuffWriter w(out); - oxReturnError(serialize(&w, in)); + oxReturnError(serialize(w, in)); return out; }; -} \ No newline at end of file +} diff --git a/deps/ox/src/ox/std/writer.hpp b/deps/ox/src/ox/std/writer.hpp index 18a4483..53fbb7a 100644 --- a/deps/ox/src/ox/std/writer.hpp +++ b/deps/ox/src/ox/std/writer.hpp @@ -72,12 +72,12 @@ class WriterT: public Writer_v { * @param sz * @return */ -constexpr ox::Result 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 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; } diff --git a/src/olympic/keel/include/keel/typeconv.hpp b/src/olympic/keel/include/keel/typeconv.hpp index 6bf9756..aa9ca38 100644 --- a/src/olympic/keel/include/keel/typeconv.hpp +++ b/src/olympic/keel/include/keel/typeconv.hpp @@ -94,16 +94,16 @@ class Converter: public BaseConverter { [[nodiscard]] bool srcMatches(ox::CRStringView pSrcTypeName, int pSrcTypeVersion) const noexcept final { - static const auto SrcTypeName = ox::requireModelTypeName(); - static const auto SrcTypeVersion = ox::requireModelTypeVersion(); + constexpr auto SrcTypeName = ox::requireModelTypeName(); + constexpr auto SrcTypeVersion = ox::requireModelTypeVersion(); return pSrcTypeName == SrcTypeName && pSrcTypeVersion == SrcTypeVersion; } [[nodiscard]] bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept final { - static const auto DstTypeName = ox::StringView(ox::requireModelTypeName()); - static const auto DstTypeVersion = ox::requireModelTypeVersion(); + constexpr auto DstTypeName = ox::StringView(ox::requireModelTypeName()); + constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); return dstTypeName == DstTypeName && dstTypeVersion == DstTypeVersion; } diff --git a/src/olympic/keel/src/pack.cpp b/src/olympic/keel/src/pack.cpp index 68dabdc..2f27e31 100644 --- a/src/olympic/keel/src/pack.cpp +++ b/src/olympic/keel/src/pack.cpp @@ -14,15 +14,15 @@ namespace keel { static ox::Error pathToInode( keel::Context &ctx, ox::FileSystem &dest, ox::ModelObject &obj) noexcept { auto &o = obj; - auto type = static_cast(o["type"].get()); - auto &data = o["data"].get(); + auto type = static_cast(o.at("type").unwrap()->get()); + auto &data = o.at("data").unwrap()->get(); ox::String path; switch (type) { case ox::FileAddressType::Path: - path = data["path"].get(); + path = data.at("path").unwrap()->get(); break; case ox::FileAddressType::ConstPath: - path = data["constPath"].get(); + path = data.at("constPath").unwrap()->get(); break; case ox::FileAddressType::Inode: case ox::FileAddressType::None: @@ -33,7 +33,7 @@ static ox::Error pathToInode( oxReturnError(keel::uuidToPath(ctx, uuid).moveTo(path)); } oxRequire(s, dest.stat(path)); - oxReturnError(o["type"].set(static_cast(ox::FileAddressType::Inode))); + oxReturnError(o.at("type").unwrap()->set(static_cast(ox::FileAddressType::Inode))); oxOutf("path to inode: {} => {}\n", path, s.inode); return data.set(2, s.inode); }