[ox/model] Make field counting automatic
This commit is contained in:
parent
55f14eb548
commit
cc5cc01e70
2
deps/ox/src/ox/model/CMakeLists.txt
vendored
2
deps/ox/src/ox/model/CMakeLists.txt
vendored
@ -24,8 +24,10 @@ install(
|
||||
desctypes.hpp
|
||||
descwrite.hpp
|
||||
optype.hpp
|
||||
metadata.hpp
|
||||
model.hpp
|
||||
modelops.hpp
|
||||
typenamecatcher.hpp
|
||||
types.hpp
|
||||
walk.hpp
|
||||
DESTINATION
|
||||
|
7
deps/ox/src/ox/model/descread.hpp
vendored
7
deps/ox/src/ox/model/descread.hpp
vendored
@ -18,14 +18,15 @@ class TypeDescReader: public ReaderBase {
|
||||
TypeStore m_typeStore;
|
||||
|
||||
public:
|
||||
TypeDescReader(uint8_t *buff, std::size_t buffLen) noexcept;
|
||||
TypeDescReader(const uint8_t *buff, std::size_t buffLen) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
const TypeStore &typeStore() const noexcept;
|
||||
|
||||
};
|
||||
|
||||
template<typename ReaderBase>
|
||||
TypeDescReader<ReaderBase>::TypeDescReader(uint8_t *buff, std::size_t buffLen) noexcept: ReaderBase(buff, buffLen) {
|
||||
TypeDescReader<ReaderBase>::TypeDescReader(const uint8_t *buff, std::size_t buffLen) noexcept: ReaderBase(buff, buffLen) {
|
||||
}
|
||||
|
||||
template<typename ReaderBase>
|
||||
@ -34,7 +35,7 @@ const TypeStore &TypeDescReader<ReaderBase>::typeStore() const noexcept {
|
||||
}
|
||||
|
||||
template<typename ReaderBase, typename T>
|
||||
int readMCDef(uint8_t *buff, std::size_t buffLen, T *val) noexcept {
|
||||
int readMCDef(const uint8_t *buff, std::size_t buffLen, T *val) noexcept {
|
||||
TypeDescReader<ReaderBase> reader(buff, buffLen);
|
||||
return model(&reader, val);
|
||||
}
|
||||
|
49
deps/ox/src/ox/model/descwrite.hpp
vendored
49
deps/ox/src/ox/model/descwrite.hpp
vendored
@ -17,25 +17,16 @@
|
||||
#include <ox/std/vector.hpp>
|
||||
|
||||
#include "desctypes.hpp"
|
||||
#include "fieldcounter.hpp"
|
||||
#include "metadata.hpp"
|
||||
#include "optype.hpp"
|
||||
#include "typenamecatcher.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace ox {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<bool>
|
||||
struct BoolWrapper {
|
||||
};
|
||||
|
||||
template<typename T, typename = BoolWrapper<true>>
|
||||
struct preloadable: false_type {};
|
||||
|
||||
template<typename T>
|
||||
struct preloadable<T, BoolWrapper<T::Preloadable>> {
|
||||
static constexpr bool value = T::Preloadable;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
static constexpr int indirectionLevels(T) noexcept {
|
||||
return 0;
|
||||
@ -51,31 +42,6 @@ static constexpr int indirectionLevels(T *t) noexcept {
|
||||
class TypeDescWriter {
|
||||
|
||||
private:
|
||||
struct NameCatcher {
|
||||
|
||||
TypeName name;
|
||||
|
||||
template<typename T = std::nullptr_t>
|
||||
constexpr void setTypeInfo(const char *n = T::TypeName, int = T::Fields) noexcept {
|
||||
this->name = n;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr Error field(const char*, T*, std::size_t) noexcept {
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr Error field(const char*, T) noexcept {
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
static constexpr auto opType() noexcept {
|
||||
return OpType::WriteDefinition;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
TypeStore *m_typeStoreOwnerRef = nullptr;
|
||||
TypeStore *m_typeStore = nullptr;
|
||||
DescriptorType *m_type = nullptr;
|
||||
@ -95,7 +61,7 @@ class TypeDescWriter {
|
||||
Error field(const char *name, T *val) noexcept;
|
||||
|
||||
template<typename T = std::nullptr_t>
|
||||
void setTypeInfo(const char *name = T::TypeName, int fields = T::Fields) noexcept;
|
||||
void setTypeInfo(const char *name = T::TypeName, int fields = countFields<T>()) noexcept;
|
||||
|
||||
[[nodiscard]] DescriptorType *definition() noexcept {
|
||||
return m_type;
|
||||
@ -190,11 +156,10 @@ DescriptorType *TypeDescWriter::type(BString<sz> *val, bool *alreadyExisted) noe
|
||||
|
||||
template<typename T>
|
||||
DescriptorType *TypeDescWriter::type(T *val, bool *alreadyExisted) noexcept {
|
||||
NameCatcher nc;
|
||||
oxLogError(model(&nc, val));
|
||||
if (m_typeStore->contains(nc.name)) {
|
||||
const auto name = getModelTypeName(val);
|
||||
if (m_typeStore->contains(name)) {
|
||||
*alreadyExisted = true;
|
||||
return m_typeStore->operator[](nc.name);
|
||||
return m_typeStore->operator[](name);
|
||||
} else {
|
||||
TypeDescWriter dw(m_typeStore);
|
||||
oxLogError(model(&dw, val));
|
||||
|
2
deps/ox/src/ox/model/fieldcounter.hpp
vendored
2
deps/ox/src/ox/model/fieldcounter.hpp
vendored
@ -21,7 +21,7 @@ class FieldCounter {
|
||||
int fields = 0;
|
||||
|
||||
template<typename U = std::nullptr_t>
|
||||
constexpr void setTypeInfo(const char* = U::TypeName, int = 0) {
|
||||
constexpr void setTypeInfo(const char* = "", int = 0) {
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
|
63
deps/ox/src/ox/model/metadata.hpp
vendored
Normal file
63
deps/ox/src/ox/model/metadata.hpp
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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/byteswap.hpp>
|
||||
#include <ox/std/bstring.hpp>
|
||||
#include <ox/std/memory.hpp>
|
||||
#include <ox/std/string.hpp>
|
||||
#include <ox/std/trace.hpp>
|
||||
#include <ox/std/types.hpp>
|
||||
#include <ox/std/vector.hpp>
|
||||
|
||||
#include "desctypes.hpp"
|
||||
#include "fieldcounter.hpp"
|
||||
#include "optype.hpp"
|
||||
#include "typenamecatcher.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace ox {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<bool>
|
||||
struct BoolWrapper {
|
||||
};
|
||||
|
||||
template<typename T, typename = BoolWrapper<true>>
|
||||
struct preloadable : false_type {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct preloadable<T, BoolWrapper<T::Preloadable>> {
|
||||
static constexpr bool value = T::Preloadable;
|
||||
};
|
||||
|
||||
// cannot be done until C++20
|
||||
//struct PseudoString {
|
||||
// constexpr PseudoString(const char* = "") noexcept {}
|
||||
//};
|
||||
//
|
||||
//template<PseudoString>
|
||||
//struct StringWrapper {
|
||||
//};
|
||||
//
|
||||
//template<typename T, typename = StringWrapper<"">>
|
||||
//struct ModelTypeName {
|
||||
// static constexpr const char *value = "";
|
||||
//};
|
||||
//
|
||||
//template<typename T>
|
||||
//struct ModelTypeName<T, detail::StringWrapper<T::TypeName>> {
|
||||
// static constexpr const char *value = T::TypeName;
|
||||
//};
|
||||
|
||||
}
|
||||
|
||||
}
|
64
deps/ox/src/ox/model/typenamecatcher.hpp
vendored
Normal file
64
deps/ox/src/ox/model/typenamecatcher.hpp
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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/string.hpp>
|
||||
#include <ox/std/types.hpp>
|
||||
|
||||
#include "fieldcounter.hpp"
|
||||
#include "optype.hpp"
|
||||
|
||||
namespace ox {
|
||||
|
||||
struct TypeNameCatcher {
|
||||
|
||||
const char *name = "";
|
||||
|
||||
constexpr TypeNameCatcher() noexcept = default;
|
||||
|
||||
template<typename T = std::nullptr_t>
|
||||
constexpr void setTypeInfo(const char *n = T::TypeName, int = 0) noexcept {
|
||||
this->name = n;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr Error field(const char*, T*, std::size_t) noexcept {
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr Error field(const char*, T) noexcept {
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
static constexpr auto opType() noexcept {
|
||||
return OpType::WriteDefinition;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
#if __cplusplus >= 202002L
|
||||
consteval
|
||||
#endif
|
||||
const char *getModelTypeName() noexcept {
|
||||
AllocAlias<T> a = {};
|
||||
TypeNameCatcher nc;
|
||||
oxIgnoreError(model(&nc, std::bit_cast<T*>(&a)));
|
||||
return nc.name;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr const char *getModelTypeName(T *val) noexcept {
|
||||
TypeNameCatcher nc;
|
||||
oxIgnoreError(model(&nc, std::bit_cast<T*>(&val)));
|
||||
return nc.name;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user