From 4f4ec089fd245013a66acd50ef260114700814d0 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 16 Jun 2020 04:40:43 -0500 Subject: [PATCH] [ox/model] Fix model descriptor writing to handle Vector, etc. --- deps/ox/src/ox/model/desctypes.hpp | 40 +++++++++++++++++------------- deps/ox/src/ox/model/descwrite.cpp | 8 +++++- deps/ox/src/ox/model/descwrite.hpp | 14 +++++++++-- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/deps/ox/src/ox/model/desctypes.hpp b/deps/ox/src/ox/model/desctypes.hpp index c54ec669..4ff799af 100644 --- a/deps/ox/src/ox/model/desctypes.hpp +++ b/deps/ox/src/ox/model/desctypes.hpp @@ -8,11 +8,14 @@ #pragma once +#include #include #include #include #include +#include "types.hpp" + namespace ox { using ModelString = BString<100>; @@ -32,8 +35,10 @@ enum class PrimitiveType: uint8_t { struct DescriptorField { // order of fields matters + static constexpr auto TypeVersion = 1; + // only serialize type name if type has already been serialized - const struct DescriptorType *type = nullptr; + struct DescriptorType *type = nullptr; FieldName fieldName; int subscriptLevels = 0; @@ -54,7 +59,7 @@ struct DescriptorField { ownsType = false; // is copy, only owns type if move } - constexpr DescriptorField(const DescriptorType *type, const FieldName &fieldName, int subscriptLevels, const TypeName &typeName, bool ownsType) noexcept { + constexpr DescriptorField(DescriptorType *type, const FieldName &fieldName, int subscriptLevels, const TypeName &typeName, bool ownsType) noexcept { this->type = type; this->fieldName = fieldName; this->subscriptLevels = subscriptLevels; @@ -99,6 +104,7 @@ struct DescriptorField { using FieldList = Vector; struct DescriptorType { + static constexpr auto TypeVersion = 1; TypeName typeName; PrimitiveType primitiveType; // fieldList only applies to structs @@ -120,14 +126,13 @@ struct DescriptorType { template Error model(T *io, DescriptorType *type) { - auto err = OxError(0); - io->setTypeInfo("ox::DescriptorType", 4); - err |= io->field("typeName", &type->typeName); - err |= io->field("primitiveType", &type->primitiveType); - err |= io->field("fieldList", &type->fieldList); - err |= io->field("length", &type->length); - err |= io->field("preloadable", &type->preloadable); - return err; + io->template setTypeInfo("net.drinkingtea.ox.DescriptorType", 5); + oxReturnError(io->field("typeName", &type->typeName)); + oxReturnError(io->field("primitiveType", bit_cast(&type->primitiveType))); + oxReturnError(io->field("fieldList", &type->fieldList)); + oxReturnError(io->field("length", &type->length)); + oxReturnError(io->field("preloadable", &type->preloadable)); + return OxError(0); } template @@ -135,16 +140,17 @@ Error modelWrite(T *io, DescriptorField *field) { auto err = OxError(0); io->setTypeInfo("ox::DescriptorField", 4); if (field->ownsType) { - err |= io->field("typeName", ""); - err |= io->field("type", field->type); + BString<2> empty = ""; + oxReturnError(io->field("typeName", SerStr(&empty))); + oxReturnError(io->field("type", field->type)); } else { - err |= io->field("typeName", &field->type->typeName); - err |= io->field("type", static_casttype)>(nullptr)); + oxReturnError(io->field("typeName", SerStr(&field->type->typeName))); + oxReturnError(io->field("type", static_casttype)>(nullptr))); } - err |= io->field("fieldName", &field->fieldName); + oxReturnError(io->field("fieldName", &field->fieldName)); // defaultValue is unused now, but leave placeholder for backwards compatibility - const int DefaultValue = 0; - err |= io->field("defaultValue", &DefaultValue); + int DefaultValue = 0; + oxReturnError(io->field("defaultValue", &DefaultValue)); return err; } diff --git a/deps/ox/src/ox/model/descwrite.cpp b/deps/ox/src/ox/model/descwrite.cpp index 0d000856..05650a3c 100644 --- a/deps/ox/src/ox/model/descwrite.cpp +++ b/deps/ox/src/ox/model/descwrite.cpp @@ -116,7 +116,7 @@ DescriptorType *TypeDescWriter::type(uint64_t*, bool *alreadyExisted) { return getType(TypeName, PT, Bytes, alreadyExisted); } -DescriptorType *TypeDescWriter::type(const char*, bool *alreadyExisted) { +DescriptorType *TypeDescWriter::type(char*, bool *alreadyExisted) { constexpr auto TypeName = "B:string"; constexpr auto PT = PrimitiveType::String; return getType(TypeName, PT, 0, alreadyExisted); @@ -128,6 +128,12 @@ DescriptorType *TypeDescWriter::type(SerStr, bool *alreadyExisted) { return getType(TypeName, PT, 0, alreadyExisted); } +DescriptorType *TypeDescWriter::type(String*, bool *alreadyExisted) { + constexpr auto TypeName = "B:string"; + constexpr auto PT = PrimitiveType::String; + return getType(TypeName, PT, 0, alreadyExisted); +} + DescriptorType *TypeDescWriter::type(bool*, bool *alreadyExisted) { constexpr auto TypeName = "B:bool"; constexpr auto PT = PrimitiveType::Bool; diff --git a/deps/ox/src/ox/model/descwrite.hpp b/deps/ox/src/ox/model/descwrite.hpp index c3e9227d..459370a5 100644 --- a/deps/ox/src/ox/model/descwrite.hpp +++ b/deps/ox/src/ox/model/descwrite.hpp @@ -9,8 +9,8 @@ #pragma once #include -#include #include +#include #include #include #include @@ -117,16 +117,21 @@ class TypeDescWriter { DescriptorType *type(bool *val, bool *alreadyExisted); - DescriptorType *type(const char *val, bool *alreadyExisted); + DescriptorType *type(char *val, bool *alreadyExisted); DescriptorType *type(SerStr val, bool *alreadyExisted); + DescriptorType *type(String *val, bool *alreadyExisted); + template DescriptorType *type(BString *val, bool *alreadyExisted); template DescriptorType *type(T *val, bool *alreadyExisted); + template + DescriptorType *type(Vector *val, bool *alreadyExisted); + template DescriptorType *type(UnionView val, bool *alreadyExisted); @@ -194,6 +199,11 @@ DescriptorType *TypeDescWriter::type(T *val, bool *alreadyExisted) { } } +template +DescriptorType *TypeDescWriter::type(Vector *val, bool *alreadyExisted) { + return type(val->data(), alreadyExisted); +} + template DescriptorType *TypeDescWriter::type(UnionView val, bool *alreadyExisted) { return type(val.get(), alreadyExisted);