/* * Copyright 2015 - 2025 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 https://mozilla.org/MPL/2.0/. */ #pragma once #include #include #include #include #include #include namespace ox { template class UnionSizeCatcher: public ModelHandlerBase, OpType::Reflect> { private: std::size_t m_size = 0; public: template constexpr ox::Error setTypeInfo( const char* = T::TypeName, int = T::TypeVersion) noexcept { return {}; } template constexpr ox::Error setTypeInfo( const char*, int, const Vector&, std::size_t) noexcept { return {}; } template constexpr ox::Error field(StringViewCR, const UnionView val) noexcept { UnionSizeCatcher sc; OX_RETURN_ERROR(model(sc.interface(), val.get())); m_size += sc.size(); return {}; } template constexpr ox::Error field(StringViewCR, const T *val) noexcept; template constexpr ox::Error field(StringViewCR, const T **val, std::size_t cnt) noexcept; [[nodiscard]] constexpr auto size() const noexcept { return m_size; } private: template constexpr ox::Error fieldStr(StringViewCR, const ox::BasicString *val) noexcept; template constexpr ox::Error fieldVector(StringViewCR, const ox::Vector *val) noexcept; constexpr void setSize(std::size_t sz) noexcept; }; template template constexpr ox::Error UnionSizeCatcher::field(StringViewCR, const T *val) noexcept { setSize(sizeOf(val)); return {}; } template template constexpr ox::Error UnionSizeCatcher::field(StringViewCR, const T **val, std::size_t cnt) noexcept { for (std::size_t i = 0; i < cnt; ++i) { OX_RETURN_ERROR(field("", &val[i])); } return {}; } template template constexpr ox::Error UnionSizeCatcher::fieldStr(StringViewCR, const ox::BasicString*) noexcept { ox::VectorMemMap v; setSize(sizeOf(v)); return {}; } template template constexpr ox::Error UnionSizeCatcher::fieldVector(StringViewCR, const ox::Vector*) noexcept { ox::VectorMemMap v; setSize(sizeOf(v)); return {}; } template constexpr void UnionSizeCatcher::setSize(std::size_t sz) noexcept { m_size = ox::max(m_size, sz); } }