diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index 52b08914a..afa048f50 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -107,7 +107,7 @@ struct TaggedUnion { /// @return The result of applying _f to the active alternative template auto visit(F&& _f) - -> decltype(std::declval().visit(std::declval())) { + -> decltype(std::declval().visit(std::declval())) { return variant_.visit(std::forward(_f)); } @@ -117,8 +117,10 @@ struct TaggedUnion { /// @param _f The function to apply to the active alternative /// @return The result of applying _f to the active alternative template - auto visit(F&& _f) const - -> decltype(std::declval().visit(std::declval())) { + requires requires(VariantType& _v, F&& _f) { + _v.visit(std::forward(_f)); + } + decltype(auto) visit(F&& _f) const { return variant_.visit(std::forward(_f)); } @@ -160,16 +162,7 @@ using possible_tags_t = typename PossibleTags::Type; template bool operator==(const TaggedUnion<_discriminator, Ts...>& lhs, const TaggedUnion<_discriminator, Ts...>& rhs) { - return (lhs.variant().index() == rhs.variant().index()) && - lhs.variant().visit([&rhs](const auto& l) { - return rhs.variant().visit([&l](const auto& r) -> bool { - if constexpr (std::is_same_v, - std::decay_t>) - return l == r; - else - return false; - }); - }); + return internal::variant::Access::equals(lhs.variant(), rhs.variant()); } } // namespace rfl diff --git a/include/rfl/Variant.hpp b/include/rfl/Variant.hpp index 018edca72..528f06f70 100644 --- a/include/rfl/Variant.hpp +++ b/include/rfl/Variant.hpp @@ -2,9 +2,10 @@ #define RFL_VARIANT_HPP_ #include +#include #include #include -#include +#include #include #include #include @@ -12,6 +13,7 @@ #include "internal/element_index.hpp" #include "internal/nth_element_t.hpp" #include "internal/ptr_cast.hpp" +#include "internal/variant/dispatch.hpp" #include "internal/variant/find_max_size.hpp" #include "internal/variant/is_alternative_type.hpp" #include "internal/variant/result_t.hpp" @@ -19,10 +21,11 @@ namespace rfl { template class Variant { - static constexpr auto max_size_wrapper_ = - internal::variant::find_max_size(); + static_assert(sizeof...(AlternativeTypes) > 0, + "Variant must have at least one alternative type."); - static constexpr unsigned long num_bytes_ = max_size_wrapper_.size_; + static constexpr std::size_t num_bytes_ = + internal::variant::find_max_size(); using DataType = std::array; @@ -33,38 +36,30 @@ class Variant { static constexpr IndexType size_ = sizeof...(AlternativeTypes); - template - using result_t = internal::variant::result_t; + using FirstAlternative = internal::nth_element_t<0, AlternativeTypes...>; - template - using Index = std::integral_constant; - - template - struct TypeWrapper {}; + friend struct internal::variant::Access; public: /// @brief Default-constructs the Variant, initializing it with the first /// alternative type. /// @details The first alternative type is default-constructed and stored in /// the variant. - Variant() : index_(IndexType()), data_(DataType()) { - using FirstAlternative = internal::nth_element_t<0, AlternativeTypes...>; - move_from_type(FirstAlternative()); + Variant() : index_(0) { + ::new (static_cast(data_.data())) FirstAlternative(); } /// @brief Copy-constructs the Variant from another Variant. /// @param _other The Variant to copy from. /// @details The contained value is copied from the other Variant, preserving /// the alternative type. - Variant(const Variant& _other) : index_(IndexType()), data_(DataType()) { - copy_from_other(_other); - } + Variant(const Variant& _other) : index_(0) { copy_from_other(_other); } /// @brief Move-constructs the Variant from another Variant. /// @param _other The Variant to move from. /// @details The contained value is moved from the other Variant, preserving /// the alternative type. - Variant(Variant&& _other) noexcept : index_(IndexType()), data_(DataType()) { + Variant(Variant&& _other) noexcept : index_(0) { move_from_other(std::move(_other)); } @@ -74,7 +69,7 @@ class Variant { /// @details Only enabled if T is one of the alternative types. template requires internal::variant::is_alternative_type_v - Variant(const T& _t) : index_(IndexType()), data_(DataType()) { + Variant(const T& _t) : index_(0) { copy_from_type(_t); } @@ -83,8 +78,11 @@ class Variant { /// @param _t The value to move into the Variant. /// @details Only enabled if T is one of the alternative types. template - requires internal::variant::is_alternative_type_v - Variant(T&& _t) noexcept : index_(IndexType()), data_(DataType()) { + requires(!std::is_lvalue_reference_v && !std::is_const_v && + internal::variant::is_alternative_type_v) + Variant(T&& _t) noexcept( + std::is_nothrow_constructible_v, T&&>) + : index_(0) { move_from_type(std::forward(_t)); } @@ -114,8 +112,14 @@ class Variant { /// @return Reference to the newly emplaced value. template constexpr auto& emplace(Args&&... _args) { + static_assert(_i >= 0 && _i < static_cast(sizeof...(AlternativeTypes)), + "Index out of bounds."); using T = internal::nth_element_t<_i, AlternativeTypes...>; - return emplace(std::move(_args)...); + auto t = T{std::forward(_args)...}; + destroy_if_necessary(); + index_ = static_cast(_i); + ::new (static_cast(data_.data())) T(std::move(t)); + return *internal::ptr_cast(data_.data()); } /// @brief Returns the index of the currently held alternative. @@ -140,7 +144,8 @@ class Variant { /// @param _t The value to assign. /// @return Reference to this Variant. template - requires internal::variant::is_alternative_type_v + requires(!std::is_lvalue_reference_v && !std::is_const_v && + internal::variant::is_alternative_type_v) Variant& operator=(T&& _t) noexcept { destroy_if_necessary(); move_from_type(std::forward(_t)); @@ -181,7 +186,7 @@ class Variant { return; } auto temp = Variant(std::move(*this)); - move_from_other(std::move(_other)); + *this = std::move(_other); _other = std::move(temp); } @@ -190,23 +195,13 @@ class Variant { /// @param _f The visitor function or functor. /// @return The result of the visitor, or void. template - result_t visit(F&& _f) { - using ResultType = result_t; - if constexpr (std::is_same_v) { - bool visited = false; - do_visit_no_result(std::forward(_f), &visited, - std::make_integer_sequence()); - } else if constexpr (std::is_reference_v) { - std::remove_reference_t* res = nullptr; - do_visit_with_reference(std::forward(_f), &res, - std::make_integer_sequence()); - return *res; - } else { - auto res = std::optional(); - do_visit_with_result(std::forward(_f), &res, - std::make_integer_sequence()); - return std::move(*res); - } + requires internal::variant::visit_invocable + internal::variant::visit_result_t visit(F&& _f) { + using ResultType = internal::variant::visit_result_t; + return internal::variant::SwitchDispatch< + false, internal::variant::VisitAction, ResultType, 0, + AlternativeTypes...>::apply(static_cast(index_), + data_.data(), std::forward(_f)); } /// @brief Visits the contained value with a visitor function (const @@ -215,299 +210,56 @@ class Variant { /// @param _f The visitor function or functor. /// @return The result of the visitor, or void. template - result_t visit(F&& _f) const { - using ResultType = result_t; - if constexpr (std::is_same_v) { - bool visited = false; - do_visit_no_result(std::forward(_f), &visited, - std::make_integer_sequence()); - } else if constexpr (std::is_reference_v) { - std::remove_reference_t* res = nullptr; - do_visit_with_reference(std::forward(_f), &res, - std::make_integer_sequence()); - return *res; - } else { - auto res = std::optional(); - do_visit_with_result(std::forward(_f), &res, - std::make_integer_sequence()); - return std::move(*res); - } + requires internal::variant::visit_const_invocable + decltype(auto) visit(F&& _f) const { + using ResultType = + internal::variant::visit_result_t; + return internal::variant::SwitchDispatch< + false, internal::variant::VisitAction, ResultType, 0, + AlternativeTypes...>::apply(static_cast(index_), + data_.data(), std::forward(_f)); } private: /// @brief Copies the value from another Variant. /// @param _other The Variant to copy from. void copy_from_other(const Variant& _other) { - const auto copy_one = [this](const auto& _t) { this->copy_from_type(_t); }; - _other.visit(copy_one); - } - - /// @brief Copies a value of type T from another type. - /// @tparam T The type to copy from. - /// @param _t The value to copy. - /// @details Tries to copy the value from T to each alternative type, and if - /// successful, stores it in the Variant. Only the first successful copy is - /// performed. - template - void copy_from_other_type(const T& _t) { - bool set = false; - const auto copy_one = [&, this](const T& _t, - const TypeWrapper) { - if constexpr (std::is_convertible_v) { - if (!set) { - move_from_type(AltType(_t)); - set = true; - } - } - }; - (copy_one(_t, TypeWrapper{}), ...); + internal::variant::SwitchDispatch< + false, internal::variant::CopyAction, void, 0, + AlternativeTypes...>::apply(static_cast(_other.index_), + data_.data(), _other.data_.data()); + index_ = _other.index_; } /// @brief Copies a value of type T into the Variant. /// @tparam T The type to copy from. /// @param _t The value to copy. template - void copy_from_type(const T& _t) noexcept { + void copy_from_type(const T& _t) { using CurrentType = std::remove_cvref_t; - index_ = - internal::element_index...>(); - new (data_.data()) CurrentType(_t); + constexpr int ix = internal::element_index< + CurrentType, std::remove_cvref_t...>(); + static_assert(ix != -1, "Type not supported."); + ::new (static_cast(data_.data())) CurrentType(_t); + index_ = static_cast(ix); } /// @brief Destroys the contained value if it is destructible. - void destroy_if_necessary() { - const auto destroy_one = [](auto& _t) { - using T = std::remove_cvref_t; - if constexpr (std::is_destructible_v) { - _t.~T(); - } - }; - visit(destroy_one); - } - - /// @brief Helper for visiting without result (non-const). - /// @tparam F The visitor type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _visited Pointer to visitation flag. - template - void do_visit_no_result(F& _f, bool* _visited, - std::integer_sequence) { - auto visit_one = [this](F& _f, bool* _visited, Index<_i>) { - if (!*_visited && index_ == _i) { - _f(get_alternative<_i>()); - *_visited = true; - } - }; - (visit_one(_f, _visited, Index<_is>{}), ...); - } - - /// @brief Helper for visiting without result (const). - /// @tparam F The visitor type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _visited Pointer to visitation flag. - template - void do_visit_no_result(F& _f, bool* _visited, - std::integer_sequence) const { - auto visit_one = [this](F& _f, bool* _visited, Index<_i>) { - if (!*_visited && index_ == _i) { - _f(get_alternative<_i>()); - *_visited = true; - } - }; - (visit_one(_f, _visited, Index<_is>{}), ...); - } - - /// @brief Helper for visiting without result (non-const, const visitor). - /// @tparam F The visitor type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _visited Pointer to visitation flag. - template - void do_visit_no_result(const F& _f, bool* _visited, - std::integer_sequence) { - const auto visit_one = [this](const F& _f, bool* _visited, - Index<_i>) { - if (!*_visited && index_ == _i) { - _f(get_alternative<_i>()); - *_visited = true; - } - }; - (visit_one(_f, _visited, Index<_is>{}), ...); - } - - /// @brief Helper for visiting without result (const, const visitor). - /// @tparam F The visitor type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _visited Pointer to visitation flag. - template - void do_visit_no_result(const F& _f, bool* _visited, - std::integer_sequence) const { - const auto visit_one = [this](const F& _f, bool* _visited, - Index<_i>) { - if (!*_visited && index_ == _i) { - _f(get_alternative<_i>()); - *_visited = true; - } - }; - (visit_one(_f, _visited, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with result (non-const). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to optional result. - template - void do_visit_with_result(F& _f, std::optional* _result, - std::integer_sequence) { - auto visit_one = [this]( - F& _f, std::optional* _result, Index<_i>) { - if (!*_result && index_ == _i) { - _result->emplace(_f(get_alternative<_i>())); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with result (const). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to optional result. - template - void do_visit_with_result(F& _f, std::optional* _result, - std::integer_sequence) const { - auto visit_one = [this]( - F& _f, std::optional* _result, Index<_i>) { - if (!*_result && index_ == _i) { - _result->emplace(_f(get_alternative<_i>())); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with result (non-const, const visitor). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to optional result. - template - void do_visit_with_result(const F& _f, std::optional* _result, - std::integer_sequence) { - const auto visit_one = [this]( - const F& _f, std::optional* _result, - Index<_i>) { - if (!*_result && index_ == _i) { - _result->emplace(_f(get_alternative<_i>())); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with result (const, const visitor). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to optional result. - template - void do_visit_with_result(const F& _f, std::optional* _result, - std::integer_sequence) const { - const auto visit_one = [this]( - const F& _f, std::optional* _result, - Index<_i>) { - if (!*_result && index_ == _i) { - _result->emplace(_f(get_alternative<_i>())); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with reference result (non-const). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to result pointer. - template - void do_visit_with_reference(F& _f, ResultType** _result, - std::integer_sequence) { - const auto visit_one = [this](F& _f, ResultType** _result, - Index<_i>) { - if (!*_result && index_ == _i) { - *_result = &_f(get_alternative<_i>()); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with reference result (const). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to result pointer. - template - void do_visit_with_reference(F& _f, ResultType** _result, - std::integer_sequence) const { - const auto visit_one = [this](F& _f, ResultType** _result, - Index<_i>) { - if (!*_result && index_ == _i) { - *_result = &_f(get_alternative<_i>()); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with reference result (non-const, const - /// visitor). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to result pointer. - template - void do_visit_with_reference(const F& _f, ResultType** _result, - std::integer_sequence) { - const auto visit_one = [this]( - const F& _f, ResultType** _result, Index<_i>) { - if (!*_result && index_ == _i) { - *_result = &_f(get_alternative<_i>()); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); - } - - /// @brief Helper for visiting with reference result (const, const visitor). - /// @tparam F The visitor type. - /// @tparam ResultType The result type. - /// @tparam _is Index sequence. - /// @param _f The visitor. - /// @param _result Pointer to result pointer. - template - void do_visit_with_reference(const F& _f, ResultType** _result, - std::integer_sequence) const { - const auto visit_one = [this]( - const F& _f, ResultType** _result, Index<_i>) { - if (!*_result && index_ == _i) { - *_result = &_f(get_alternative<_i>()); - } - }; - (visit_one(_f, _result, Index<_is>{}), ...); + void destroy_if_necessary() noexcept { + if constexpr (!(std::is_trivially_destructible_v && + ...)) { + internal::variant::SwitchDispatch< + true, internal::variant::DestroyAction, void, 0, + AlternativeTypes...>::apply(static_cast(index_), + data_.data()); + } } /// @brief Returns a reference to the contained value of the _i-th alternative /// type. /// @tparam _i The index of the alternative type. /// @return Reference to the contained value. - template + template auto& get_alternative() noexcept { using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>; return *internal::ptr_cast(data_.data()); @@ -517,7 +269,7 @@ class Variant { /// alternative type. /// @tparam _i The index of the alternative type. /// @return Const reference to the contained value. - template + template const auto& get_alternative() const noexcept { using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>; return *internal::ptr_cast(data_.data()); @@ -526,22 +278,25 @@ class Variant { /// @brief Moves the value from another Variant into this Variant. /// @param _other The Variant to move from. void move_from_other(Variant&& _other) noexcept { - const auto move_one = [this](auto&& _t) { - this->move_from_type(std::forward>(_t)); - }; - std::move(_other).visit(move_one); + internal::variant::SwitchDispatch< + true, internal::variant::MoveAction, void, 0, + AlternativeTypes...>::apply(static_cast(_other.index_), + data_.data(), _other.data_.data()); + index_ = _other.index_; } /// @brief Moves a value of type T into the Variant. /// @tparam T The type to move from. /// @param _t The value to move. template - void move_from_type(T&& _t) noexcept { + void move_from_type(T&& _t) noexcept( + std::is_nothrow_constructible_v, T&&>) { using CurrentType = std::remove_cvref_t; - index_ = - internal::element_index...>(); - new (data_.data()) CurrentType(std::forward(_t)); + constexpr int ix = internal::element_index< + CurrentType, std::remove_cvref_t...>(); + static_assert(ix != -1, "Type not supported."); + ::new (static_cast(data_.data())) CurrentType(std::forward(_t)); + index_ = static_cast(ix); } private: @@ -553,6 +308,43 @@ class Variant { alignas(AlternativeTypes...) DataType data_; }; +namespace internal::variant { + +struct Access { + template + static auto& get_alternative(Variant& _v) noexcept { + return _v.template get_alternative<_i>(); + } + + template + static const auto& get_alternative(const Variant& _v) noexcept { + return _v.template get_alternative<_i>(); + } + + template + static T* ptr_as(Variant& _v) noexcept { + return internal::ptr_cast(_v.data_.data()); + } + + template + static const T* ptr_as(const Variant& _v) noexcept { + return internal::ptr_cast(_v.data_.data()); + } + + template + static bool equals(const Variant& _lhs, + const Variant& _rhs) { + if (_lhs.index_ != _rhs.index_) { + return false; + } + return SwitchDispatch::apply( + static_cast(_lhs.index_), _lhs.data_.data(), + _rhs.data_.data()); + } +}; + +} // namespace internal::variant + /// @brief Concept to check if a type is Variant-based. /// @tparam V The type to check. template @@ -568,15 +360,27 @@ concept VariantBased = requires(std::decay_t v) { /// @return Pointer to the contained value or nullptr. template constexpr T* get_if(Variant* _v) noexcept { - const auto get = [](auto& _v) -> T* { - using Type = std::remove_cvref_t; - if constexpr (std::is_same>()) { - return &_v; - } else { - return nullptr; + if (!_v) { + return nullptr; + } + constexpr auto matches = + internal::variant::get_if_matches(); + constexpr auto plan = internal::variant::analyze_get_if_matches(matches); + if constexpr (plan.count == 0) { + return nullptr; + } else if constexpr (plan.count == 1) { + return _v->index() == plan.first_index + ? &internal::variant::Access::template get_alternative< + plan.first_index>(*_v) + : nullptr; + } else { + const int idx = _v->index(); + if (idx >= 0 && static_cast(idx) < matches.size() && + matches[static_cast(idx)]) { + return internal::variant::Access::template ptr_as(*_v); } - }; - return _v->visit(get); + return nullptr; + } } /// @brief Returns a const pointer to the contained value if it is of type T, @@ -587,15 +391,27 @@ constexpr T* get_if(Variant* _v) noexcept { /// @return Const pointer to the contained value or nullptr. template constexpr const T* get_if(const Variant* _v) noexcept { - const auto get = [](const auto& _v) -> const T* { - using Type = std::remove_cvref_t; - if constexpr (std::is_same>()) { - return &_v; - } else { - return nullptr; + if (!_v) { + return nullptr; + } + constexpr auto matches = + internal::variant::get_if_matches(); + constexpr auto plan = internal::variant::analyze_get_if_matches(matches); + if constexpr (plan.count == 0) { + return nullptr; + } else if constexpr (plan.count == 1) { + return _v->index() == plan.first_index + ? &internal::variant::Access::template get_alternative< + plan.first_index>(*_v) + : nullptr; + } else { + const int idx = _v->index(); + if (idx >= 0 && static_cast(idx) < matches.size() && + matches[static_cast(idx)]) { + return internal::variant::Access::template ptr_as(*_v); } - }; - return _v->visit(get); + return nullptr; + } } /// @brief Returns a pointer to the contained value if it is of the N-th @@ -606,8 +422,11 @@ constexpr const T* get_if(const Variant* _v) noexcept { /// @return Pointer to the contained value or nullptr. template constexpr auto* get_if(Variant* _v) noexcept { - using T = internal::nth_element_t<_i, Types...>; - return get_if(_v); + static_assert(_i >= 0 && _i < static_cast(sizeof...(Types)), + "Index out of bounds."); + return (_v && _v->index() == _i) + ? &internal::variant::Access::template get_alternative<_i>(*_v) + : nullptr; } /// @brief Returns a const pointer to the contained value if it is of the N-th @@ -618,8 +437,11 @@ constexpr auto* get_if(Variant* _v) noexcept { /// @return Const pointer to the contained value or nullptr. template constexpr auto* get_if(const Variant* _v) noexcept { - using T = internal::nth_element_t<_i, Types...>; - return get_if(_v); + static_assert(_i >= 0 && _i < static_cast(sizeof...(Types)), + "Index out of bounds."); + return (_v && _v->index() == _i) + ? &internal::variant::Access::template get_alternative<_i>(*_v) + : nullptr; } /// @brief Returns a reference to the contained value if it is of type T, throws diff --git a/include/rfl/internal/element_index.hpp b/include/rfl/internal/element_index.hpp index 09085a7bc..45db26ebf 100644 --- a/include/rfl/internal/element_index.hpp +++ b/include/rfl/internal/element_index.hpp @@ -1,38 +1,31 @@ #ifndef RFL_INTERNAL_ELEMENT_INDEX_HPP_ #define RFL_INTERNAL_ELEMENT_INDEX_HPP_ +#include +#include #include -#include namespace rfl::internal { -template -struct ElementIndexWrapper { - static constexpr int index = _i; - - consteval auto operator||(auto other) const { - if constexpr (std::is_same_v) { - return *this; - } else { - return other; - } - } -}; - /** * Finds the index of the first type T in the list of AlternativeTypes. * If T is not found, returns -1. */ template consteval int element_index() { - return [](std::integer_sequence) { - return ((ElementIndexWrapper, - std::remove_cvref_t, - _is>{} || - ...) || - ElementIndexWrapper{}) - .index; - }(std::make_integer_sequence()); + if constexpr (sizeof...(AlternativeTypes) == 0) { + return -1; + } else { + using Target = std::remove_cvref_t; + constexpr std::array matches{ + std::is_same_v>...}; + for (std::size_t i = 0; i < matches.size(); ++i) { + if (matches[i]) { + return static_cast(i); + } + } + return -1; + } } } // namespace rfl::internal diff --git a/include/rfl/internal/variant/dispatch.hpp b/include/rfl/internal/variant/dispatch.hpp new file mode 100644 index 000000000..782512409 --- /dev/null +++ b/include/rfl/internal/variant/dispatch.hpp @@ -0,0 +1,329 @@ +#ifndef RFL_INTERNAL_VARIANT_DISPATCH_HPP_ +#define RFL_INTERNAL_VARIANT_DISPATCH_HPP_ + +#include +#include +#include +#include +#include + +#include "../always_inline.hpp" +#include "../ptr_cast.hpp" + +// MSVC /W4 /WX emits C4714 when it declines to inline a __forceinline +// function, including at template instantiation sites outside header-local +// warning push/pop regions. Use plain inline on MSVC while keeping +// RFL_ALWAYS_INLINE on Clang/GCC for flat jump-table merging and zero emitted +// dispatch helper symbols. +#if defined(_MSC_VER) && !defined(__clang__) +#define RFL_VARIANT_DISPATCH_INLINE inline +#else +#define RFL_VARIANT_DISPATCH_INLINE RFL_ALWAYS_INLINE +#endif + +namespace rfl::internal::variant { + +struct Access; + +[[noreturn]] RFL_VARIANT_DISPATCH_INLINE void unreachable() noexcept { +#if defined(_MSC_VER) && !defined(__clang__) + __assume(false); +#elif defined(__GNUC__) || defined(__clang__) + __builtin_unreachable(); +#else + for (;;) { + } +#endif +} + +struct GetIfPlan { + int first_index = -1; + std::size_t count = 0; +}; + +template +consteval std::array get_if_matches() { + if constexpr (sizeof...(AlternativeTypes) == 0) { + return {}; + } else { + constexpr std::array exact{ + std::is_same_v...}; + for (const bool m : exact) { + if (m) { + return exact; + } + } + if constexpr (_is_const_variant) { + return {(std::is_same_v, + std::remove_cv_t> && + std::is_convertible_v)...}; + } else { + return {(std::is_same_v, + std::remove_cv_t> && + std::is_convertible_v)...}; + } + } +} + +template +consteval GetIfPlan analyze_get_if_matches( + const std::array& _matches) { + GetIfPlan plan{}; + if constexpr (_n > 0) { + for (std::size_t i = 0; i < _n; ++i) { + if (_matches[i]) { + if (plan.first_index == -1) { + plan.first_index = static_cast(i); + } + ++plan.count; + } + } + } + return plan; +} + +template +struct VisitAction { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply(StoragePtr _data, + F&& _f) { + using Ptr = std::conditional_t<_is_const, const Alt*, Alt*>; + if constexpr (std::is_void_v) { + std::forward(_f)(*internal::ptr_cast(_data)); + } else { + return std::forward(_f)(*internal::ptr_cast(_data)); + } + } +}; + +struct CopyAction { + template + RFL_VARIANT_DISPATCH_INLINE static void apply(unsigned char* _dst, + const unsigned char* _src) { + ::new (static_cast(_dst)) + Alt(*internal::ptr_cast(_src)); + } +}; + +struct MoveAction { + template + RFL_VARIANT_DISPATCH_INLINE static void apply(unsigned char* _dst, + unsigned char* _src) noexcept { + ::new (static_cast(_dst)) + Alt(std::move(*internal::ptr_cast(_src))); + } +}; + +struct DestroyAction { + template + RFL_VARIANT_DISPATCH_INLINE static void apply(unsigned char* _data) noexcept { + if constexpr (!std::is_trivially_destructible_v && + std::is_destructible_v) { + internal::ptr_cast(_data)->~Alt(); + } + } +}; + +struct EqualsAction { + template + RFL_VARIANT_DISPATCH_INLINE static bool apply(const unsigned char* _lhs, + const unsigned char* _rhs) { + return *internal::ptr_cast(_lhs) == + *internal::ptr_cast(_rhs); + } +}; + +// Inline multi-way switch dispatcher over a variant's active index. +// Avoids function-pointer trampoline tables (which block inlining/constant +// folding and bloat symbols on small variants) and avoids nth_element_t or +// integer_sequence folds (O(K/16) instantiations with zero extra symbols). +template +struct SwitchDispatch; + +template +struct SwitchDispatch<_noexcept, Action, ResultType, _offset, T0> { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply( + std::size_t, Args&&... _args) noexcept(_noexcept) { + if constexpr (std::is_void_v) { + Action::template apply(std::forward(_args)...); + } else { + return Action::template apply(std::forward(_args)...); + } + } +}; + +template +struct SwitchDispatch<_noexcept, Action, ResultType, _offset, T0, T1> { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply( + const std::size_t _index, Args&&... _args) noexcept(_noexcept) { + if (_index == _offset) { + if constexpr (std::is_void_v) { + Action::template apply(std::forward(_args)...); + return; + } else { + return Action::template apply(std::forward(_args)...); + } + } + if constexpr (std::is_void_v) { + Action::template apply(std::forward(_args)...); + } else { + return Action::template apply(std::forward(_args)...); + } + } +}; + +#define RFL_VARIANT_CASE(K, T) \ + case _offset + (K): \ + if constexpr (std::is_void_v) { \ + Action::template apply(std::forward(_args)...); \ + return; \ + } else { \ + return Action::template apply(std::forward(_args)...); \ + } + +template +struct SwitchDispatch<_noexcept, Action, ResultType, _offset, T0, T1, T2> { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply( + const std::size_t _index, Args&&... _args) noexcept(_noexcept) { + switch (_index) { + RFL_VARIANT_CASE(0, T0) + RFL_VARIANT_CASE(1, T1) + RFL_VARIANT_CASE(2, T2) + default: + unreachable(); + } + } +}; + +template +struct SwitchDispatch<_noexcept, Action, ResultType, _offset, T0, T1, T2, T3> { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply( + const std::size_t _index, Args&&... _args) noexcept(_noexcept) { + switch (_index) { + RFL_VARIANT_CASE(0, T0) + RFL_VARIANT_CASE(1, T1) + RFL_VARIANT_CASE(2, T2) + RFL_VARIANT_CASE(3, T3) + default: + unreachable(); + } + } +}; + +template +struct SwitchDispatch<_noexcept, Action, ResultType, _offset, T0, T1, T2, T3, + T4, Rest...> { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply( + const std::size_t _index, Args&&... _args) noexcept(_noexcept) { + switch (_index) { + RFL_VARIANT_CASE(0, T0) + RFL_VARIANT_CASE(1, T1) + RFL_VARIANT_CASE(2, T2) + RFL_VARIANT_CASE(3, T3) + default: + if constexpr (std::is_void_v) { + SwitchDispatch<_noexcept, Action, ResultType, _offset + 4, T4, + Rest...>::apply(_index, std::forward(_args)...); + return; + } else { + return SwitchDispatch<_noexcept, Action, ResultType, _offset + 4, T4, + Rest...>::apply(_index, + std::forward(_args)...); + } + } + } +}; + +template +struct SwitchDispatch<_noexcept, Action, ResultType, _offset, T0, T1, T2, T3, + T4, T5, T6, T7, Rest...> { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply( + const std::size_t _index, Args&&... _args) noexcept(_noexcept) { + switch (_index) { + RFL_VARIANT_CASE(0, T0) + RFL_VARIANT_CASE(1, T1) + RFL_VARIANT_CASE(2, T2) + RFL_VARIANT_CASE(3, T3) + RFL_VARIANT_CASE(4, T4) + RFL_VARIANT_CASE(5, T5) + RFL_VARIANT_CASE(6, T6) + RFL_VARIANT_CASE(7, T7) + default: + if constexpr (sizeof...(Rest) == 0) { + unreachable(); + } else if constexpr (std::is_void_v) { + SwitchDispatch<_noexcept, Action, ResultType, _offset + 8, + Rest...>::apply(_index, std::forward(_args)...); + return; + } else { + return SwitchDispatch<_noexcept, Action, ResultType, _offset + 8, + Rest...>::apply(_index, + std::forward(_args)...); + } + } + } +}; + +template +struct SwitchDispatch<_noexcept, Action, ResultType, _offset, T0, T1, T2, T3, + T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, + Rest...> { + template + RFL_VARIANT_DISPATCH_INLINE static ResultType apply( + const std::size_t _index, Args&&... _args) noexcept(_noexcept) { + switch (_index) { + RFL_VARIANT_CASE(0, T0) + RFL_VARIANT_CASE(1, T1) + RFL_VARIANT_CASE(2, T2) + RFL_VARIANT_CASE(3, T3) + RFL_VARIANT_CASE(4, T4) + RFL_VARIANT_CASE(5, T5) + RFL_VARIANT_CASE(6, T6) + RFL_VARIANT_CASE(7, T7) + RFL_VARIANT_CASE(8, T8) + RFL_VARIANT_CASE(9, T9) + RFL_VARIANT_CASE(10, T10) + RFL_VARIANT_CASE(11, T11) + RFL_VARIANT_CASE(12, T12) + RFL_VARIANT_CASE(13, T13) + RFL_VARIANT_CASE(14, T14) + RFL_VARIANT_CASE(15, T15) + default: + if constexpr (sizeof...(Rest) == 0) { + unreachable(); + } else if constexpr (std::is_void_v) { + SwitchDispatch<_noexcept, Action, ResultType, _offset + 16, + Rest...>::apply(_index, std::forward(_args)...); + return; + } else { + return SwitchDispatch<_noexcept, Action, ResultType, _offset + 16, + Rest...>::apply(_index, + std::forward(_args)...); + } + } + } +}; + +#undef RFL_VARIANT_CASE +#undef RFL_VARIANT_DISPATCH_INLINE + +} // namespace rfl::internal::variant + +#endif diff --git a/include/rfl/internal/variant/find_max_size.hpp b/include/rfl/internal/variant/find_max_size.hpp index c43ed669c..9b52e01dc 100644 --- a/include/rfl/internal/variant/find_max_size.hpp +++ b/include/rfl/internal/variant/find_max_size.hpp @@ -1,29 +1,27 @@ #ifndef RFL_INTERNAL_VARIANT_FIND_MAX_SIZE_HPP_ #define RFL_INTERNAL_VARIANT_FIND_MAX_SIZE_HPP_ -namespace rfl::internal::variant { +#include +#include -template -struct SizeWrapper { - using Type = T; - static constexpr unsigned long size_ = sizeof(T); -}; +namespace rfl::internal::variant { -template -consteval auto operator|(const SizeWrapper& _s1, - const SizeWrapper& _s2) { - if constexpr (sizeof(T2) > sizeof(T1)) { - return _s2; +template +consteval std::size_t find_max_size() { + if constexpr (sizeof...(Types) == 0) { + return 0; } else { - return _s1; + constexpr std::array sizes{sizeof(Types)...}; + std::size_t max_size = 0; + for (const std::size_t s : sizes) { + if (s > max_size) { + max_size = s; + } + } + return max_size; } } -template -consteval auto find_max_size() { - return (SizeWrapper{} | ... | SizeWrapper{}); -} - } // namespace rfl::internal::variant #endif diff --git a/include/rfl/internal/variant/result_t.hpp b/include/rfl/internal/variant/result_t.hpp index 78af725b6..9d9dc9f91 100644 --- a/include/rfl/internal/variant/result_t.hpp +++ b/include/rfl/internal/variant/result_t.hpp @@ -8,6 +8,44 @@ namespace rfl::internal::variant { +template +using visit_result_t = + std::remove_cv_t()(std::declval()))>; + +template +concept visit_void_invocable = requires(F&& _f) { + ((void)std::forward(_f)(std::declval()), ...); +}; + +template +concept visit_nonvoid_invocable = + requires(F&& _f, void (*_sink)(ResultType)) { + (_sink(std::forward(_f)(std::declval())), ...); + }; + +template +concept visit_branches_invocable = + (std::is_void_v && visit_void_invocable) || + (!std::is_void_v && + visit_nonvoid_invocable); + +template +concept visit_refs_invocable = + requires { typename visit_result_t; } && + visit_branches_invocable, F, AltRefs...>; + +template +concept visit_invocable = + visit_refs_invocable&, + AlternativeTypes&...>; + +template +concept visit_const_invocable = + visit_invocable || + visit_refs_invocable< + F, const internal::nth_element_t<0, AlternativeTypes...>&, + const AlternativeTypes&...>; + template using result_t = std::remove_cv_t>()(