[ox/model] Fix model descriptor writing to handle Vector, etc.
This commit is contained in:
parent
f58c658be1
commit
4f4ec089fd
40
deps/ox/src/ox/model/desctypes.hpp
vendored
40
deps/ox/src/ox/model/desctypes.hpp
vendored
@ -8,11 +8,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ox/std/bit.hpp>
|
||||
#include <ox/std/error.hpp>
|
||||
#include <ox/std/hashmap.hpp>
|
||||
#include <ox/std/bstring.hpp>
|
||||
#include <ox/std/vector.hpp>
|
||||
|
||||
#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<DescriptorField>;
|
||||
|
||||
struct DescriptorType {
|
||||
static constexpr auto TypeVersion = 1;
|
||||
TypeName typeName;
|
||||
PrimitiveType primitiveType;
|
||||
// fieldList only applies to structs
|
||||
@ -120,14 +126,13 @@ struct DescriptorType {
|
||||
|
||||
template<typename T>
|
||||
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<T>("net.drinkingtea.ox.DescriptorType", 5);
|
||||
oxReturnError(io->field("typeName", &type->typeName));
|
||||
oxReturnError(io->field("primitiveType", bit_cast<uint8_t*>(&type->primitiveType)));
|
||||
oxReturnError(io->field("fieldList", &type->fieldList));
|
||||
oxReturnError(io->field("length", &type->length));
|
||||
oxReturnError(io->field("preloadable", &type->preloadable));
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@ -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_cast<decltype(field->type)>(nullptr));
|
||||
oxReturnError(io->field("typeName", SerStr(&field->type->typeName)));
|
||||
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
|
||||
const int DefaultValue = 0;
|
||||
err |= io->field("defaultValue", &DefaultValue);
|
||||
int DefaultValue = 0;
|
||||
oxReturnError(io->field("defaultValue", &DefaultValue));
|
||||
return err;
|
||||
}
|
||||
|
||||
|
8
deps/ox/src/ox/model/descwrite.cpp
vendored
8
deps/ox/src/ox/model/descwrite.cpp
vendored
@ -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;
|
||||
|
14
deps/ox/src/ox/model/descwrite.hpp
vendored
14
deps/ox/src/ox/model/descwrite.hpp
vendored
@ -9,8 +9,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <ox/std/byteswap.hpp>
|
||||
#include <ox/std/hashmap.hpp>
|
||||
#include <ox/std/bstring.hpp>
|
||||
#include <ox/std/string.hpp>
|
||||
#include <ox/std/trace.hpp>
|
||||
#include <ox/std/types.hpp>
|
||||
#include <ox/std/vector.hpp>
|
||||
@ -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<std::size_t sz>
|
||||
DescriptorType *type(BString<sz> *val, bool *alreadyExisted);
|
||||
|
||||
template<typename T>
|
||||
DescriptorType *type(T *val, bool *alreadyExisted);
|
||||
|
||||
template<typename T>
|
||||
DescriptorType *type(Vector<T> *val, bool *alreadyExisted);
|
||||
|
||||
template<typename U>
|
||||
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>
|
||||
DescriptorType *TypeDescWriter::type(UnionView<U> val, bool *alreadyExisted) {
|
||||
return type(val.get(), alreadyExisted);
|
||||
|
Loading…
x
Reference in New Issue
Block a user