diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index a8ff438c..2338b6f2 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -33,15 +34,13 @@ class MetalClawReaderTemplate: public ModelHandlerBase m_fieldPresence; std::size_t m_fields = 0; std::size_t m_field = 0; - int m_unionIdx = -1; + ox::Optional m_unionIdx; Reader &m_reader; - MetalClawReaderTemplate *m_parent = nullptr; public: explicit constexpr MetalClawReaderTemplate( Reader &reader, - int unionIdx = -1, - MetalClawReaderTemplate *parent = nullptr) noexcept; + ox::Optional const&unionIdx = {}) noexcept; constexpr ~MetalClawReaderTemplate() noexcept; @@ -108,7 +107,7 @@ class MetalClawReaderTemplate: public ModelHandlerBase child(const char *name, int unionIdx = -1) noexcept; + constexpr MetalClawReaderTemplate child(const char *name, ox::Optional unionIdx = {}) noexcept; /** * Indicates whether or not the next field to be read is present. @@ -136,12 +135,10 @@ class MetalClawReaderTemplate: public ModelHandlerBase constexpr MetalClawReaderTemplate::MetalClawReaderTemplate( Reader &reader, - int unionIdx, - MetalClawReaderTemplate *parent) noexcept: + ox::Optional const&unionIdx) noexcept: m_fieldPresence(reader), m_unionIdx(unionIdx), - m_reader(reader), - m_parent(parent) { + m_reader(reader) { } template @@ -194,7 +191,7 @@ constexpr Error MetalClawReaderTemplate::field(const char*, uint64_t *va template constexpr Error MetalClawReaderTemplate::field(const char*, bool *val) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { auto const result = m_fieldPresence.get(static_cast(m_field)); *val = result.value; oxReturnError(result); @@ -206,7 +203,7 @@ constexpr Error MetalClawReaderTemplate::field(const char*, bool *val) n // array handler template constexpr Error MetalClawReaderTemplate::field(const char *name, auto *val, std::size_t valLen) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { // read the length std::size_t bytesRead = 0; @@ -232,7 +229,7 @@ constexpr Error MetalClawReaderTemplate::field(const char *name, auto *v template template constexpr Error MetalClawReaderTemplate::field(const char*, HashMap *val) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { // read the length oxRequire(g, m_reader.tellg()); @@ -264,7 +261,7 @@ template template constexpr Error MetalClawReaderTemplate::field(const char *name, T *val) noexcept { if constexpr(isVector_v) { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { // set size of val if the field is present, don't worry about it if not if (m_fieldPresence.get(static_cast(m_field))) { oxRequire(len, arrayLength(name, false)); @@ -276,7 +273,7 @@ constexpr Error MetalClawReaderTemplate::field(const char *name, T *val) ++m_field; return {}; } else if constexpr(isArray_v) { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { // set size of val if the field is present, don't worry about it if not if (m_fieldPresence.get(static_cast(m_field))) { oxRequire(len, arrayLength(name, false)); @@ -289,7 +286,7 @@ constexpr Error MetalClawReaderTemplate::field(const char *name, T *val) ++m_field; return {}; } else { - if ((m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) && val) { + if ((!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) && val) { if (m_fieldPresence.get(static_cast(m_field))) { auto reader = child(""); oxReturnError(model(reader.interface(), val)); @@ -303,9 +300,9 @@ constexpr Error MetalClawReaderTemplate::field(const char *name, T *val) template template constexpr Error MetalClawReaderTemplate::field(const char*, UnionView val) noexcept { - if ((m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) && val.get()) { + if ((!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) && val.get()) { if (m_fieldPresence.get(static_cast(m_field))) { - auto reader = child("", val.idx()); + auto reader = child("", ox::Optional(ox::in_place_t{}, val.idx())); oxReturnError(model(reader.interface(), val.get())); } } @@ -316,7 +313,7 @@ constexpr Error MetalClawReaderTemplate::field(const char*, UnionView template constexpr Error MetalClawReaderTemplate::field(const char*, BasicString *val) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { // read the length std::size_t bytesRead = 0; @@ -382,7 +379,7 @@ constexpr Error MetalClawReaderTemplate::fieldCString(const char*, char template constexpr Error MetalClawReaderTemplate::fieldCString(const char*, char **val, std::size_t buffLen) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { // read the length std::size_t bytesRead = 0; @@ -410,7 +407,7 @@ constexpr Error MetalClawReaderTemplate::fieldCString(const char*, char template constexpr Result MetalClawReaderTemplate::arrayLength(const char*, bool pass) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { // read the length std::size_t bytesRead = 0; @@ -427,7 +424,7 @@ constexpr Result MetalClawReaderTemplate::arrayLength(const template constexpr Result MetalClawReaderTemplate::stringLength(const char*) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { // read the length std::size_t bytesRead = 0; @@ -442,7 +439,7 @@ constexpr Result MetalClawReaderTemplate::stringLength(con template template constexpr Error MetalClawReaderTemplate::readInteger(I *val) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { std::size_t bytesRead = 0; auto const result = mc::decodeInteger(m_reader, &bytesRead); @@ -459,7 +456,7 @@ constexpr Error MetalClawReaderTemplate::readInteger(I *val) noexcept { template template constexpr Error MetalClawReaderTemplate::field(const char*, CB cb) noexcept { - if (m_unionIdx == -1 || static_cast(m_unionIdx) == m_field) { + if (!m_unionIdx.has_value() || static_cast(*m_unionIdx) == m_field) { if (m_fieldPresence.get(static_cast(m_field))) { // read the length std::size_t bytesRead = 0; @@ -491,8 +488,10 @@ constexpr ox::Error MetalClawReaderTemplate::setTypeInfo( } template -constexpr MetalClawReaderTemplate MetalClawReaderTemplate::child(const char*, int unionIdx) noexcept { - return MetalClawReaderTemplate(m_reader, unionIdx, this); +constexpr MetalClawReaderTemplate MetalClawReaderTemplate::child( + const char*, + ox::Optional unionIdx) noexcept { + return MetalClawReaderTemplate(m_reader, unionIdx); } template diff --git a/deps/ox/src/ox/mc/write.hpp b/deps/ox/src/ox/mc/write.hpp index cc905c35..9b3c1015 100644 --- a/deps/ox/src/ox/mc/write.hpp +++ b/deps/ox/src/ox/mc/write.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -34,12 +35,12 @@ class MetalClawWriter { ox::Vector m_presenceMapBuff{}; FieldBitmap m_fieldPresence; int m_field = 0; - int m_unionIdx = -1; + ox::Optional m_unionIdx; std::size_t m_writerBeginP{}; Writer &m_writer; public: - constexpr explicit MetalClawWriter(Writer &writer, int unionIdx = -1) noexcept; + constexpr explicit MetalClawWriter(Writer &writer, ox::Optional const&unionIdx = {}) noexcept; constexpr ~MetalClawWriter() noexcept = default; @@ -114,7 +115,7 @@ class MetalClawWriter { private: constexpr Error appendInteger(Integer_c auto val) noexcept { bool fieldSet = false; - if (val && (m_unionIdx == -1 || m_unionIdx == m_field)) { + if (val && (!m_unionIdx.has_value() || *m_unionIdx == m_field)) { auto mi = mc::encodeInteger(val); oxReturnError(m_writer.write(reinterpret_cast(mi.data), mi.length)); fieldSet = true; @@ -130,7 +131,7 @@ extern template class ModelHandlerInterface>; extern template class ModelHandlerInterface>; template -constexpr MetalClawWriter::MetalClawWriter(Writer &writer, int unionIdx) noexcept: +constexpr MetalClawWriter::MetalClawWriter(Writer &writer, ox::Optional const&unionIdx) noexcept: m_fieldPresence(m_presenceMapBuff.data(), m_presenceMapBuff.size()), m_unionIdx(unionIdx), m_writerBeginP(writer.tellp()), @@ -179,7 +180,7 @@ constexpr Error MetalClawWriter::field(const char*, const uint64_t *val) template constexpr Error MetalClawWriter::field(const char*, const bool *val) noexcept { - if (m_unionIdx == -1 || m_unionIdx == m_field) { + if (!m_unionIdx.has_value() || *m_unionIdx == m_field) { oxReturnError(m_fieldPresence.set(static_cast(m_field), *val)); } ++m_field; @@ -190,7 +191,7 @@ template template constexpr Error MetalClawWriter::field(const char*, const BasicString *val) noexcept { bool fieldSet = false; - if (val->len() && (m_unionIdx == -1 || m_unionIdx == m_field)) { + if (val->len() && (!m_unionIdx.has_value() || *m_unionIdx == m_field)) { // write the length const auto strLen = mc::encodeInteger(val->len()); oxReturnError(m_writer.write(reinterpret_cast(strLen.data), strLen.length)); @@ -212,7 +213,7 @@ constexpr Error MetalClawWriter::field(const char *name, const BString constexpr Error MetalClawWriter::fieldCString(const char*, const char *const*val, std::size_t) noexcept { bool fieldSet = false; - if (m_unionIdx == -1 || m_unionIdx == m_field) { + if (!m_unionIdx.has_value() || *m_unionIdx == m_field) { const auto strLen = *val ? ox_strlen(*val) : 0; // write the length const auto strLenBuff = mc::encodeInteger(strLen); @@ -239,7 +240,7 @@ constexpr Error MetalClawWriter::fieldCString(const char *name, const ch template constexpr Error MetalClawWriter::fieldCString(const char*, const char *val, std::size_t strLen) noexcept { bool fieldSet = false; - if (strLen && (m_unionIdx == -1 || m_unionIdx == m_field)) { + if (strLen && (!m_unionIdx.has_value() || *m_unionIdx == m_field)) { // write the length const auto strLenBuff = mc::encodeInteger(strLen); oxReturnError(m_writer.write(reinterpret_cast(strLenBuff.data), strLenBuff.length)); @@ -259,7 +260,7 @@ constexpr Error MetalClawWriter::field(const char*, const T *val) noexce return field(nullptr, val->data(), val->size()); } else { bool fieldSet = false; - if (val && (m_unionIdx == -1 || m_unionIdx == m_field)) { + if (val && (!m_unionIdx.has_value() || *m_unionIdx == m_field)) { MetalClawWriter writer(m_writer); ModelHandlerInterface> handler{&writer}; oxReturnError(model(&handler, val)); @@ -276,8 +277,8 @@ template template constexpr Error MetalClawWriter::field(const char*, UnionView val) noexcept { bool fieldSet = false; - if (val.get() && (m_unionIdx == -1 || m_unionIdx == m_field)) { - MetalClawWriter writer(m_writer, val.idx()); + if (val.get() && (!m_unionIdx.has_value() || *m_unionIdx == m_field)) { + MetalClawWriter writer(m_writer, ox::Optional(ox::in_place, val.idx())); ModelHandlerInterface handler{&writer}; oxReturnError(model(&handler, val.get())); oxReturnError(writer.finalize()); @@ -292,7 +293,7 @@ template template constexpr Error MetalClawWriter::field(const char*, const T *val, std::size_t len) noexcept { bool fieldSet = false; - if (len && (m_unionIdx == -1 || m_unionIdx == m_field)) { + if (len && (!m_unionIdx.has_value() || *m_unionIdx == m_field)) { // write the length const auto arrLen = mc::encodeInteger(len); oxReturnError(m_writer.write(reinterpret_cast(arrLen.data), arrLen.length)); @@ -317,7 +318,7 @@ constexpr Error MetalClawWriter::field(const char*, const HashMapkeys(); const auto len = keys.size(); bool fieldSet = false; - if (len && (m_unionIdx == -1 || m_unionIdx == m_field)) { + if (len && (!m_unionIdx.has_value() || *m_unionIdx == m_field)) { // write the length const auto arrLen = mc::encodeInteger(len); oxReturnError(m_writer.write(reinterpret_cast(arrLen.data), arrLen.length));