[ox/model] Fix model descriptor writing to handle Vector, etc.

This commit is contained in:
Gary Talent 2020-06-16 04:40:43 -05:00
parent f58c658be1
commit 4f4ec089fd
3 changed files with 42 additions and 20 deletions

View File

@ -8,11 +8,14 @@
#pragma once #pragma once
#include <ox/std/bit.hpp>
#include <ox/std/error.hpp> #include <ox/std/error.hpp>
#include <ox/std/hashmap.hpp> #include <ox/std/hashmap.hpp>
#include <ox/std/bstring.hpp> #include <ox/std/bstring.hpp>
#include <ox/std/vector.hpp> #include <ox/std/vector.hpp>
#include "types.hpp"
namespace ox { namespace ox {
using ModelString = BString<100>; using ModelString = BString<100>;
@ -32,8 +35,10 @@ enum class PrimitiveType: uint8_t {
struct DescriptorField { struct DescriptorField {
// order of fields matters // order of fields matters
static constexpr auto TypeVersion = 1;
// only serialize type name if type has already been serialized // only serialize type name if type has already been serialized
const struct DescriptorType *type = nullptr; struct DescriptorType *type = nullptr;
FieldName fieldName; FieldName fieldName;
int subscriptLevels = 0; int subscriptLevels = 0;
@ -54,7 +59,7 @@ struct DescriptorField {
ownsType = false; // is copy, only owns type if move 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->type = type;
this->fieldName = fieldName; this->fieldName = fieldName;
this->subscriptLevels = subscriptLevels; this->subscriptLevels = subscriptLevels;
@ -99,6 +104,7 @@ struct DescriptorField {
using FieldList = Vector<DescriptorField>; using FieldList = Vector<DescriptorField>;
struct DescriptorType { struct DescriptorType {
static constexpr auto TypeVersion = 1;
TypeName typeName; TypeName typeName;
PrimitiveType primitiveType; PrimitiveType primitiveType;
// fieldList only applies to structs // fieldList only applies to structs
@ -120,14 +126,13 @@ struct DescriptorType {
template<typename T> template<typename T>
Error model(T *io, DescriptorType *type) { Error model(T *io, DescriptorType *type) {
auto err = OxError(0); io->template setTypeInfo<T>("net.drinkingtea.ox.DescriptorType", 5);
io->setTypeInfo("ox::DescriptorType", 4); oxReturnError(io->field("typeName", &type->typeName));
err |= io->field("typeName", &type->typeName); oxReturnError(io->field("primitiveType", bit_cast<uint8_t*>(&type->primitiveType)));
err |= io->field("primitiveType", &type->primitiveType); oxReturnError(io->field("fieldList", &type->fieldList));
err |= io->field("fieldList", &type->fieldList); oxReturnError(io->field("length", &type->length));
err |= io->field("length", &type->length); oxReturnError(io->field("preloadable", &type->preloadable));
err |= io->field("preloadable", &type->preloadable); return OxError(0);
return err;
} }
template<typename T> template<typename T>
@ -135,16 +140,17 @@ Error modelWrite(T *io, DescriptorField *field) {
auto err = OxError(0); auto err = OxError(0);
io->setTypeInfo("ox::DescriptorField", 4); io->setTypeInfo("ox::DescriptorField", 4);
if (field->ownsType) { if (field->ownsType) {
err |= io->field("typeName", ""); BString<2> empty = "";
err |= io->field("type", field->type); oxReturnError(io->field("typeName", SerStr(&empty)));
oxReturnError(io->field("type", field->type));
} else { } else {
err |= io->field("typeName", &field->type->typeName); oxReturnError(io->field("typeName", SerStr(&field->type->typeName)));
err |= io->field("type", static_cast<decltype(field->type)>(nullptr)); oxReturnError(io->field("type", static_cast<decltype(field->type)>(nullptr)));
} }
err |= io->field("fieldName", &field->fieldName); oxReturnError(io->field("fieldName", &field->fieldName));
// defaultValue is unused now, but leave placeholder for backwards compatibility // defaultValue is unused now, but leave placeholder for backwards compatibility
const int DefaultValue = 0; int DefaultValue = 0;
err |= io->field("defaultValue", &DefaultValue); oxReturnError(io->field("defaultValue", &DefaultValue));
return err; return err;
} }

View File

@ -116,7 +116,7 @@ DescriptorType *TypeDescWriter::type(uint64_t*, bool *alreadyExisted) {
return getType(TypeName, PT, Bytes, 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 TypeName = "B:string";
constexpr auto PT = PrimitiveType::String; constexpr auto PT = PrimitiveType::String;
return getType(TypeName, PT, 0, alreadyExisted); return getType(TypeName, PT, 0, alreadyExisted);
@ -128,6 +128,12 @@ DescriptorType *TypeDescWriter::type(SerStr, bool *alreadyExisted) {
return getType(TypeName, PT, 0, 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) { DescriptorType *TypeDescWriter::type(bool*, bool *alreadyExisted) {
constexpr auto TypeName = "B:bool"; constexpr auto TypeName = "B:bool";
constexpr auto PT = PrimitiveType::Bool; constexpr auto PT = PrimitiveType::Bool;

View File

@ -9,8 +9,8 @@
#pragma once #pragma once
#include <ox/std/byteswap.hpp> #include <ox/std/byteswap.hpp>
#include <ox/std/hashmap.hpp>
#include <ox/std/bstring.hpp> #include <ox/std/bstring.hpp>
#include <ox/std/string.hpp>
#include <ox/std/trace.hpp> #include <ox/std/trace.hpp>
#include <ox/std/types.hpp> #include <ox/std/types.hpp>
#include <ox/std/vector.hpp> #include <ox/std/vector.hpp>
@ -117,16 +117,21 @@ class TypeDescWriter {
DescriptorType *type(bool *val, bool *alreadyExisted); 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(SerStr val, bool *alreadyExisted);
DescriptorType *type(String *val, bool *alreadyExisted);
template<std::size_t sz> template<std::size_t sz>
DescriptorType *type(BString<sz> *val, bool *alreadyExisted); DescriptorType *type(BString<sz> *val, bool *alreadyExisted);
template<typename T> template<typename T>
DescriptorType *type(T *val, bool *alreadyExisted); DescriptorType *type(T *val, bool *alreadyExisted);
template<typename T>
DescriptorType *type(Vector<T> *val, bool *alreadyExisted);
template<typename U> template<typename U>
DescriptorType *type(UnionView<U> val, bool *alreadyExisted); DescriptorType *type(UnionView<U> val, bool *alreadyExisted);
@ -194,6 +199,11 @@ DescriptorType *TypeDescWriter::type(T *val, bool *alreadyExisted) {
} }
} }
template<typename T>
DescriptorType *TypeDescWriter::type(Vector<T> *val, bool *alreadyExisted) {
return type(val->data(), alreadyExisted);
}
template<typename U> template<typename U>
DescriptorType *TypeDescWriter::type(UnionView<U> val, bool *alreadyExisted) { DescriptorType *TypeDescWriter::type(UnionView<U> val, bool *alreadyExisted) {
return type(val.get(), alreadyExisted); return type(val.get(), alreadyExisted);