[ox] Add Result<T> read{OC,MC,Claw}<T>(...)
This commit is contained in:
parent
4540b410dc
commit
58dbda48c4
12
deps/ox/src/ox/claw/read.hpp
vendored
12
deps/ox/src/ox/claw/read.hpp
vendored
@ -58,4 +58,16 @@ Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
12
deps/ox/src/ox/mc/read.hpp
vendored
12
deps/ox/src/ox/mc/read.hpp
vendored
@ -277,4 +277,16 @@ Error readMC(const uint8_t *buff, std::size_t buffLen, T *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());
|
||||
}
|
||||
|
||||
}
|
||||
|
22
deps/ox/src/ox/oc/read.hpp
vendored
22
deps/ox/src/ox/oc/read.hpp
vendored
@ -173,7 +173,7 @@ Error OrganicClawReader::field(const char *key, HashMap<String, T> *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<typename T>
|
||||
Result<ox::UniquePtr<T>> readOC(const char *json) {
|
||||
auto val = ox::make_unique<T>();
|
||||
oxReturnError(readOC(json, ox_strlen(json), val.get()));
|
||||
return val;
|
||||
Result<T> readOC(const char *json, std::size_t jsonLen) {
|
||||
T val;
|
||||
oxReturnError(readOC(json, jsonLen, &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());
|
||||
}
|
||||
|
||||
}
|
||||
|
50
deps/ox/src/ox/oc/test/tests.cpp
vendored
50
deps/ox/src/ox/oc/test/tests.cpp
vendored
@ -132,31 +132,31 @@ std::map<std::string, ox::Error(*)()> tests = {
|
||||
auto [testOut, readErr] = ox::readOC<TestStruct>(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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user