[ox] Remove need for explicit fields count in model system, add ox::String handler to OC
This commit is contained in:
parent
9418f54ebc
commit
2e9b7fe871
3
deps/ox/src/ox/mc/read.hpp
vendored
3
deps/ox/src/ox/mc/read.hpp
vendored
@ -8,6 +8,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ox/model/fieldcounter.hpp>
|
||||
#include <ox/model/optype.hpp>
|
||||
#include <ox/model/types.hpp>
|
||||
#include <ox/std/byteswap.hpp>
|
||||
@ -87,7 +88,7 @@ class MetalClawReader {
|
||||
StringLength stringLength(const char *name);
|
||||
|
||||
template<typename T = std::nullptr_t>
|
||||
void setTypeInfo(const char *name = T::TypeName, int fields = T::Fields);
|
||||
void setTypeInfo(const char *name = T::TypeName, int fields = countFields<T>());
|
||||
|
||||
/**
|
||||
* Returns a MetalClawReader to parse a child object.
|
||||
|
3
deps/ox/src/ox/mc/write.hpp
vendored
3
deps/ox/src/ox/mc/write.hpp
vendored
@ -8,6 +8,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ox/model/fieldcounter.hpp>
|
||||
#include <ox/model/optype.hpp>
|
||||
#include <ox/model/types.hpp>
|
||||
#include <ox/std/bit.hpp>
|
||||
@ -71,7 +72,7 @@ class MetalClawWriter {
|
||||
Error field(const char*, UnionView<U> val);
|
||||
|
||||
template<typename T = std::nullptr_t>
|
||||
void setTypeInfo(const char *name = T::TypeName, int fields = T::Fields);
|
||||
void setTypeInfo(const char *name = T::TypeName, int fields = countFields<T>());
|
||||
|
||||
std::size_t size() noexcept;
|
||||
|
||||
|
61
deps/ox/src/ox/model/fieldcounter.hpp
vendored
Normal file
61
deps/ox/src/ox/model/fieldcounter.hpp
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2015 - 2021 gary@drinkingtea.net
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ox/std/bit.hpp>
|
||||
#include <ox/std/error.hpp>
|
||||
|
||||
#include "optype.hpp"
|
||||
|
||||
namespace ox {
|
||||
|
||||
template<typename T>
|
||||
class FieldCounter {
|
||||
public:
|
||||
int fields = 0;
|
||||
|
||||
template<typename U = std::nullptr_t>
|
||||
constexpr void setTypeInfo(const char* = U::TypeName, int = 0) {
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
constexpr ox::Error field(const char*, U) noexcept {
|
||||
++fields;
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
constexpr ox::Error field(const char*, U, std::size_t) noexcept {
|
||||
++fields;
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
template<typename U, typename Handler>
|
||||
constexpr Error field(const char*, Handler) {
|
||||
++fields;
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
static constexpr auto opType() {
|
||||
return OpType::Read;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
#if __cplusplus >= 202002L
|
||||
consteval
|
||||
#endif
|
||||
int countFields() noexcept {
|
||||
AllocAlias<T> a = {};
|
||||
FieldCounter<T> c;
|
||||
oxIgnoreError(model(&c, std::bit_cast<T*>(&a)));
|
||||
return c.fields;
|
||||
}
|
||||
|
||||
}
|
1
deps/ox/src/ox/model/model.hpp
vendored
1
deps/ox/src/ox/model/model.hpp
vendored
@ -11,6 +11,7 @@
|
||||
#include "descread.hpp"
|
||||
#include "desctypes.hpp"
|
||||
#include "descwrite.hpp"
|
||||
#include "fieldcounter.hpp"
|
||||
#include "modelops.hpp"
|
||||
#include "types.hpp"
|
||||
#include "walk.hpp"
|
||||
|
29
deps/ox/src/ox/oc/read.hpp
vendored
29
deps/ox/src/ox/oc/read.hpp
vendored
@ -10,6 +10,7 @@
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
#include <ox/model/fieldcounter.hpp>
|
||||
#include <ox/model/optype.hpp>
|
||||
#include <ox/model/types.hpp>
|
||||
#include <ox/std/buffer.hpp>
|
||||
@ -35,7 +36,7 @@ class OrganicClawReader {
|
||||
|
||||
OrganicClawReader(const char *json, std::size_t buffSize);
|
||||
|
||||
OrganicClawReader(const Json::Value &json, int unionIdx = -1);
|
||||
explicit OrganicClawReader(const Json::Value &json, int unionIdx = -1);
|
||||
|
||||
Error field(const char *key, int8_t *val);
|
||||
Error field(const char *key, int16_t *val);
|
||||
@ -63,7 +64,10 @@ class OrganicClawReader {
|
||||
Error field(const char *key, UnionView<U> val);
|
||||
|
||||
template<std::size_t L>
|
||||
Error field(const char *key, BString<L> *val);
|
||||
Error field(const char *key, BasicString<L> *val) noexcept;
|
||||
|
||||
template<std::size_t L>
|
||||
Error field(const char *key, BString<L> *val) noexcept;
|
||||
|
||||
Error field(const char *key, SerStr val);
|
||||
|
||||
@ -80,7 +84,7 @@ class OrganicClawReader {
|
||||
std::size_t stringLength(const char *name);
|
||||
|
||||
template<typename T = void>
|
||||
constexpr void setTypeInfo(const char* = T::TypeName, int = T::Fields) {
|
||||
constexpr void setTypeInfo(const char* = T::TypeName, int = countFields<T>()) {
|
||||
}
|
||||
|
||||
/**
|
||||
@ -141,7 +145,24 @@ Error OrganicClawReader::field(const char *key, UnionView<U> val) {
|
||||
}
|
||||
|
||||
template<std::size_t L>
|
||||
Error OrganicClawReader::field(const char *name, BString<L> *val) {
|
||||
Error OrganicClawReader::field(const char *key, BasicString<L> *val) noexcept {
|
||||
auto err = OxError(0);
|
||||
if (targetValid()) {
|
||||
const auto &jv = value(key);
|
||||
if (jv.empty()) {
|
||||
*val = 0;
|
||||
} else if (jv.isString()) {
|
||||
*val = jv.asString().c_str();
|
||||
} else {
|
||||
err = OxError(1, "Type mismatch");
|
||||
}
|
||||
}
|
||||
++m_fieldIt;
|
||||
return err;
|
||||
}
|
||||
|
||||
template<std::size_t L>
|
||||
Error OrganicClawReader::field(const char *name, BString<L> *val) noexcept {
|
||||
return field(name, SerStr(val->data(), val->cap()));
|
||||
}
|
||||
|
||||
|
3
deps/ox/src/ox/oc/write.hpp
vendored
3
deps/ox/src/ox/oc/write.hpp
vendored
@ -10,6 +10,7 @@
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
#include <ox/model/fieldcounter.hpp>
|
||||
#include <ox/model/optype.hpp>
|
||||
#include <ox/model/types.hpp>
|
||||
#include <ox/std/buffer.hpp>
|
||||
@ -65,7 +66,7 @@ class OrganicClawWriter {
|
||||
Error field(const char*, T *val);
|
||||
|
||||
template<typename T = void>
|
||||
constexpr void setTypeInfo(const char* = T::TypeName, int = T::Fields) {
|
||||
constexpr void setTypeInfo(const char* = T::TypeName, int = countFields<T>()) {
|
||||
}
|
||||
|
||||
static constexpr auto opType() {
|
||||
|
Loading…
Reference in New Issue
Block a user