mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
267 lines
9.2 KiB
C++
267 lines
9.2 KiB
C++
#ifndef ENTT_CORE_COMPRESSED_PAIR_HPP
|
|
#define ENTT_CORE_COMPRESSED_PAIR_HPP
|
|
|
|
#include "../stl/concepts.hpp"
|
|
#include "../stl/cstddef.hpp"
|
|
#include "../stl/tuple.hpp"
|
|
#include "../stl/type_traits.hpp"
|
|
#include "../stl/utility.hpp"
|
|
#include "fwd.hpp"
|
|
#include "type_traits.hpp"
|
|
|
|
namespace entt {
|
|
|
|
/*! @cond ENTT_INTERNAL */
|
|
namespace internal {
|
|
|
|
template<typename Type, stl::size_t>
|
|
struct compressed_pair_element {
|
|
using reference = Type &;
|
|
using const_reference = const Type &;
|
|
|
|
// NOLINTNEXTLINE(modernize-use-equals-default)
|
|
constexpr compressed_pair_element() noexcept(stl::is_nothrow_default_constructible_v<Type>)
|
|
requires stl::default_initializable<Type> {}
|
|
|
|
template<typename Arg>
|
|
constexpr compressed_pair_element(Arg &&arg) noexcept(stl::is_nothrow_constructible_v<Type, Arg>)
|
|
requires (!stl::same_as<stl::remove_cvref_t<Arg>, compressed_pair_element>)
|
|
: value{stl::forward<Arg>(arg)} {}
|
|
|
|
template<typename... Args, stl::size_t... Index>
|
|
constexpr compressed_pair_element(stl::tuple<Args...> args, stl::index_sequence<Index...>) noexcept(stl::is_nothrow_constructible_v<Type, Args...>)
|
|
: value{stl::forward<Args>(stl::get<Index>(args))...} {}
|
|
|
|
[[nodiscard]] constexpr reference get() noexcept {
|
|
return value;
|
|
}
|
|
|
|
[[nodiscard]] constexpr const_reference get() const noexcept {
|
|
return value;
|
|
}
|
|
|
|
private:
|
|
Type value{};
|
|
};
|
|
|
|
template<typename Type, stl::size_t Tag>
|
|
requires is_ebco_eligible_v<Type>
|
|
struct compressed_pair_element<Type, Tag>: Type {
|
|
using reference = Type &;
|
|
using const_reference = const Type &;
|
|
using base_type = Type;
|
|
|
|
constexpr compressed_pair_element() noexcept(stl::is_nothrow_default_constructible_v<base_type>)
|
|
requires stl::default_initializable<Type>
|
|
: base_type{} {}
|
|
|
|
template<typename Arg>
|
|
constexpr compressed_pair_element(Arg &&arg) noexcept(stl::is_nothrow_constructible_v<base_type, Arg>)
|
|
requires (!stl::same_as<stl::remove_cvref_t<Arg>, compressed_pair_element>)
|
|
: base_type{stl::forward<Arg>(arg)} {}
|
|
|
|
template<typename... Args, stl::size_t... Index>
|
|
constexpr compressed_pair_element(stl::tuple<Args...> args, stl::index_sequence<Index...>) noexcept(stl::is_nothrow_constructible_v<base_type, Args...>)
|
|
: base_type{stl::forward<Args>(stl::get<Index>(args))...} {}
|
|
|
|
[[nodiscard]] constexpr reference get() noexcept {
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] constexpr const_reference get() const noexcept {
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
} // namespace internal
|
|
/*! @endcond */
|
|
|
|
/**
|
|
* @brief A compressed pair.
|
|
*
|
|
* A pair that exploits the _Empty Base Class Optimization_ (or _EBCO_) to
|
|
* reduce its final size to a minimum.
|
|
*
|
|
* @tparam First The type of the first element that the pair stores.
|
|
* @tparam Second The type of the second element that the pair stores.
|
|
*/
|
|
template<typename First, typename Second>
|
|
class compressed_pair final
|
|
: internal::compressed_pair_element<First, 0u>,
|
|
internal::compressed_pair_element<Second, 1u> {
|
|
using first_base = internal::compressed_pair_element<First, 0u>;
|
|
using second_base = internal::compressed_pair_element<Second, 1u>;
|
|
|
|
public:
|
|
/*! @brief The type of the first element that the pair stores. */
|
|
using first_type = First;
|
|
/*! @brief The type of the second element that the pair stores. */
|
|
using second_type = Second;
|
|
|
|
/**
|
|
* @brief Default constructor, conditionally enabled.
|
|
*
|
|
* This constructor is only available when the types that the pair stores
|
|
* are both at least default constructible.
|
|
*/
|
|
constexpr compressed_pair() noexcept(stl::is_nothrow_default_constructible_v<first_base> && stl::is_nothrow_default_constructible_v<second_base>)
|
|
requires stl::default_initializable<first_type> && stl::default_initializable<second_type>
|
|
: first_base{},
|
|
second_base{} {}
|
|
|
|
/**
|
|
* @brief Copy constructor.
|
|
* @param other The instance to copy from.
|
|
*/
|
|
constexpr compressed_pair(const compressed_pair &other) = default;
|
|
|
|
/**
|
|
* @brief Move constructor.
|
|
* @param other The instance to move from.
|
|
*/
|
|
constexpr compressed_pair(compressed_pair &&other) noexcept = default;
|
|
|
|
/**
|
|
* @brief Constructs a pair from its values.
|
|
* @tparam Arg Type of value to use to initialize the first element.
|
|
* @tparam Other Type of value to use to initialize the second element.
|
|
* @param arg Value to use to initialize the first element.
|
|
* @param other Value to use to initialize the second element.
|
|
*/
|
|
template<typename Arg, typename Other>
|
|
constexpr compressed_pair(Arg &&arg, Other &&other) noexcept(stl::is_nothrow_constructible_v<first_base, Arg> && stl::is_nothrow_constructible_v<second_base, Other>)
|
|
: first_base{stl::forward<Arg>(arg)},
|
|
second_base{stl::forward<Other>(other)} {}
|
|
|
|
/**
|
|
* @brief Constructs a pair by forwarding the arguments to its parts.
|
|
* @tparam Args Types of arguments to use to initialize the first element.
|
|
* @tparam Other Types of arguments to use to initialize the second element.
|
|
* @param args Arguments to use to initialize the first element.
|
|
* @param other Arguments to use to initialize the second element.
|
|
*/
|
|
template<typename... Args, typename... Other>
|
|
constexpr compressed_pair(stl::piecewise_construct_t, stl::tuple<Args...> args, stl::tuple<Other...> other) noexcept(stl::is_nothrow_constructible_v<first_base, Args...> && stl::is_nothrow_constructible_v<second_base, Other...>)
|
|
: first_base{stl::move(args), stl::index_sequence_for<Args...>{}},
|
|
second_base{stl::move(other), stl::index_sequence_for<Other...>{}} {}
|
|
|
|
/*! @brief Default destructor. */
|
|
~compressed_pair() = default;
|
|
|
|
/**
|
|
* @brief Copy assignment operator.
|
|
* @param other The instance to copy from.
|
|
* @return This compressed pair object.
|
|
*/
|
|
constexpr compressed_pair &operator=(const compressed_pair &other) = default;
|
|
|
|
/**
|
|
* @brief Move assignment operator.
|
|
* @param other The instance to move from.
|
|
* @return This compressed pair object.
|
|
*/
|
|
constexpr compressed_pair &operator=(compressed_pair &&other) noexcept = default;
|
|
|
|
/**
|
|
* @brief Returns the first element that a pair stores.
|
|
* @return The first element that a pair stores.
|
|
*/
|
|
[[nodiscard]] constexpr first_type &first() noexcept {
|
|
return static_cast<first_base &>(*this).get();
|
|
}
|
|
|
|
/*! @copydoc first */
|
|
[[nodiscard]] constexpr const first_type &first() const noexcept {
|
|
return static_cast<const first_base &>(*this).get();
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the second element that a pair stores.
|
|
* @return The second element that a pair stores.
|
|
*/
|
|
[[nodiscard]] constexpr second_type &second() noexcept {
|
|
return static_cast<second_base &>(*this).get();
|
|
}
|
|
|
|
/*! @copydoc second */
|
|
[[nodiscard]] constexpr const second_type &second() const noexcept {
|
|
return static_cast<const second_base &>(*this).get();
|
|
}
|
|
|
|
/**
|
|
* @brief Swaps two compressed pair objects.
|
|
* @param other The compressed pair to swap with.
|
|
*/
|
|
constexpr void swap(compressed_pair &other) noexcept {
|
|
using stl::swap;
|
|
swap(first(), other.first());
|
|
swap(second(), other.second());
|
|
}
|
|
|
|
/**
|
|
* @brief Extracts an element from the compressed pair.
|
|
* @tparam Index An integer value that is either 0 or 1.
|
|
* @return Returns a reference to the first element if `Index` is 0 and a
|
|
* reference to the second element if `Index` is 1.
|
|
*/
|
|
template<stl::size_t Index>
|
|
requires (Index <= 1u)
|
|
[[nodiscard]] constexpr decltype(auto) get() noexcept {
|
|
if constexpr(Index == 0u) {
|
|
return first();
|
|
} else {
|
|
return second();
|
|
}
|
|
}
|
|
|
|
/*! @copydoc get */
|
|
template<stl::size_t Index>
|
|
requires (Index <= 1u)
|
|
[[nodiscard]] constexpr decltype(auto) get() const noexcept {
|
|
if constexpr(Index == 0u) {
|
|
return first();
|
|
} else {
|
|
return second();
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @brief Deduction guide.
|
|
* @tparam Type Type of value to use to initialize the first element.
|
|
* @tparam Other Type of value to use to initialize the second element.
|
|
*/
|
|
template<typename Type, typename Other>
|
|
compressed_pair(Type &&, Other &&) -> compressed_pair<stl::decay_t<Type>, stl::decay_t<Other>>;
|
|
|
|
/**
|
|
* @brief Swaps two compressed pair objects.
|
|
* @tparam First The type of the first element that the pairs store.
|
|
* @tparam Second The type of the second element that the pairs store.
|
|
* @param lhs A valid compressed pair object.
|
|
* @param rhs A valid compressed pair object.
|
|
*/
|
|
template<typename First, typename Second>
|
|
constexpr void swap(compressed_pair<First, Second> &lhs, compressed_pair<First, Second> &rhs) noexcept {
|
|
lhs.swap(rhs);
|
|
}
|
|
|
|
} // namespace entt
|
|
|
|
/*! @cond ENTT_INTERNAL */
|
|
#include <utility>
|
|
|
|
namespace std {
|
|
|
|
template<typename First, typename Second>
|
|
struct tuple_size<entt::compressed_pair<First, Second>>: integral_constant<entt::stl::size_t, 2u> {};
|
|
|
|
template<entt::stl::size_t Index, typename First, typename Second>
|
|
requires (Index <= 1u)
|
|
struct tuple_element<Index, entt::compressed_pair<First, Second>>: conditional<Index == 0u, First, Second> {};
|
|
|
|
} // namespace std
|
|
/*! @endcond */
|
|
|
|
#endif
|