diff --git a/deps/ox/src/ox/model/CMakeLists.txt b/deps/ox/src/ox/model/CMakeLists.txt index 6af044b76..d52704496 100644 --- a/deps/ox/src/ox/model/CMakeLists.txt +++ b/deps/ox/src/ox/model/CMakeLists.txt @@ -24,8 +24,10 @@ install( desctypes.hpp descwrite.hpp optype.hpp + metadata.hpp model.hpp modelops.hpp + typenamecatcher.hpp types.hpp walk.hpp DESTINATION diff --git a/deps/ox/src/ox/model/descread.hpp b/deps/ox/src/ox/model/descread.hpp index 3e18de79a..26b77635b 100644 --- a/deps/ox/src/ox/model/descread.hpp +++ b/deps/ox/src/ox/model/descread.hpp @@ -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 -TypeDescReader::TypeDescReader(uint8_t *buff, std::size_t buffLen) noexcept: ReaderBase(buff, buffLen) { +TypeDescReader::TypeDescReader(const uint8_t *buff, std::size_t buffLen) noexcept: ReaderBase(buff, buffLen) { } template @@ -34,7 +35,7 @@ const TypeStore &TypeDescReader::typeStore() const noexcept { } template -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 reader(buff, buffLen); return model(&reader, val); } diff --git a/deps/ox/src/ox/model/descwrite.hpp b/deps/ox/src/ox/model/descwrite.hpp index f9147a73e..350a59223 100644 --- a/deps/ox/src/ox/model/descwrite.hpp +++ b/deps/ox/src/ox/model/descwrite.hpp @@ -17,25 +17,16 @@ #include #include "desctypes.hpp" +#include "fieldcounter.hpp" +#include "metadata.hpp" #include "optype.hpp" +#include "typenamecatcher.hpp" #include "types.hpp" namespace ox { namespace detail { -template -struct BoolWrapper { -}; - -template> -struct preloadable: false_type {}; - -template -struct preloadable> { - static constexpr bool value = T::Preloadable; -}; - template 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 - constexpr void setTypeInfo(const char *n = T::TypeName, int = T::Fields) noexcept { - this->name = n; - } - - template - constexpr Error field(const char*, T*, std::size_t) noexcept { - return OxError(0); - } - - template - 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 - void setTypeInfo(const char *name = T::TypeName, int fields = T::Fields) noexcept; + void setTypeInfo(const char *name = T::TypeName, int fields = countFields()) noexcept; [[nodiscard]] DescriptorType *definition() noexcept { return m_type; @@ -190,11 +156,10 @@ DescriptorType *TypeDescWriter::type(BString *val, bool *alreadyExisted) noe template 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)); diff --git a/deps/ox/src/ox/model/fieldcounter.hpp b/deps/ox/src/ox/model/fieldcounter.hpp index ec50ed91f..920619187 100644 --- a/deps/ox/src/ox/model/fieldcounter.hpp +++ b/deps/ox/src/ox/model/fieldcounter.hpp @@ -21,7 +21,7 @@ class FieldCounter { int fields = 0; template - constexpr void setTypeInfo(const char* = U::TypeName, int = 0) { + constexpr void setTypeInfo(const char* = "", int = 0) { } template diff --git a/deps/ox/src/ox/model/metadata.hpp b/deps/ox/src/ox/model/metadata.hpp new file mode 100644 index 000000000..f7fd61582 --- /dev/null +++ b/deps/ox/src/ox/model/metadata.hpp @@ -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 +#include +#include +#include +#include +#include +#include + +#include "desctypes.hpp" +#include "fieldcounter.hpp" +#include "optype.hpp" +#include "typenamecatcher.hpp" +#include "types.hpp" + +namespace ox { + +namespace detail { + +template +struct BoolWrapper { +}; + +template> +struct preloadable : false_type { +}; + +template +struct preloadable> { + static constexpr bool value = T::Preloadable; +}; + +// cannot be done until C++20 +//struct PseudoString { +// constexpr PseudoString(const char* = "") noexcept {} +//}; +// +//template +//struct StringWrapper { +//}; +// +//template> +//struct ModelTypeName { +// static constexpr const char *value = ""; +//}; +// +//template +//struct ModelTypeName> { +// static constexpr const char *value = T::TypeName; +//}; + +} + +} \ No newline at end of file diff --git a/deps/ox/src/ox/model/typenamecatcher.hpp b/deps/ox/src/ox/model/typenamecatcher.hpp new file mode 100644 index 000000000..464a091d2 --- /dev/null +++ b/deps/ox/src/ox/model/typenamecatcher.hpp @@ -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 +#include + +#include "fieldcounter.hpp" +#include "optype.hpp" + +namespace ox { + +struct TypeNameCatcher { + + const char *name = ""; + + constexpr TypeNameCatcher() noexcept = default; + + template + constexpr void setTypeInfo(const char *n = T::TypeName, int = 0) noexcept { + this->name = n; + } + + template + constexpr Error field(const char*, T*, std::size_t) noexcept { + return OxError(0); + } + + template + constexpr Error field(const char*, T) noexcept { + return OxError(0); + } + + static constexpr auto opType() noexcept { + return OpType::WriteDefinition; + } + +}; + +template +#if __cplusplus >= 202002L +consteval +#endif +const char *getModelTypeName() noexcept { + AllocAlias a = {}; + TypeNameCatcher nc; + oxIgnoreError(model(&nc, std::bit_cast(&a))); + return nc.name; +} + +template +constexpr const char *getModelTypeName(T *val) noexcept { + TypeNameCatcher nc; + oxIgnoreError(model(&nc, std::bit_cast(&val))); + return nc.name; +} + +} \ No newline at end of file