From 2e9b7fe87142420572d2081df9c48796959011f7 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 24 Jul 2021 20:40:11 -0500 Subject: [PATCH] [ox] Remove need for explicit fields count in model system, add ox::String handler to OC --- deps/ox/src/ox/mc/read.hpp | 3 +- deps/ox/src/ox/mc/write.hpp | 3 +- deps/ox/src/ox/model/fieldcounter.hpp | 61 +++++++++++++++++++++++++++ deps/ox/src/ox/model/model.hpp | 1 + deps/ox/src/ox/oc/read.hpp | 29 +++++++++++-- deps/ox/src/ox/oc/write.hpp | 3 +- 6 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 deps/ox/src/ox/model/fieldcounter.hpp diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index 594aa9c3..14fa7ac8 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -8,6 +8,7 @@ #pragma once +#include #include #include #include @@ -87,7 +88,7 @@ class MetalClawReader { StringLength stringLength(const char *name); template - void setTypeInfo(const char *name = T::TypeName, int fields = T::Fields); + void setTypeInfo(const char *name = T::TypeName, int fields = countFields()); /** * Returns a MetalClawReader to parse a child object. diff --git a/deps/ox/src/ox/mc/write.hpp b/deps/ox/src/ox/mc/write.hpp index 2ef7ad60..03bbe9fe 100644 --- a/deps/ox/src/ox/mc/write.hpp +++ b/deps/ox/src/ox/mc/write.hpp @@ -8,6 +8,7 @@ #pragma once +#include #include #include #include @@ -71,7 +72,7 @@ class MetalClawWriter { Error field(const char*, UnionView val); template - void setTypeInfo(const char *name = T::TypeName, int fields = T::Fields); + void setTypeInfo(const char *name = T::TypeName, int fields = countFields()); std::size_t size() noexcept; diff --git a/deps/ox/src/ox/model/fieldcounter.hpp b/deps/ox/src/ox/model/fieldcounter.hpp new file mode 100644 index 00000000..ec50ed91 --- /dev/null +++ b/deps/ox/src/ox/model/fieldcounter.hpp @@ -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 +#include + +#include "optype.hpp" + +namespace ox { + +template +class FieldCounter { + public: + int fields = 0; + + template + constexpr void setTypeInfo(const char* = U::TypeName, int = 0) { + } + + template + constexpr ox::Error field(const char*, U) noexcept { + ++fields; + return OxError(0); + } + + template + constexpr ox::Error field(const char*, U, std::size_t) noexcept { + ++fields; + return OxError(0); + } + + template + constexpr Error field(const char*, Handler) { + ++fields; + return OxError(0); + } + + static constexpr auto opType() { + return OpType::Read; + } +}; + +template +#if __cplusplus >= 202002L +consteval +#endif +int countFields() noexcept { + AllocAlias a = {}; + FieldCounter c; + oxIgnoreError(model(&c, std::bit_cast(&a))); + return c.fields; +} + +} diff --git a/deps/ox/src/ox/model/model.hpp b/deps/ox/src/ox/model/model.hpp index ca9fdd67..5dbc0c08 100644 --- a/deps/ox/src/ox/model/model.hpp +++ b/deps/ox/src/ox/model/model.hpp @@ -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" diff --git a/deps/ox/src/ox/oc/read.hpp b/deps/ox/src/ox/oc/read.hpp index 05b056d5..ab41dcac 100644 --- a/deps/ox/src/ox/oc/read.hpp +++ b/deps/ox/src/ox/oc/read.hpp @@ -10,6 +10,7 @@ #include +#include #include #include #include @@ -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 val); template - Error field(const char *key, BString *val); + Error field(const char *key, BasicString *val) noexcept; + + template + Error field(const char *key, BString *val) noexcept; Error field(const char *key, SerStr val); @@ -80,7 +84,7 @@ class OrganicClawReader { std::size_t stringLength(const char *name); template - constexpr void setTypeInfo(const char* = T::TypeName, int = T::Fields) { + constexpr void setTypeInfo(const char* = T::TypeName, int = countFields()) { } /** @@ -141,7 +145,24 @@ Error OrganicClawReader::field(const char *key, UnionView val) { } template -Error OrganicClawReader::field(const char *name, BString *val) { +Error OrganicClawReader::field(const char *key, BasicString *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 +Error OrganicClawReader::field(const char *name, BString *val) noexcept { return field(name, SerStr(val->data(), val->cap())); } diff --git a/deps/ox/src/ox/oc/write.hpp b/deps/ox/src/ox/oc/write.hpp index 0bacdb90..bd1fb622 100644 --- a/deps/ox/src/ox/oc/write.hpp +++ b/deps/ox/src/ox/oc/write.hpp @@ -10,6 +10,7 @@ #include +#include #include #include #include @@ -65,7 +66,7 @@ class OrganicClawWriter { Error field(const char*, T *val); template - constexpr void setTypeInfo(const char* = T::TypeName, int = T::Fields) { + constexpr void setTypeInfo(const char* = T::TypeName, int = countFields()) { } static constexpr auto opType() {