[ox] Cleanup
This commit is contained in:
parent
47a6a410c4
commit
63486c23d4
105
deps/ox/src/ox/claw/read.cpp
vendored
105
deps/ox/src/ox/claw/read.cpp
vendored
@ -12,34 +12,66 @@
|
||||
|
||||
namespace ox {
|
||||
|
||||
Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcept {
|
||||
const auto s1End = ox::strchr(buff, ';', buffLen);
|
||||
ox::Result<ox::StringView> readClawTypeId(ox::BufferView buff) noexcept {
|
||||
auto buffRaw = buff.data();
|
||||
auto buffLen = buff.size();
|
||||
size_t outSz{};
|
||||
const auto s1End = ox::strchr(buffRaw, ';', buffLen);
|
||||
if (!s1End) {
|
||||
return OxError(1, "Could not read Claw header");
|
||||
}
|
||||
const auto s1Size = static_cast<std::size_t>(s1End - buff);
|
||||
const String fmt(buff, s1Size);
|
||||
buff += s1Size + 1;
|
||||
buffLen -= s1Size + 1;
|
||||
|
||||
const auto s2End = ox::strchr(buff, ';', buffLen);
|
||||
auto const fmtSz = static_cast<std::size_t>(s1End - buffRaw) + 1;
|
||||
buffRaw += fmtSz;
|
||||
buffLen -= fmtSz;
|
||||
outSz += fmtSz;
|
||||
auto const s2End = ox::strchr(buffRaw, ';', buffLen);
|
||||
if (!s2End) {
|
||||
return OxError(2, "Could not read Claw header");
|
||||
}
|
||||
const auto s2Size = static_cast<std::size_t>(s2End - buff);
|
||||
const String typeName(buff, s2Size);
|
||||
buff += s2Size + 1;
|
||||
buffLen -= s2Size + 1;
|
||||
|
||||
const auto s3End = ox::strchr(buff, ';', buffLen);
|
||||
auto const s2Size = static_cast<std::size_t>(s2End - buffRaw) + 1;
|
||||
buffRaw += s2Size;
|
||||
buffLen -= s2Size;
|
||||
outSz += s2Size;
|
||||
auto const s3End = ox::strchr(buffRaw, ';', buffLen) + 1;
|
||||
if (!s3End) {
|
||||
return OxError(3, "Could not read Claw header");
|
||||
}
|
||||
const auto s3Size = static_cast<std::size_t>(s3End - buff);
|
||||
const String versionStr(buff, s3Size);
|
||||
buff += s3Size + 1;
|
||||
buffLen -= s3Size + 1;
|
||||
auto const s3Size = static_cast<std::size_t>(s3End - buffRaw);
|
||||
buffRaw += s3Size;
|
||||
buffLen -= s3Size;
|
||||
outSz += s3Size;
|
||||
return {{buff.data() + fmtSz, outSz - fmtSz - 1}};
|
||||
}
|
||||
|
||||
Result<ClawHeader> readClawHeader(ox::BufferView buff) noexcept {
|
||||
auto buffRaw = buff.data();
|
||||
auto buffLen = buff.size();
|
||||
const auto s1End = ox::strchr(buffRaw, ';', buffLen);
|
||||
if (!s1End) {
|
||||
return OxError(1, "Could not read Claw header");
|
||||
}
|
||||
auto const s1Size = static_cast<std::size_t>(s1End - buffRaw);
|
||||
StringView const fmt(buffRaw, s1Size);
|
||||
buffRaw += s1Size + 1;
|
||||
buffLen -= s1Size + 1;
|
||||
|
||||
auto const s2End = ox::strchr(buffRaw, ';', buffLen);
|
||||
if (!s2End) {
|
||||
return OxError(2, "Could not read Claw header");
|
||||
}
|
||||
auto const s2Size = static_cast<std::size_t>(s2End - buffRaw);
|
||||
StringView const typeName(buffRaw, s2Size);
|
||||
buffRaw += s2Size + 1;
|
||||
buffLen -= s2Size + 1;
|
||||
|
||||
auto const s3End = ox::strchr(buffRaw, ';', buffLen);
|
||||
if (!s3End) {
|
||||
return OxError(3, "Could not read Claw header");
|
||||
}
|
||||
auto const s3Size = static_cast<std::size_t>(s3End - buffRaw);
|
||||
StringView const versionStr(buffRaw, s3Size);
|
||||
buffRaw += s3Size + 1;
|
||||
buffLen -= s3Size + 1;
|
||||
ClawHeader hdr;
|
||||
if (fmt == "M2") {
|
||||
hdr.fmt = ClawFormat::Metal;
|
||||
@ -49,32 +81,21 @@ Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcep
|
||||
return OxError(4, "Claw format does not match any supported format/version combo");
|
||||
}
|
||||
hdr.typeName = typeName;
|
||||
if (auto r = ox::atoi(versionStr.c_str()); r.error == 0) {
|
||||
hdr.typeVersion = r.value;
|
||||
}
|
||||
hdr.data = buff;
|
||||
std::ignore = ox::atoi(versionStr).copyTo(hdr.typeVersion);
|
||||
hdr.data = buffRaw;
|
||||
hdr.dataSize = buffLen;
|
||||
return hdr;
|
||||
}
|
||||
|
||||
Result<ClawHeader> readClawHeader(const ox::Buffer &buff) noexcept {
|
||||
return readClawHeader(buff.data(), buff.size());
|
||||
Result<BufferView> stripClawHeader(ox::BufferView buff) noexcept {
|
||||
oxRequire(header, readClawHeader(buff));
|
||||
return {{header.data, header.dataSize}};
|
||||
}
|
||||
|
||||
Result<Buffer> stripClawHeader(const char *buff, std::size_t buffLen) noexcept {
|
||||
oxRequire(header, readClawHeader(buff, buffLen));
|
||||
Buffer out(header.dataSize);
|
||||
ox::listcpy(out.data(), header.data, out.size());
|
||||
return out;
|
||||
}
|
||||
|
||||
Result<Buffer> stripClawHeader(const ox::Buffer &buff) noexcept {
|
||||
return stripClawHeader(buff.data(), buff.size());
|
||||
}
|
||||
|
||||
Result<ModelObject> readClaw(TypeStore &ts, const char *buff, std::size_t buffSz) noexcept {
|
||||
oxRequire(header, readClawHeader(buff, buffSz));
|
||||
auto const [t, tdErr] = ts.getLoad(header.typeName, header.typeVersion, header.typeParams);
|
||||
Result<ModelObject> readClaw(TypeStore &ts, BufferView buff) noexcept {
|
||||
oxRequire(header, readClawHeader(buff));
|
||||
auto const [t, tdErr] = ts.getLoad(
|
||||
header.typeName, header.typeVersion, header.typeParams);
|
||||
if (tdErr) {
|
||||
return OxError(3, "Could not load type descriptor");
|
||||
}
|
||||
@ -83,7 +104,7 @@ Result<ModelObject> readClaw(TypeStore &ts, const char *buff, std::size_t buffSz
|
||||
switch (header.fmt) {
|
||||
case ClawFormat::Metal:
|
||||
{
|
||||
ox::BufferReader br(header.data, header.dataSize);
|
||||
ox::BufferReader br({header.data, header.dataSize});
|
||||
MetalClawReader reader(br);
|
||||
ModelHandlerInterface handler(&reader);
|
||||
oxReturnError(model(&handler, &obj));
|
||||
@ -92,7 +113,7 @@ Result<ModelObject> readClaw(TypeStore &ts, const char *buff, std::size_t buffSz
|
||||
case ClawFormat::Organic:
|
||||
{
|
||||
#ifdef OX_USE_STDLIB
|
||||
OrganicClawReader reader(header.data, header.dataSize);
|
||||
OrganicClawReader reader({header.data, header.dataSize});
|
||||
ModelHandlerInterface handler(&reader);
|
||||
oxReturnError(model(&handler, &obj));
|
||||
return obj;
|
||||
@ -106,8 +127,4 @@ Result<ModelObject> readClaw(TypeStore &ts, const char *buff, std::size_t buffSz
|
||||
return OxError(1);
|
||||
}
|
||||
|
||||
Result<ModelObject> readClaw(TypeStore &ts, const Buffer &buff) noexcept {
|
||||
return readClaw(ts, buff.data(), buff.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
39
deps/ox/src/ox/claw/read.hpp
vendored
39
deps/ox/src/ox/claw/read.hpp
vendored
@ -8,6 +8,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ox/std/span.hpp>
|
||||
#include <ox/mc/read.hpp>
|
||||
#ifdef OX_USE_STDLIB
|
||||
#include <ox/oc/read.hpp>
|
||||
@ -31,17 +32,15 @@ struct ClawHeader {
|
||||
std::size_t dataSize = 0;
|
||||
};
|
||||
|
||||
Result<ClawHeader> readClawHeader(const char *buff, std::size_t buffLen) noexcept;
|
||||
ox::Result<ox::StringView> readClawTypeId(ox::BufferView buff) noexcept;
|
||||
|
||||
Result<ClawHeader> readClawHeader(const ox::Buffer &buff) noexcept;
|
||||
Result<ClawHeader> readClawHeader(ox::BufferView buff) noexcept;
|
||||
|
||||
Result<Buffer> stripClawHeader(const char *buff, std::size_t buffLen) noexcept;
|
||||
|
||||
Result<Buffer> stripClawHeader(const ox::Buffer &buff) noexcept;
|
||||
Result<BufferView> stripClawHeader(ox::BufferView buff) noexcept;
|
||||
|
||||
template<typename T>
|
||||
Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
||||
oxRequire(header, readClawHeader(buff, buffLen));
|
||||
Error readClaw(ox::BufferView buff, T &val) {
|
||||
oxRequire(header, readClawHeader(buff));
|
||||
if (header.typeName != getModelTypeName<T>()) {
|
||||
return OxError(Error_ClawTypeMismatch, "Claw Read: Type mismatch");
|
||||
}
|
||||
@ -51,16 +50,16 @@ Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
||||
switch (header.fmt) {
|
||||
case ClawFormat::Metal:
|
||||
{
|
||||
ox::BufferReader br(header.data, header.dataSize);
|
||||
ox::BufferReader br({header.data, header.dataSize});
|
||||
MetalClawReader reader(br);
|
||||
ModelHandlerInterface handler(&reader);
|
||||
return model(&handler, val);
|
||||
return model(&handler, &val);
|
||||
}
|
||||
case ClawFormat::Organic:
|
||||
{
|
||||
#ifdef OX_USE_STDLIB
|
||||
OrganicClawReader reader(header.data, header.dataSize);
|
||||
return model(&reader, val);
|
||||
return model(&reader, &val);
|
||||
#else
|
||||
break;
|
||||
#endif
|
||||
@ -72,24 +71,12 @@ Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> readClaw(const char *buff, std::size_t buffLen) {
|
||||
T val;
|
||||
oxReturnError(readClaw(buff, buffLen, &val));
|
||||
Result<T> readClaw(ox::BufferView buff) {
|
||||
Result<T> val;
|
||||
oxReturnError(readClaw(buff, val.value));
|
||||
return val;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Error readClaw(const Buffer &buff, T *val) {
|
||||
return readClaw<T>(buff.data(), buff.size(), val);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> readClaw(const Buffer &buff) {
|
||||
return readClaw<T>(buff.data(), buff.size());
|
||||
}
|
||||
|
||||
Result<ModelObject> readClaw(TypeStore &ts, const char *buff, std::size_t buffSz) noexcept;
|
||||
|
||||
Result<ModelObject> readClaw(TypeStore &ts, const Buffer &buff) noexcept;
|
||||
Result<ModelObject> readClaw(TypeStore &ts, BufferView buff) noexcept;
|
||||
|
||||
}
|
||||
|
1
deps/ox/src/ox/claw/test/CMakeLists.txt
vendored
1
deps/ox/src/ox/claw/test/CMakeLists.txt
vendored
@ -10,5 +10,6 @@ target_link_libraries(
|
||||
|
||||
add_test("[ox/claw] ClawHeaderReader" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ClawTest ClawHeaderReader)
|
||||
add_test("[ox/claw] ClawHeaderReader2" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ClawTest ClawHeaderReader2)
|
||||
add_test("[ox/claw] ClawHeaderReadTypeId" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ClawTest ClawHeaderReadTypeId)
|
||||
add_test("[ox/claw] ClawWriter" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ClawTest ClawWriter)
|
||||
add_test("[ox/claw] ClawReader" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ClawTest ClawReader)
|
||||
|
16
deps/ox/src/ox/claw/test/tests.cpp
vendored
16
deps/ox/src/ox/claw/test/tests.cpp
vendored
@ -109,7 +109,7 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
"ClawHeaderReader",
|
||||
[] {
|
||||
constexpr auto hdr = ox::StringLiteral("O1;com.drinkingtea.ox.claw.test.Header;2;");
|
||||
auto [ch, err] = ox::readClawHeader(hdr.c_str(), hdr.len() + 1);
|
||||
auto [ch, err] = ox::readClawHeader({hdr.c_str(), hdr.len() + 1});
|
||||
oxAssert(err, "Error parsing header");
|
||||
oxAssert(ch.fmt == ox::ClawFormat::Organic, "Format wrong");
|
||||
oxAssert(ch.typeName == "com.drinkingtea.ox.claw.test.Header", "Type name wrong");
|
||||
@ -121,7 +121,7 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
"ClawHeaderReader2",
|
||||
[] {
|
||||
constexpr auto hdr = ox::StringLiteral("M2;com.drinkingtea.ox.claw.test.Header2;3;");
|
||||
auto [ch, err] = ox::readClawHeader(hdr.c_str(), hdr.len() + 1);
|
||||
auto [ch, err] = ox::readClawHeader({hdr.c_str(), hdr.len() + 1});
|
||||
oxAssert(err, "Error parsing header");
|
||||
oxAssert(ch.fmt == ox::ClawFormat::Metal, "Format wrong");
|
||||
oxAssert(ch.typeName == "com.drinkingtea.ox.claw.test.Header2", "Type name wrong");
|
||||
@ -129,6 +129,16 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
{
|
||||
"ClawHeaderReadTypeId",
|
||||
[] {
|
||||
constexpr auto hdr = ox::StringLiteral("M2;com.drinkingtea.ox.claw.test.Header2;3;awefawf");
|
||||
constexpr auto expected = ox::StringLiteral("com.drinkingtea.ox.claw.test.Header2;3");
|
||||
oxRequire(actual, ox::readClawTypeId({hdr.data(), hdr.len() + 1}));
|
||||
oxExpect(actual, expected);
|
||||
return ox::Error{};
|
||||
}
|
||||
},
|
||||
{
|
||||
"ClawWriter",
|
||||
[] {
|
||||
@ -156,7 +166,7 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
testIn.Struct.String = "Test String 2";
|
||||
const auto [buff, err] = ox::writeMC(testIn);
|
||||
oxAssert(err, "writeClaw failed");
|
||||
oxAssert(ox::readMC(buff.data(), buff.size(), &testOut), "readClaw failed");
|
||||
oxAssert(ox::readMC(buff, testOut), "readClaw failed");
|
||||
//std::cout << testIn.Union.Int << "|" << testOut.Union.Int << "|\n";
|
||||
oxAssert(testIn.Bool == testOut.Bool, "Bool value mismatch");
|
||||
oxAssert(testIn.Int == testOut.Int, "Int value mismatch");
|
||||
|
2
deps/ox/src/ox/mc/intops.hpp
vendored
2
deps/ox/src/ox/mc/intops.hpp
vendored
@ -185,7 +185,7 @@ constexpr Result<I> decodeInteger(Reader_c auto&rdr, std::size_t *bytesRead) noe
|
||||
template<typename I>
|
||||
Result<I> decodeInteger(McInt m) noexcept {
|
||||
std::size_t bytesRead{};
|
||||
BufferReader br(reinterpret_cast<const char*>(m.data), 9);
|
||||
BufferReader br({reinterpret_cast<const char*>(m.data), 9});
|
||||
return decodeInteger<I>(br, &bytesRead);
|
||||
}
|
||||
|
||||
|
17
deps/ox/src/ox/mc/read.hpp
vendored
17
deps/ox/src/ox/mc/read.hpp
vendored
@ -524,25 +524,20 @@ constexpr void MetalClawReaderTemplate<Reader>::nextField() noexcept {
|
||||
using MetalClawReader = MetalClawReaderTemplate<ox::BufferReader>;
|
||||
|
||||
template<typename T>
|
||||
Error readMC(const char *buff, std::size_t buffLen, T *val) noexcept {
|
||||
BufferReader br(buff, buffLen);
|
||||
Error readMC(ox::BufferView buff, T &val) noexcept {
|
||||
BufferReader br(buff);
|
||||
MetalClawReader reader(br);
|
||||
ModelHandlerInterface<MetalClawReader, ox::OpType::Read> handler(&reader);
|
||||
return model(&handler, val);
|
||||
return model(&handler, &val);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> readMC(const char *buff, std::size_t buffLen) noexcept {
|
||||
T val;
|
||||
oxReturnError(readMC(buff, buffLen, &val));
|
||||
Result<T> readMC(ox::BufferView buff) noexcept {
|
||||
Result<T> val;
|
||||
oxReturnError(readMC(buff, val.value));
|
||||
return val;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> readMC(const Buffer &buff) noexcept {
|
||||
return readMC<T>(buff.data(), buff.size());
|
||||
}
|
||||
|
||||
extern template class ModelHandlerInterface<MetalClawReaderTemplate<BufferReader>>;
|
||||
|
||||
}
|
||||
|
6
deps/ox/src/ox/mc/test/tests.cpp
vendored
6
deps/ox/src/ox/mc/test/tests.cpp
vendored
@ -143,7 +143,7 @@ std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
// run tests
|
||||
const auto [buff, err] = ox::writeMC(testIn);
|
||||
oxAssert(err, "writeMC failed");
|
||||
oxAssert(ox::readMC(buff.data(), buff.size(), &testOut), "readMC failed");
|
||||
oxAssert(ox::readMC(buff, testOut), "readMC failed");
|
||||
//std::cout << testIn.Union.Int << "|" << testOut.Union.Int << "|\n";
|
||||
oxAssert(testIn.Bool == testOut.Bool, "Bool value mismatch");
|
||||
oxAssert(testIn.Int == testOut.Int, "Int value mismatch");
|
||||
@ -319,7 +319,7 @@ std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
oxAssert(typeErr, "Descriptor write failed");
|
||||
ox::ModelObject testOut;
|
||||
oxReturnError(testOut.setType(type));
|
||||
oxAssert(ox::readMC(dataBuff.data(), dataBuff.size(), &testOut), "Data read failed");
|
||||
oxAssert(ox::readMC(dataBuff, testOut), "Data read failed");
|
||||
oxAssert(testOut.at("Int").unwrap()->get<int>() == testIn.Int, "testOut.Int failed");
|
||||
oxAssert(testOut.at("Bool").unwrap()->get<bool>() == testIn.Bool, "testOut.Bool failed");
|
||||
oxAssert(testOut.at("IString").unwrap()->get<ox::String>() == testIn.IString.c_str(), "testOut.String failed");
|
||||
@ -369,7 +369,7 @@ std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
ox::TypeStore typeStore;
|
||||
const auto [type, typeErr] = ox::buildTypeDef(&typeStore, &testIn);
|
||||
oxAssert(typeErr, "Descriptor write failed");
|
||||
ox::BufferReader br(dataBuff, dataBuffLen);
|
||||
ox::BufferReader br({dataBuff, dataBuffLen});
|
||||
oxReturnError(ox::walkModel<ox::MetalClawReader>(type, br,
|
||||
[](const ox::Vector<ox::FieldName>&, const ox::Vector<ox::String>&, const ox::DescriptorField &f, ox::MetalClawReader *rdr) -> ox::Error {
|
||||
//std::cout << f.fieldName.c_str() << '\n';
|
||||
|
4
deps/ox/src/ox/model/desctypes.hpp
vendored
4
deps/ox/src/ox/model/desctypes.hpp
vendored
@ -37,12 +37,12 @@ constexpr auto buildTypeId() noexcept {
|
||||
static constexpr auto buildTypeId(CRStringView name, int version,
|
||||
const TypeParamPack &typeParams = {}) noexcept {
|
||||
String tp;
|
||||
if (typeParams.size()) {
|
||||
if (!typeParams.empty()) {
|
||||
tp = "#";
|
||||
for (const auto &p : typeParams) {
|
||||
tp += p + ",";
|
||||
}
|
||||
tp = tp.substr(0, tp.len() - 1);
|
||||
tp.resize(tp.len() - 1);
|
||||
tp += "#";
|
||||
}
|
||||
return ox::sfmt("{}{};{}", name, tp, version);
|
||||
|
7
deps/ox/src/ox/model/typenamecatcher.hpp
vendored
7
deps/ox/src/ox/model/typenamecatcher.hpp
vendored
@ -97,6 +97,7 @@ struct TypeInfoCatcher {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
constexpr int getModelTypeVersion(T *val) noexcept {
|
||||
TypeInfoCatcher nc;
|
||||
std::ignore = model(&nc, val);
|
||||
@ -104,6 +105,7 @@ constexpr int getModelTypeVersion(T *val) noexcept {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
constexpr int getModelTypeVersion() noexcept {
|
||||
std::allocator<T> a;
|
||||
const auto t = a.allocate(1);
|
||||
@ -113,6 +115,7 @@ constexpr int getModelTypeVersion() noexcept {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
consteval int requireModelTypeVersion() noexcept {
|
||||
constexpr auto version = getModelTypeVersion<T>();
|
||||
static_assert(version != 0, "TypeVersion is required");
|
||||
@ -120,6 +123,7 @@ consteval int requireModelTypeVersion() noexcept {
|
||||
}
|
||||
|
||||
template<typename T, typename Str = const char*>
|
||||
[[nodiscard]]
|
||||
constexpr Str getModelTypeName(T *val) noexcept {
|
||||
TypeNameCatcher nc;
|
||||
std::ignore = model(&nc, val);
|
||||
@ -127,6 +131,7 @@ constexpr Str getModelTypeName(T *val) noexcept {
|
||||
}
|
||||
|
||||
template<typename T, typename Str = const char*>
|
||||
[[nodiscard]]
|
||||
constexpr Str getModelTypeName() noexcept {
|
||||
std::allocator<T> a;
|
||||
auto t = a.allocate(1);
|
||||
@ -136,8 +141,10 @@ constexpr Str getModelTypeName() noexcept {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
consteval auto requireModelTypeName() noexcept {
|
||||
constexpr auto name = getModelTypeName<T>();
|
||||
static_assert(ox::StringView{name}.len(), "Type lacks required TypeName");
|
||||
return name;
|
||||
}
|
||||
|
||||
|
2
deps/ox/src/ox/model/typestore.hpp
vendored
2
deps/ox/src/ox/model/typestore.hpp
vendored
@ -101,7 +101,7 @@ class TypeStore {
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual Result<UniquePtr<DescriptorType>> loadDescriptor(ox::CRStringView) noexcept {
|
||||
virtual Result<UniquePtr<DescriptorType>> loadDescriptor(ox::StringView) noexcept {
|
||||
return OxError(1);
|
||||
}
|
||||
|
||||
|
23
deps/ox/src/ox/oc/read.hpp
vendored
23
deps/ox/src/ox/oc/read.hpp
vendored
@ -246,18 +246,18 @@ Error OrganicClawReader::field(const char *key, HashMap<String, T> *val) noexcep
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
Error readOC(const char *buff, std::size_t buffSize, auto *val) noexcept {
|
||||
Error readOC(BufferView buff, auto &val) noexcept {
|
||||
// OrganicClawReader constructor can throw, but readOC should return its errors.
|
||||
try {
|
||||
Json::Value doc;
|
||||
Json::CharReaderBuilder parserBuilder;
|
||||
auto parser = UniquePtr<Json::CharReader>(parserBuilder.newCharReader());
|
||||
if (!parser->parse(buff, buff + buffSize, &doc, nullptr)) {
|
||||
if (!parser->parse(buff.data(), buff.data() + buff.size(), &doc, nullptr)) {
|
||||
return OxError(1, "Could not parse JSON");
|
||||
}
|
||||
OrganicClawReader reader(buff, buffSize);
|
||||
OrganicClawReader reader(buff.data(), buff.size());
|
||||
ModelHandlerInterface handler(&reader);
|
||||
return model(&handler, val);
|
||||
return model(&handler, &val);
|
||||
} catch (const Error &err) {
|
||||
return err;
|
||||
} catch (...) {
|
||||
@ -266,20 +266,15 @@ Error readOC(const char *buff, std::size_t buffSize, auto *val) noexcept {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> readOC(const char *json, std::size_t jsonLen) noexcept {
|
||||
T val;
|
||||
oxReturnError(readOC(json, jsonLen, &val));
|
||||
Result<T> readOC(BufferView buff) noexcept {
|
||||
Result<T> val;
|
||||
oxReturnError(readOC(buff, val.value));
|
||||
return val;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> readOC(const char *json) noexcept {
|
||||
return readOC<T>(json, ox::strlen(json));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> readOC(const Buffer &buff) noexcept {
|
||||
return readOC<T>(buff.data(), buff.size());
|
||||
Result<T> readOC(ox::StringView json) noexcept {
|
||||
return readOC<T>(ox::BufferView{json.data(), json.len()});
|
||||
}
|
||||
|
||||
}
|
||||
|
2
deps/ox/src/ox/oc/test/tests.cpp
vendored
2
deps/ox/src/ox/oc/test/tests.cpp
vendored
@ -211,7 +211,7 @@ const std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
oxAssert(type.error, "Descriptor write failed");
|
||||
ox::ModelObject testOut;
|
||||
oxReturnError(testOut.setType(type.value));
|
||||
oxAssert(ox::readOC(dataBuff.data(), dataBuff.size(), &testOut), "Data read failed");
|
||||
oxAssert(ox::readOC(dataBuff, testOut), "Data read failed");
|
||||
oxAssert(testOut.get("Int").unwrap()->get<int>() == testIn.Int, "testOut.Int failed");
|
||||
oxAssert(testOut.get("Bool").unwrap()->get<bool>() == testIn.Bool, "testOut.Bool failed");
|
||||
oxAssert(testOut.get("String").unwrap()->get<ox::String>() == testIn.String, "testOut.String failed");
|
||||
|
7
deps/ox/src/ox/std/buffer.hpp
vendored
7
deps/ox/src/ox/std/buffer.hpp
vendored
@ -10,6 +10,7 @@
|
||||
|
||||
#include "error.hpp"
|
||||
#include "reader.hpp"
|
||||
#include "span.hpp"
|
||||
#include "vector.hpp"
|
||||
#include "writer.hpp"
|
||||
|
||||
@ -18,6 +19,7 @@ namespace ox {
|
||||
extern template class Vector<char>;
|
||||
|
||||
using Buffer = Vector<char>;
|
||||
using BufferView = SpanView<char>;
|
||||
|
||||
class BufferWriter {
|
||||
private:
|
||||
@ -174,10 +176,7 @@ class BufferReader {
|
||||
char const* m_buff = nullptr;
|
||||
|
||||
public:
|
||||
constexpr explicit BufferReader(char const*buff, std::size_t sz) noexcept:
|
||||
m_size(sz), m_buff(buff) {}
|
||||
|
||||
constexpr explicit BufferReader(ox::Buffer const&buffer) noexcept:
|
||||
constexpr explicit BufferReader(ox::BufferView buffer) noexcept:
|
||||
m_size(buffer.size()), m_buff(buffer.data()) {}
|
||||
|
||||
constexpr ox::Result<char> peek() const noexcept {
|
||||
|
16
deps/ox/src/ox/std/error.hpp
vendored
16
deps/ox/src/ox/std/error.hpp
vendored
@ -234,6 +234,22 @@ struct [[nodiscard]] Result {
|
||||
return value;
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
constexpr ox::Result<U> to() & noexcept {
|
||||
if (error) [[unlikely]] {
|
||||
return error;
|
||||
}
|
||||
return U(value);
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
constexpr ox::Result<U> to() && noexcept {
|
||||
if (error) [[unlikely]] {
|
||||
return error;
|
||||
}
|
||||
return U(std::move(value));
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
constexpr ox::Result<U> to(auto const&f) & noexcept {
|
||||
if (error) [[unlikely]] {
|
||||
|
9
deps/ox/src/ox/std/fmt.hpp
vendored
9
deps/ox/src/ox/std/fmt.hpp
vendored
@ -40,12 +40,17 @@ constexpr StringView toStringView(const char *s) noexcept {
|
||||
|
||||
template<bool force = false, std::size_t size>
|
||||
constexpr StringView toStringView(const IString<size> &s) noexcept {
|
||||
return s.c_str();
|
||||
return s;
|
||||
}
|
||||
|
||||
template<bool force = false>
|
||||
constexpr StringView toStringView(ox::StringLiteral s) noexcept {
|
||||
return s;
|
||||
}
|
||||
|
||||
template<bool force = false, std::size_t size>
|
||||
constexpr StringView toStringView(const BasicString<size> &s) noexcept {
|
||||
return s.c_str();
|
||||
return s;
|
||||
}
|
||||
|
||||
#if __has_include(<string>)
|
||||
|
14
deps/ox/src/ox/std/span.hpp
vendored
14
deps/ox/src/ox/std/span.hpp
vendored
@ -19,8 +19,8 @@ template<typename T>
|
||||
class SpanView {
|
||||
|
||||
private:
|
||||
const T *m_items{};
|
||||
const std::size_t m_size{};
|
||||
T const*m_items{};
|
||||
std::size_t m_size{};
|
||||
|
||||
public:
|
||||
using value_type = T;
|
||||
@ -106,6 +106,16 @@ class SpanView {
|
||||
return m_items[i];
|
||||
}
|
||||
|
||||
constexpr SpanView operator+(size_t i) const noexcept {
|
||||
return {m_items + i, m_size - i};
|
||||
}
|
||||
|
||||
constexpr SpanView operator+=(size_t i) noexcept {
|
||||
m_items += i;
|
||||
m_size -= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr T const*data() const noexcept {
|
||||
return m_items;
|
||||
|
15
deps/ox/src/ox/std/strconv.hpp
vendored
15
deps/ox/src/ox/std/strconv.hpp
vendored
@ -1,7 +1,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "bit.hpp"
|
||||
#include "error.hpp"
|
||||
#include "math.hpp"
|
||||
#include "types.hpp"
|
||||
#include "writer.hpp"
|
||||
|
||||
@ -39,6 +41,19 @@ constexpr ox::Error writeItoa(Integer v, ox::Writer_c auto &writer) noexcept {
|
||||
return {};
|
||||
}
|
||||
|
||||
constexpr ox::Error writeF32toa(float const v, ox::Writer_c auto &writer) noexcept {
|
||||
auto const raw = std::bit_cast<uint32_t>(v);
|
||||
if (raw >> 31) {
|
||||
oxReturnError(writer.put('-'));
|
||||
}
|
||||
auto const mantissa = raw & onMask<uint32_t>(23);
|
||||
auto const exp = ((raw >> 23) & 0xff) - 126;
|
||||
auto const p = ox::pow(2u, exp);
|
||||
auto const out = p * mantissa;
|
||||
oxReturnError(writeItoa(out, writer));
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include "istring.hpp"
|
||||
|
4
deps/ox/src/ox/std/string.hpp
vendored
4
deps/ox/src/ox/std/string.hpp
vendored
@ -192,6 +192,10 @@ class BasicString {
|
||||
[[nodiscard]]
|
||||
constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
|
||||
|
||||
constexpr void resize(size_t sz) noexcept {
|
||||
m_buff.resize(sz);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const char *data() const noexcept {
|
||||
return m_buff.data();
|
||||
|
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
@ -11,6 +11,7 @@ add_test("[ox/std] ox_memcmp ABCDEFG != HIJKLMN" ${CMAKE_RUNTIME_OUTPUT_DIRECTOR
|
||||
add_test("[ox/std] ox_memcmp HIJKLMN != ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HIJKLMN != ABCDEFG")
|
||||
add_test("[ox/std] ox_memcmp ABCDEFG == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFG == ABCDEFG")
|
||||
add_test("[ox/std] ox_memcmp ABCDEFGHI == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFGHI == ABCDEFG")
|
||||
#add_test("[ox/std] ftoa" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ftoa")
|
||||
add_test("[ox/std] itoa" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "itoa")
|
||||
add_test("[ox/std] BString" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "BString")
|
||||
add_test("[ox/std] String" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "String")
|
||||
|
22
deps/ox/src/ox/std/test/tests.cpp
vendored
22
deps/ox/src/ox/std/test/tests.cpp
vendored
@ -26,6 +26,16 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
{
|
||||
"ftoa",
|
||||
[]() {
|
||||
ox::Array<char, 10> buff;
|
||||
ox::CharBuffWriter bw(buff);
|
||||
oxAssert(ox::writeF32toa(5, bw), "ox::writeItoa returned Error");
|
||||
oxExpect(ox::StringView(buff.data()), ox::StringView("5"));
|
||||
return ox::Error{};
|
||||
}
|
||||
},
|
||||
{
|
||||
"itoa",
|
||||
[]() {
|
||||
@ -109,7 +119,9 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
oxAssert(ox::StringView("Write") != ox::String(""), "String / StringView comparison broken");
|
||||
oxAssert(ox::String("Write") != ox::StringView(""), "String / StringView comparison broken");
|
||||
oxAssert(ox::String("Write") == ox::StringView("Write"), "String / StringView comparison broken");
|
||||
oxAssert(ox::String(ox::StringView("Write")) == ox::StringView("Write"), "String / StringView comparison broken");
|
||||
oxAssert(
|
||||
ox::String(ox::StringView("Write")) == ox::StringView("Write"),
|
||||
"String / StringView comparison broken");
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
@ -119,7 +131,7 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
ox::Vector<int> v;
|
||||
oxAssert(v.size() == 0, "Initial Vector size not 0");
|
||||
oxAssert(v.empty(), "Vector::empty() is broken");
|
||||
auto insertTest = [&v](int val, [[maybe_unused]] std::size_t size) {
|
||||
auto insertTest = [&v](int val, std::size_t size) {
|
||||
v.push_back(val);
|
||||
oxReturnError(OxError(v.size() != size, "Vector size incorrect"));
|
||||
oxReturnError(OxError(v[v.size() - 1] != val, "Vector value wrong"));
|
||||
@ -152,7 +164,11 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
[] {
|
||||
using BA = ox::Array<char, 4>;
|
||||
const auto actual = ox::serialize<uint32_t>(256).unwrap();
|
||||
oxOutf("[{}, {}, {}, {}]", static_cast<int>(actual[0]), static_cast<int>(actual[1]), static_cast<int>(actual[2]), static_cast<int>(actual[3]));
|
||||
oxOutf("[{}, {}, {}, {}]",
|
||||
static_cast<int>(actual[0]),
|
||||
static_cast<int>(actual[1]),
|
||||
static_cast<int>(actual[2]),
|
||||
static_cast<int>(actual[3]));
|
||||
oxExpect(ox::serialize<int32_t>(4).unwrap(), BA({4, 0, 0, 0}));
|
||||
oxExpect(ox::serialize<int32_t>(256).unwrap(), BA({0, 1, 0, 0}));
|
||||
oxExpect(ox::serialize<int32_t>(257).unwrap(), BA({1, 1, 0, 0}));
|
||||
|
Loading…
Reference in New Issue
Block a user