[ox] Add Result<T> read{OC,MC,Claw}<T>(...)

This commit is contained in:
Gary Talent 2021-04-21 00:06:24 -05:00
parent 4540b410dc
commit 58dbda48c4
4 changed files with 65 additions and 31 deletions

View File

@ -58,4 +58,16 @@ Error readClaw(const char *buff, std::size_t buffLen, T *val) {
return OxError(1); return OxError(1);
} }
template<typename T>
Result<T> readClaw(const char *buff, std::size_t buffLen) {
T val;
oxReturnError(readClaw(buff, buffLen, &val));
return ox::move(val);
}
template<typename T>
Result<T> readClaw(const Vector<char> &buff) {
return readClaw<T>(buff.data(), buff.size());
}
} }

View File

@ -277,4 +277,16 @@ Error readMC(const uint8_t *buff, std::size_t buffLen, T *val) {
return model(&reader, val); return model(&reader, val);
} }
template<typename T>
Result<T> readMC(const char *buff, std::size_t buffLen) {
T val;
oxReturnError(readMC(buff, buffLen, &val));
return ox::move(val);
}
template<typename T>
Result<T> readMC(const Vector<char> &buff) {
return readMC<T>(buff.data(), buff.size());
}
} }

View File

@ -173,7 +173,7 @@ Error OrganicClawReader::field(const char *key, HashMap<String, T> *val) {
auto srcSize = srcVal.size(); auto srcSize = srcVal.size();
OrganicClawReader r(srcVal); OrganicClawReader r(srcVal);
for (decltype(srcSize) i = 0; i < srcSize; ++i) { 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))); oxReturnError(r.field(k, &val->operator[](k)));
} }
return OxError(0); return OxError(0);
@ -191,7 +191,7 @@ Error readOC(const char *json, std::size_t jsonSize, T *val) noexcept {
} }
OrganicClawReader reader(json, jsonSize); OrganicClawReader reader(json, jsonSize);
return model(&reader, val); return model(&reader, val);
} catch (Error err) { } catch (const Error &err) {
return err; return err;
} catch (...) { } catch (...) {
return OxError(1, "Unkown Error"); return OxError(1, "Unkown Error");
@ -199,10 +199,20 @@ Error readOC(const char *json, std::size_t jsonSize, T *val) noexcept {
} }
template<typename T> template<typename T>
Result<ox::UniquePtr<T>> readOC(const char *json) { Result<T> readOC(const char *json, std::size_t jsonLen) {
auto val = ox::make_unique<T>(); T val;
oxReturnError(readOC(json, ox_strlen(json), val.get())); oxReturnError(readOC(json, jsonLen, &val));
return val; return ox::move(val);
}
template<typename T>
Result<T> readOC(const char *json) {
return readOC<T>(json, ox_strlen(json));
}
template<typename T>
Result<T> readOC(const Vector<char> &buff) {
return readOC<T>(buff.data(), buff.size());
} }
} }

View File

@ -132,31 +132,31 @@ std::map<std::string, ox::Error(*)()> tests = {
auto [testOut, readErr] = ox::readOC<TestStruct>(oc.data()); auto [testOut, readErr] = ox::readOC<TestStruct>(oc.data());
oxAssert(readErr, "readOC failed"); oxAssert(readErr, "readOC failed");
oxAssert(testIn.Bool == testOut->Bool, "Bool value mismatch"); oxAssert(testIn.Bool == testOut.Bool, "Bool value mismatch");
oxAssert(testIn.Int == testOut->Int, "Int value mismatch"); oxAssert(testIn.Int == testOut.Int, "Int value mismatch");
oxAssert(testIn.Int1 == testOut->Int1, "Int1 value mismatch"); oxAssert(testIn.Int1 == testOut.Int1, "Int1 value mismatch");
oxAssert(testIn.Int2 == testOut->Int2, "Int2 value mismatch"); oxAssert(testIn.Int2 == testOut.Int2, "Int2 value mismatch");
oxAssert(testIn.Int3 == testOut->Int3, "Int3 value mismatch"); oxAssert(testIn.Int3 == testOut.Int3, "Int3 value mismatch");
oxAssert(testIn.Int4 == testOut->Int4, "Int4 value mismatch"); oxAssert(testIn.Int4 == testOut.Int4, "Int4 value mismatch");
oxAssert(testIn.Int5 == testOut->Int5, "Int5 value mismatch"); oxAssert(testIn.Int5 == testOut.Int5, "Int5 value mismatch");
oxAssert(testIn.Int6 == testOut->Int6, "Int6 value mismatch"); oxAssert(testIn.Int6 == testOut.Int6, "Int6 value mismatch");
oxAssert(testIn.Int7 == testOut->Int7, "Int7 value mismatch"); oxAssert(testIn.Int7 == testOut.Int7, "Int7 value mismatch");
oxAssert(testIn.Int8 == testOut->Int8, "Int8 value mismatch"); oxAssert(testIn.Int8 == testOut.Int8, "Int8 value mismatch");
oxAssert(ox_strcmp(testIn.CString, testOut->CString) == 0, "CString 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.Union.Int == testOut.Union.Int, "Union.Int value mismatch");
oxAssert(testIn.String == testOut->String, "String value mismatch"); oxAssert(testIn.String == testOut.String, "String value mismatch");
oxAssert(testIn.List[0] == testOut->List[0], "List[0] 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[1] == testOut.List[1], "List[1] value mismatch");
oxAssert(testIn.List[2] == testOut->List[2], "List[2] 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.List[3] == testOut.List[3], "List[3] value mismatch");
oxAssert(testIn.Map["asdf"] == testOut->Map["asdf"], "Map[\"asdf\"] 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.Map["aoeu"] == testOut.Map["aoeu"], "Map[\"aoeu\"] value mismatch");
oxAssert(testIn.EmptyStruct.Bool == testOut->EmptyStruct.Bool, "EmptyStruct.Bool 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.Int == testOut.EmptyStruct.Int, "EmptyStruct.Int value mismatch");
oxAssert(testIn.EmptyStruct.String == testOut->EmptyStruct.String, "EmptyStruct.String 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.Int == testOut.Struct.Int, "Struct.Int value mismatch");
oxAssert(testIn.Struct.String == testOut->Struct.String, "Struct.String 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.Struct.Bool == testOut.Struct.Bool, "Struct.Bool value mismatch");
return OxError(0); return OxError(0);
} }