From 58dbda48c402f99577a8432e60e63ef954bc3d87 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 21 Apr 2021 00:06:24 -0500 Subject: [PATCH] [ox] Add Result read{OC,MC,Claw}(...) --- deps/ox/src/ox/claw/read.hpp | 12 ++++++++ deps/ox/src/ox/mc/read.hpp | 12 ++++++++ deps/ox/src/ox/oc/read.hpp | 22 ++++++++++---- deps/ox/src/ox/oc/test/tests.cpp | 50 ++++++++++++++++---------------- 4 files changed, 65 insertions(+), 31 deletions(-) diff --git a/deps/ox/src/ox/claw/read.hpp b/deps/ox/src/ox/claw/read.hpp index 7f10e780..c72e542b 100644 --- a/deps/ox/src/ox/claw/read.hpp +++ b/deps/ox/src/ox/claw/read.hpp @@ -58,4 +58,16 @@ Error readClaw(const char *buff, std::size_t buffLen, T *val) { return OxError(1); } +template +Result readClaw(const char *buff, std::size_t buffLen) { + T val; + oxReturnError(readClaw(buff, buffLen, &val)); + return ox::move(val); +} + +template +Result readClaw(const Vector &buff) { + return readClaw(buff.data(), buff.size()); +} + } diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index 2616ebf5..d05f95b1 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -277,4 +277,16 @@ Error readMC(const uint8_t *buff, std::size_t buffLen, T *val) { return model(&reader, val); } +template +Result readMC(const char *buff, std::size_t buffLen) { + T val; + oxReturnError(readMC(buff, buffLen, &val)); + return ox::move(val); +} + +template +Result readMC(const Vector &buff) { + return readMC(buff.data(), buff.size()); +} + } diff --git a/deps/ox/src/ox/oc/read.hpp b/deps/ox/src/ox/oc/read.hpp index 0c5c9bee..4590a02a 100644 --- a/deps/ox/src/ox/oc/read.hpp +++ b/deps/ox/src/ox/oc/read.hpp @@ -173,7 +173,7 @@ Error OrganicClawReader::field(const char *key, HashMap *val) { auto srcSize = srcVal.size(); OrganicClawReader r(srcVal); for (decltype(srcSize) i = 0; i < srcSize; ++i) { - auto k = keys[i].c_str(); + const auto k = keys[i].c_str(); oxReturnError(r.field(k, &val->operator[](k))); } return OxError(0); @@ -191,7 +191,7 @@ Error readOC(const char *json, std::size_t jsonSize, T *val) noexcept { } OrganicClawReader reader(json, jsonSize); return model(&reader, val); - } catch (Error err) { + } catch (const Error &err) { return err; } catch (...) { return OxError(1, "Unkown Error"); @@ -199,10 +199,20 @@ Error readOC(const char *json, std::size_t jsonSize, T *val) noexcept { } template -Result> readOC(const char *json) { - auto val = ox::make_unique(); - oxReturnError(readOC(json, ox_strlen(json), val.get())); - return val; +Result readOC(const char *json, std::size_t jsonLen) { + T val; + oxReturnError(readOC(json, jsonLen, &val)); + return ox::move(val); +} + +template +Result readOC(const char *json) { + return readOC(json, ox_strlen(json)); +} + +template +Result readOC(const Vector &buff) { + return readOC(buff.data(), buff.size()); } } diff --git a/deps/ox/src/ox/oc/test/tests.cpp b/deps/ox/src/ox/oc/test/tests.cpp index 0e677ec7..bddcfd3f 100644 --- a/deps/ox/src/ox/oc/test/tests.cpp +++ b/deps/ox/src/ox/oc/test/tests.cpp @@ -132,31 +132,31 @@ std::map tests = { auto [testOut, readErr] = ox::readOC(oc.data()); oxAssert(readErr, "readOC failed"); - oxAssert(testIn.Bool == testOut->Bool, "Bool value mismatch"); - oxAssert(testIn.Int == testOut->Int, "Int value mismatch"); - oxAssert(testIn.Int1 == testOut->Int1, "Int1 value mismatch"); - oxAssert(testIn.Int2 == testOut->Int2, "Int2 value mismatch"); - oxAssert(testIn.Int3 == testOut->Int3, "Int3 value mismatch"); - oxAssert(testIn.Int4 == testOut->Int4, "Int4 value mismatch"); - oxAssert(testIn.Int5 == testOut->Int5, "Int5 value mismatch"); - oxAssert(testIn.Int6 == testOut->Int6, "Int6 value mismatch"); - oxAssert(testIn.Int7 == testOut->Int7, "Int7 value mismatch"); - oxAssert(testIn.Int8 == testOut->Int8, "Int8 value mismatch"); - oxAssert(ox_strcmp(testIn.CString, testOut->CString) == 0, "CString value mismatch"); - oxAssert(testIn.Union.Int == testOut->Union.Int, "Union.Int value mismatch"); - oxAssert(testIn.String == testOut->String, "String value mismatch"); - oxAssert(testIn.List[0] == testOut->List[0], "List[0] value mismatch"); - oxAssert(testIn.List[1] == testOut->List[1], "List[1] value mismatch"); - oxAssert(testIn.List[2] == testOut->List[2], "List[2] value mismatch"); - oxAssert(testIn.List[3] == testOut->List[3], "List[3] value mismatch"); - oxAssert(testIn.Map["asdf"] == testOut->Map["asdf"], "Map[\"asdf\"] value mismatch"); - oxAssert(testIn.Map["aoeu"] == testOut->Map["aoeu"], "Map[\"aoeu\"] value mismatch"); - oxAssert(testIn.EmptyStruct.Bool == testOut->EmptyStruct.Bool, "EmptyStruct.Bool value mismatch"); - oxAssert(testIn.EmptyStruct.Int == testOut->EmptyStruct.Int, "EmptyStruct.Int value mismatch"); - oxAssert(testIn.EmptyStruct.String == testOut->EmptyStruct.String, "EmptyStruct.String value mismatch"); - oxAssert(testIn.Struct.Int == testOut->Struct.Int, "Struct.Int value mismatch"); - oxAssert(testIn.Struct.String == testOut->Struct.String, "Struct.String value mismatch"); - oxAssert(testIn.Struct.Bool == testOut->Struct.Bool, "Struct.Bool value mismatch"); + oxAssert(testIn.Bool == testOut.Bool, "Bool value mismatch"); + oxAssert(testIn.Int == testOut.Int, "Int value mismatch"); + oxAssert(testIn.Int1 == testOut.Int1, "Int1 value mismatch"); + oxAssert(testIn.Int2 == testOut.Int2, "Int2 value mismatch"); + oxAssert(testIn.Int3 == testOut.Int3, "Int3 value mismatch"); + oxAssert(testIn.Int4 == testOut.Int4, "Int4 value mismatch"); + oxAssert(testIn.Int5 == testOut.Int5, "Int5 value mismatch"); + oxAssert(testIn.Int6 == testOut.Int6, "Int6 value mismatch"); + oxAssert(testIn.Int7 == testOut.Int7, "Int7 value mismatch"); + oxAssert(testIn.Int8 == testOut.Int8, "Int8 value mismatch"); + oxAssert(ox_strcmp(testIn.CString, testOut.CString) == 0, "CString value mismatch"); + oxAssert(testIn.Union.Int == testOut.Union.Int, "Union.Int value mismatch"); + oxAssert(testIn.String == testOut.String, "String value mismatch"); + oxAssert(testIn.List[0] == testOut.List[0], "List[0] value mismatch"); + oxAssert(testIn.List[1] == testOut.List[1], "List[1] value mismatch"); + oxAssert(testIn.List[2] == testOut.List[2], "List[2] value mismatch"); + oxAssert(testIn.List[3] == testOut.List[3], "List[3] value mismatch"); + oxAssert(testIn.Map["asdf"] == testOut.Map["asdf"], "Map[\"asdf\"] value mismatch"); + oxAssert(testIn.Map["aoeu"] == testOut.Map["aoeu"], "Map[\"aoeu\"] value mismatch"); + oxAssert(testIn.EmptyStruct.Bool == testOut.EmptyStruct.Bool, "EmptyStruct.Bool value mismatch"); + oxAssert(testIn.EmptyStruct.Int == testOut.EmptyStruct.Int, "EmptyStruct.Int value mismatch"); + oxAssert(testIn.EmptyStruct.String == testOut.EmptyStruct.String, "EmptyStruct.String value mismatch"); + oxAssert(testIn.Struct.Int == testOut.Struct.Int, "Struct.Int value mismatch"); + oxAssert(testIn.Struct.String == testOut.Struct.String, "Struct.String value mismatch"); + oxAssert(testIn.Struct.Bool == testOut.Struct.Bool, "Struct.Bool value mismatch"); return OxError(0); }