From 74fb051ef24fb89a05c8ed9dc60cf0e862c3772b Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 11 Feb 2024 20:35:19 -0600 Subject: [PATCH] [ox] Remove panicing ModelValue operators --- deps/ox/src/ox/mc/test/tests.cpp | 32 ++++++++++++++--------------- deps/ox/src/ox/model/modelvalue.hpp | 18 ++++++---------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/deps/ox/src/ox/mc/test/tests.cpp b/deps/ox/src/ox/mc/test/tests.cpp index a41dc5c0..81003667 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 ca3a96b6..53130335 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 {