diff --git a/include/rfl/Tuple.hpp b/include/rfl/Tuple.hpp index ee12e54c2..c32d06bd0 100644 --- a/include/rfl/Tuple.hpp +++ b/include/rfl/Tuple.hpp @@ -2,7 +2,9 @@ #define RFL_TUPLE_HPP_ #include +#include #include +#include #include #include #include @@ -13,6 +15,23 @@ namespace rfl { +namespace internal::tuple { + +template + requires(std::is_trivially_destructible_v> || + !std::is_destructible_v>) +inline void destroy_element(unsigned char*) noexcept {} + +template + requires(std::is_destructible_v> && + !std::is_trivially_destructible_v>) +inline void destroy_element(unsigned char* _ptr) noexcept { + using RawT = std::remove_cv_t; + internal::ptr_cast(_ptr)->~RawT(); +} + +} // namespace internal::tuple + template class Tuple; @@ -27,14 +46,17 @@ class Tuple<> { /// Tuple class template for arbitrary types. template class Tuple { + template + friend class Tuple; + static constexpr size_t size_ = sizeof...(Types); static constexpr auto positions_ = internal::tuple::calculate_positions(); - static constexpr auto seq_ = std::make_integer_sequence{}; + static constexpr auto seq_ = std::make_index_sequence{}; - static constexpr unsigned int num_bytes_ = std::get(positions_); + static constexpr size_t num_bytes_ = positions_[size_]; using DataType = unsigned char[num_bytes_]; @@ -59,16 +81,28 @@ class Tuple { /// @param _other The Tuple to move from. Tuple(Tuple&& _other) noexcept { move_from_other(std::move(_other), seq_); } + /// Destructor. Trivial when all elements are trivially destructible. + ~Tuple() + requires(std::is_trivially_destructible_v && ...) + = default; + /// Destructor. Destroys all elements if necessary. - ~Tuple() { destroy_if_necessary(seq_); } + ~Tuple() + requires(!(std::is_trivially_destructible_v && ...)) + { + destroy_if_necessary(seq_); + } /// Gets an element by index (mutable). /// @tparam _index The index of the element to access. /// @return Reference to the element at the specified index. template constexpr auto& get() { + static_assert(_index >= 0 && static_cast(_index) < size_, + "Tuple index out of bounds."); using Type = internal::nth_element_t<_index, Types...>; - return *internal::ptr_cast(data_ + pos<_index>()); + return *internal::ptr_cast(data_ + + positions_[static_cast(_index)]); } /// Gets an element by index (const). @@ -76,8 +110,11 @@ class Tuple { /// @return Const reference to the element at the specified index. template constexpr const auto& get() const { + static_assert(_index >= 0 && static_cast(_index) < size_, + "Tuple index out of bounds."); using Type = internal::nth_element_t<_index, Types...>; - return *internal::ptr_cast(data_ + pos<_index>()); + return *internal::ptr_cast( + data_ + positions_[static_cast(_index)]); } /// Copy assignment operator. Assigns from another Tuple by copy. @@ -113,12 +150,11 @@ class Tuple { bool operator==(const Tuple& _other) const noexcept { static_assert(sizeof...(Types) == sizeof...(OtherTypes), "The size of the two tuples must be the same."); - const auto is_same = [&](std::integral_constant) -> bool { - return this->get<_i>() == _other.template get<_i>(); - }; - return [&](std::integer_sequence) { - return (true && ... && is_same(std::integral_constant{})); - }(std::make_integer_sequence()); + if constexpr (sizeof...(Types) == sizeof...(OtherTypes)) { + return is_equal(_other, seq_); + } else { + return false; + } } /// Three-way comparison operator. Compares two Tuples lexicographically. @@ -129,100 +165,126 @@ class Tuple { auto operator<=>(const Tuple& _other) const noexcept { static_assert(sizeof...(Types) == sizeof...(OtherTypes), "The size of the two tuples must be the same."); + if constexpr (sizeof...(Types) == sizeof...(OtherTypes)) { + return compare_three_way(_other, seq_); + } + } + + private: + /// Compares all elements for equality with another Tuple. + template + bool is_equal(const Tuple& _other, + std::index_sequence<_is...>) const noexcept { + return (static_cast( + *internal::ptr_cast(data_ + positions_[_is]) == + *internal::ptr_cast( + _other.data_ + Tuple::positions_[_is])) && + ...); + } + /// Compares all elements three-way with another Tuple. + template + auto compare_three_way(const Tuple& _other, + std::index_sequence<_is...>) const noexcept { using OrderingType = std::common_comparison_category_t< - decltype(std::declval() <=> std::declval())...>; + decltype(std::declval() <=> + std::declval())...>; auto ordering = OrderingType::equivalent; - - const auto compare = [&](std::integral_constant) { - ordering = static_cast(this->get<_i>() <=> - _other.template get<_i>()); - return ordering != 0; - }; - - return [&](std::integer_sequence) { - (compare(std::integral_constant{}) || ...); - return ordering; - }(std::make_integer_sequence()); + static_cast( + (((ordering = static_cast( + *internal::ptr_cast(data_ + positions_[_is]) <=> + *internal::ptr_cast( + _other.data_ + Tuple::positions_[_is]))) != + 0) || + ...)); + return ordering; } - private: /// Copies all elements from another Tuple. - /// @param _other The Tuple to copy from. - /// @param seq_ Integer sequence for element indices. - template - void copy_from_other(const Tuple& _other, - std::integer_sequence) { - const auto copy_one = [this](const auto& _other, - std::integral_constant) { - using Type = internal::nth_element_t<_i, Types...>; - ::new (static_cast(data_ + pos<_i>())) - Type(_other.template get<_i>()); - }; - (copy_one(_other, std::integral_constant{}), ...); + template + void copy_from_other(const Tuple& _other, std::index_sequence<_is...>) { +#if defined(__cpp_exceptions) || defined(_CPPUNWIND) + if constexpr (size_ > 1 && + !(std::is_nothrow_copy_constructible_v && ...) && + !(std::is_trivially_destructible_v && ...)) { + size_t constructed = 0; + try { + ((::new (static_cast(data_ + positions_[_is])) + Types(*internal::ptr_cast(_other.data_ + + positions_[_is])), + ++constructed), + ...); + } catch (...) { + destroy_prefix(constructed, seq_); + throw; + } + } else +#endif + { + (::new (static_cast(data_ + positions_[_is])) + Types(*internal::ptr_cast(_other.data_ + + positions_[_is])), + ...); + } } /// Copies all elements from provided arguments. - /// @param _types The values to copy. - /// @param seq_ Integer sequence for element indices. - template - void copy_from_types(const Types&... _types, - std::integer_sequence) { - const auto copy_one = [this](const auto& _t, - std::integral_constant) { - using Type = internal::nth_element_t<_i, Types...>; - ::new (static_cast(data_ + pos<_i>())) Type(_t); - }; - (copy_one(_types, std::integral_constant{}), ...); + template + void copy_from_types(const Types&... _types, std::index_sequence<_is...>) { +#if defined(__cpp_exceptions) || defined(_CPPUNWIND) + if constexpr (size_ > 1 && + !(std::is_nothrow_copy_constructible_v && ...) && + !(std::is_trivially_destructible_v && ...)) { + size_t constructed = 0; + try { + ((::new (static_cast(data_ + positions_[_is])) Types(_types), + ++constructed), + ...); + } catch (...) { + destroy_prefix(constructed, seq_); + throw; + } + } else +#endif + { + (::new (static_cast(data_ + positions_[_is])) Types(_types), ...); + } } - /// Destroys all elements if they are destructible. - /// @param seq_ Integer sequence for element indices. - template - void destroy_if_necessary(std::integer_sequence) { - const auto destroy_one = [](auto& _t) { - using Type = std::remove_cvref_t; - if constexpr (std::is_destructible_v) { - _t.~Type(); - } - }; - (destroy_one(get<_is>()), ...); + /// Destroys all elements if they are non-trivially destructible. + template + void destroy_if_necessary(std::index_sequence<_is...>) noexcept { + if constexpr (!(std::is_trivially_destructible_v && ...)) { + (internal::tuple::destroy_element(data_ + positions_[_is]), ...); + } } - /// Moves all elements from another Tuple. - /// @param _other The Tuple to move from. - /// @param seq_ Integer sequence for element indices. - template - void move_from_other(Tuple&& _other, std::integer_sequence) { - const auto move_one = [this](auto&& _other, - std::integral_constant) { - using Type = internal::nth_element_t<_i, Types...>; - ::new (static_cast(data_ + pos<_i>())) - Type(std::move(_other.template get<_i>())); - }; - (move_one(_other, std::integral_constant{}), ...); + /// Destroys the first _count elements during exception rollback. + template + void destroy_prefix(size_t _count, std::index_sequence<_is...>) noexcept { + ((_is < _count + ? internal::tuple::destroy_element(data_ + positions_[_is]) + : void()), + ...); } - /// Moves all elements from provided arguments. - /// @param _types The values to move. - /// @param seq_ Integer sequence for element indices. - template - void move_from_types(Types&&... _types, std::integer_sequence) { - const auto move_one = [this](auto&& _t, - std::integral_constant) { - using Type = internal::nth_element_t<_i, Types...>; - ::new (static_cast(data_ + pos<_i>())) Type(std::move(_t)); - }; - (move_one(std::move(_types), std::integral_constant{}), ...); + /// Moves all elements from another Tuple. + template + void move_from_other(Tuple&& _other, std::index_sequence<_is...>) noexcept { + (::new (static_cast(data_ + positions_[_is])) + Types(std::move(*internal::ptr_cast(_other.data_ + + positions_[_is]))), + ...); } - /// Returns the byte offset of the element at index _i. - /// @tparam _i The index of the element. - /// @return The byte offset of the element. - template - static consteval unsigned int pos() { - return std::get<_i>(positions_); + /// Moves all elements from provided arguments. + template + void move_from_types(Types&&... _types, + std::index_sequence<_is...>) noexcept { + (::new (static_cast(data_ + positions_[_is])) + Types(std::move(_types)), + ...); } private: diff --git a/include/rfl/internal/tuple/calculate_positions.hpp b/include/rfl/internal/tuple/calculate_positions.hpp index 416d3451f..8b5941a4f 100644 --- a/include/rfl/internal/tuple/calculate_positions.hpp +++ b/include/rfl/internal/tuple/calculate_positions.hpp @@ -2,35 +2,29 @@ #define RFL_INTERNAL_TUPLE_CALCULATE_POSITIONS_HPP_ #include +#include namespace rfl::internal::tuple { -template -struct PositionWrapper {}; - -template -struct Positions { - static consteval auto to_array() { - return std::array({_is..., _last}); - } -}; - -template -consteval auto operator+(const Positions<_last, _is...>&, - const PositionWrapper&) { - if constexpr (_last % alignof(T) == 0) { - constexpr auto last_new = _last + sizeof(T); - return Positions{}; - } else { - constexpr auto last_corrected = _last + alignof(T) - (_last % alignof(T)); - constexpr auto last_new = last_corrected + sizeof(T); - return Positions{}; - } -} - template consteval auto calculate_positions() { - return (Positions<0>{} + ... + PositionWrapper{}).to_array(); + std::array positions{}; + if constexpr (sizeof...(Types) > 0) { + constexpr std::size_t sizes[] = {sizeof(Types)...}; + constexpr std::size_t alignments[] = {alignof(Types)...}; + std::size_t current = 0; + for (std::size_t i = 0; i < sizeof...(Types); ++i) { + const std::size_t align = alignments[i]; + const std::size_t rem = current % align; + if (rem != 0) { + current += align - rem; + } + positions[i] = current; + current += sizes[i]; + } + positions[sizeof...(Types)] = current; + } + return positions; } } // namespace rfl::internal::tuple