[ox] Remove need for explicit fields count in model system, add ox::String handler to OC

This commit is contained in:
Gary Talent 2021-07-24 20:40:11 -05:00
parent 9418f54ebc
commit 2e9b7fe871
6 changed files with 93 additions and 7 deletions

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <ox/model/fieldcounter.hpp>
#include <ox/model/optype.hpp> #include <ox/model/optype.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
#include <ox/std/byteswap.hpp> #include <ox/std/byteswap.hpp>
@ -87,7 +88,7 @@ class MetalClawReader {
StringLength stringLength(const char *name); StringLength stringLength(const char *name);
template<typename T = std::nullptr_t> 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. * Returns a MetalClawReader to parse a child object.

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <ox/model/fieldcounter.hpp>
#include <ox/model/optype.hpp> #include <ox/model/optype.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
#include <ox/std/bit.hpp> #include <ox/std/bit.hpp>
@ -71,7 +72,7 @@ class MetalClawWriter {
Error field(const char*, UnionView<U> val); Error field(const char*, UnionView<U> val);
template<typename T = std::nullptr_t> 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; std::size_t size() noexcept;

61
deps/ox/src/ox/model/fieldcounter.hpp vendored Normal file
View 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;
}
}

View File

@ -11,6 +11,7 @@
#include "descread.hpp" #include "descread.hpp"
#include "desctypes.hpp" #include "desctypes.hpp"
#include "descwrite.hpp" #include "descwrite.hpp"
#include "fieldcounter.hpp"
#include "modelops.hpp" #include "modelops.hpp"
#include "types.hpp" #include "types.hpp"
#include "walk.hpp" #include "walk.hpp"

View File

@ -10,6 +10,7 @@
#include <json/json.h> #include <json/json.h>
#include <ox/model/fieldcounter.hpp>
#include <ox/model/optype.hpp> #include <ox/model/optype.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
#include <ox/std/buffer.hpp> #include <ox/std/buffer.hpp>
@ -35,7 +36,7 @@ class OrganicClawReader {
OrganicClawReader(const char *json, std::size_t buffSize); 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, int8_t *val);
Error field(const char *key, int16_t *val); Error field(const char *key, int16_t *val);
@ -63,7 +64,10 @@ class OrganicClawReader {
Error field(const char *key, UnionView<U> val); Error field(const char *key, UnionView<U> val);
template<std::size_t L> 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); Error field(const char *key, SerStr val);
@ -80,7 +84,7 @@ class OrganicClawReader {
std::size_t stringLength(const char *name); std::size_t stringLength(const char *name);
template<typename T = void> 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> 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())); return field(name, SerStr(val->data(), val->cap()));
} }

View File

@ -10,6 +10,7 @@
#include <json/json.h> #include <json/json.h>
#include <ox/model/fieldcounter.hpp>
#include <ox/model/optype.hpp> #include <ox/model/optype.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
#include <ox/std/buffer.hpp> #include <ox/std/buffer.hpp>
@ -65,7 +66,7 @@ class OrganicClawWriter {
Error field(const char*, T *val); Error field(const char*, T *val);
template<typename T = void> 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() { static constexpr auto opType() {