Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 149 additions & 87 deletions include/rfl/Tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define RFL_TUPLE_HPP_

#include <array>
#include <compare>
#include <cstddef>
#include <new>
#include <tuple>
#include <type_traits>
#include <utility>
Expand All @@ -13,6 +15,23 @@

namespace rfl {

namespace internal::tuple {

template <class T>
requires(std::is_trivially_destructible_v<std::remove_cv_t<T>> ||
!std::is_destructible_v<std::remove_cv_t<T>>)
inline void destroy_element(unsigned char*) noexcept {}

template <class T>
requires(std::is_destructible_v<std::remove_cv_t<T>> &&
!std::is_trivially_destructible_v<std::remove_cv_t<T>>)
inline void destroy_element(unsigned char* _ptr) noexcept {
using RawT = std::remove_cv_t<T>;
internal::ptr_cast<T*>(_ptr)->~RawT();
}

} // namespace internal::tuple

template <class... FieldTypes>
class Tuple;

Expand All @@ -27,14 +46,17 @@ class Tuple<> {
/// Tuple class template for arbitrary types.
template <class... Types>
class Tuple {
template <class... OtherTypes>
friend class Tuple;

static constexpr size_t size_ = sizeof...(Types);

static constexpr auto positions_ =
internal::tuple::calculate_positions<Types...>();

static constexpr auto seq_ = std::make_integer_sequence<int, size_>{};
static constexpr auto seq_ = std::make_index_sequence<size_>{};

static constexpr unsigned int num_bytes_ = std::get<size_>(positions_);
static constexpr size_t num_bytes_ = positions_[size_];

using DataType = unsigned char[num_bytes_];

Expand All @@ -59,25 +81,40 @@ 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<Types> && ...)
= default;

/// Destructor. Destroys all elements if necessary.
~Tuple() { destroy_if_necessary(seq_); }
~Tuple()
requires(!(std::is_trivially_destructible_v<Types> && ...))
{
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 <int _index>
constexpr auto& get() {
static_assert(_index >= 0 && static_cast<size_t>(_index) < size_,
"Tuple index out of bounds.");
using Type = internal::nth_element_t<_index, Types...>;
return *internal::ptr_cast<Type*>(data_ + pos<_index>());
return *internal::ptr_cast<Type*>(data_ +
positions_[static_cast<size_t>(_index)]);
}

/// Gets an element by index (const).
/// @tparam _index The index of the element to access.
/// @return Const reference to the element at the specified index.
template <int _index>
constexpr const auto& get() const {
static_assert(_index >= 0 && static_cast<size_t>(_index) < size_,
"Tuple index out of bounds.");
using Type = internal::nth_element_t<_index, Types...>;
return *internal::ptr_cast<const Type*>(data_ + pos<_index>());
return *internal::ptr_cast<const Type*>(
data_ + positions_[static_cast<size_t>(_index)]);
}

/// Copy assignment operator. Assigns from another Tuple by copy.
Expand Down Expand Up @@ -113,12 +150,11 @@ class Tuple {
bool operator==(const Tuple<OtherTypes...>& _other) const noexcept {
static_assert(sizeof...(Types) == sizeof...(OtherTypes),
"The size of the two tuples must be the same.");
const auto is_same = [&]<int _i>(std::integral_constant<int, _i>) -> bool {
return this->get<_i>() == _other.template get<_i>();
};
return [&]<int... _is>(std::integer_sequence<int, _is...>) {
return (true && ... && is_same(std::integral_constant<int, _is>{}));
}(std::make_integer_sequence<int, sizeof...(Types)>());
if constexpr (sizeof...(Types) == sizeof...(OtherTypes)) {
return is_equal(_other, seq_);
} else {
return false;
}
}

/// Three-way comparison operator. Compares two Tuples lexicographically.
Expand All @@ -129,100 +165,126 @@ class Tuple {
auto operator<=>(const Tuple<OtherTypes...>& _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 <class... OtherTypes, size_t... _is>
bool is_equal(const Tuple<OtherTypes...>& _other,
std::index_sequence<_is...>) const noexcept {
return (static_cast<bool>(
*internal::ptr_cast<const Types*>(data_ + positions_[_is]) ==
*internal::ptr_cast<const OtherTypes*>(
_other.data_ + Tuple<OtherTypes...>::positions_[_is])) &&
...);
}

/// Compares all elements three-way with another Tuple.
template <class... OtherTypes, size_t... _is>
auto compare_three_way(const Tuple<OtherTypes...>& _other,
std::index_sequence<_is...>) const noexcept {
using OrderingType = std::common_comparison_category_t<
decltype(std::declval<Types>() <=> std::declval<OtherTypes>())...>;
decltype(std::declval<const Types&>() <=>
std::declval<const OtherTypes&>())...>;

auto ordering = OrderingType::equivalent;

const auto compare = [&]<int _i>(std::integral_constant<int, _i>) {
ordering = static_cast<OrderingType>(this->get<_i>() <=>
_other.template get<_i>());
return ordering != 0;
};

return [&]<int... _is>(std::integer_sequence<int, _is...>) {
(compare(std::integral_constant<int, _is>{}) || ...);
return ordering;
}(std::make_integer_sequence<int, sizeof...(Types)>());
static_cast<void>(
(((ordering = static_cast<OrderingType>(
*internal::ptr_cast<const Types*>(data_ + positions_[_is]) <=>
*internal::ptr_cast<const OtherTypes*>(
_other.data_ + Tuple<OtherTypes...>::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 <int... _is>
void copy_from_other(const Tuple& _other,
std::integer_sequence<int, _is...>) {
const auto copy_one = [this]<int _i>(const auto& _other,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>()))
Type(_other.template get<_i>());
};
(copy_one(_other, std::integral_constant<int, _is>{}), ...);
template <size_t... _is>
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<Types> && ...) &&
!(std::is_trivially_destructible_v<Types> && ...)) {
size_t constructed = 0;
try {
((::new (static_cast<void*>(data_ + positions_[_is]))
Types(*internal::ptr_cast<const Types*>(_other.data_ +
positions_[_is])),
++constructed),
...);
} catch (...) {
destroy_prefix(constructed, seq_);
throw;
}
} else
#endif
{
(::new (static_cast<void*>(data_ + positions_[_is]))
Types(*internal::ptr_cast<const Types*>(_other.data_ +
positions_[_is])),
...);
}
}

/// Copies all elements from provided arguments.
/// @param _types The values to copy.
/// @param seq_ Integer sequence for element indices.
template <int... _is>
void copy_from_types(const Types&... _types,
std::integer_sequence<int, _is...>) {
const auto copy_one = [this]<int _i>(const auto& _t,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>())) Type(_t);
};
(copy_one(_types, std::integral_constant<int, _is>{}), ...);
template <size_t... _is>
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<Types> && ...) &&
!(std::is_trivially_destructible_v<Types> && ...)) {
size_t constructed = 0;
try {
((::new (static_cast<void*>(data_ + positions_[_is])) Types(_types),
++constructed),
...);
} catch (...) {
destroy_prefix(constructed, seq_);
throw;
}
} else
#endif
{
(::new (static_cast<void*>(data_ + positions_[_is])) Types(_types), ...);
}
}

/// Destroys all elements if they are destructible.
/// @param seq_ Integer sequence for element indices.
template <int... _is>
void destroy_if_necessary(std::integer_sequence<int, _is...>) {
const auto destroy_one = [](auto& _t) {
using Type = std::remove_cvref_t<decltype(_t)>;
if constexpr (std::is_destructible_v<Type>) {
_t.~Type();
}
};
(destroy_one(get<_is>()), ...);
/// Destroys all elements if they are non-trivially destructible.
template <size_t... _is>
void destroy_if_necessary(std::index_sequence<_is...>) noexcept {
if constexpr (!(std::is_trivially_destructible_v<Types> && ...)) {
(internal::tuple::destroy_element<Types>(data_ + positions_[_is]), ...);
}
}

/// Moves all elements from another Tuple.
/// @param _other The Tuple to move from.
/// @param seq_ Integer sequence for element indices.
template <int... _is>
void move_from_other(Tuple&& _other, std::integer_sequence<int, _is...>) {
const auto move_one = [this]<int _i>(auto&& _other,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>()))
Type(std::move(_other.template get<_i>()));
};
(move_one(_other, std::integral_constant<int, _is>{}), ...);
/// Destroys the first _count elements during exception rollback.
template <size_t... _is>
void destroy_prefix(size_t _count, std::index_sequence<_is...>) noexcept {
((_is < _count
? internal::tuple::destroy_element<Types>(data_ + positions_[_is])
: void()),
...);
}

/// Moves all elements from provided arguments.
/// @param _types The values to move.
/// @param seq_ Integer sequence for element indices.
template <int... _is>
void move_from_types(Types&&... _types, std::integer_sequence<int, _is...>) {
const auto move_one = [this]<int _i>(auto&& _t,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>())) Type(std::move(_t));
};
(move_one(std::move(_types), std::integral_constant<int, _is>{}), ...);
/// Moves all elements from another Tuple.
template <size_t... _is>
void move_from_other(Tuple&& _other, std::index_sequence<_is...>) noexcept {
(::new (static_cast<void*>(data_ + positions_[_is]))
Types(std::move(*internal::ptr_cast<Types*>(_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 <int _i>
static consteval unsigned int pos() {
return std::get<_i>(positions_);
/// Moves all elements from provided arguments.
template <size_t... _is>
void move_from_types(Types&&... _types,
std::index_sequence<_is...>) noexcept {
(::new (static_cast<void*>(data_ + positions_[_is]))
Types(std::move(_types)),
...);
}

private:
Expand Down
42 changes: 18 additions & 24 deletions include/rfl/internal/tuple/calculate_positions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@
#define RFL_INTERNAL_TUPLE_CALCULATE_POSITIONS_HPP_

#include <array>
#include <cstddef>

namespace rfl::internal::tuple {

template <class T>
struct PositionWrapper {};

template <unsigned long _last, unsigned long... _is>
struct Positions {
static consteval auto to_array() {
return std::array<unsigned long, sizeof...(_is) + 1>({_is..., _last});
}
};

template <class T, unsigned long _last, unsigned long... _is>
consteval auto operator+(const Positions<_last, _is...>&,
const PositionWrapper<T>&) {
if constexpr (_last % alignof(T) == 0) {
constexpr auto last_new = _last + sizeof(T);
return Positions<last_new, _is..., _last>{};
} else {
constexpr auto last_corrected = _last + alignof(T) - (_last % alignof(T));
constexpr auto last_new = last_corrected + sizeof(T);
return Positions<last_new, _is..., last_corrected>{};
}
}

template <class... Types>
consteval auto calculate_positions() {
return (Positions<0>{} + ... + PositionWrapper<Types>{}).to_array();
std::array<std::size_t, sizeof...(Types) + 1> 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
Expand Down
Loading