value_type.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef ENTT_COMMON_VALUE_TYPE_H
  2. #define ENTT_COMMON_VALUE_TYPE_H
  3. #include <compare>
  4. #include <type_traits>
  5. namespace test {
  6. namespace internal {
  7. template<typename Type>
  8. struct pointer_stable_mixin: Type {
  9. static constexpr auto in_place_delete = true;
  10. using Type::Type;
  11. using Type::operator=;
  12. };
  13. template<typename Type>
  14. struct non_default_constructible_mixin: Type {
  15. using Type::Type;
  16. using Type::operator=;
  17. non_default_constructible_mixin() = delete;
  18. };
  19. template<typename Type>
  20. struct non_trivially_destructible_mixin: Type {
  21. using Type::Type;
  22. using Type::operator=;
  23. virtual ~non_trivially_destructible_mixin() noexcept = default;
  24. };
  25. template<typename Type>
  26. struct non_comparable_mixin: Type {
  27. using Type::Type;
  28. using Type::operator=;
  29. bool operator==(const non_comparable_mixin &) const noexcept = delete;
  30. };
  31. template<typename Type>
  32. struct non_movable_mixin: Type {
  33. using Type::Type;
  34. non_movable_mixin(non_movable_mixin &&) noexcept = delete;
  35. non_movable_mixin(const non_movable_mixin &) noexcept = default;
  36. non_movable_mixin &operator=(non_movable_mixin &&) noexcept = delete;
  37. non_movable_mixin &operator=(const non_movable_mixin &) noexcept = default;
  38. };
  39. struct empty_type {};
  40. template<typename Type>
  41. struct value_type {
  42. constexpr value_type() = default;
  43. constexpr value_type(Type elem): value{elem} {}
  44. [[nodiscard]] constexpr bool operator==(const value_type &) const noexcept = default;
  45. [[nodiscard]] constexpr auto operator<=>(const value_type &) const noexcept = default;
  46. operator Type() const noexcept { return value; }
  47. Type value{};
  48. };
  49. } // namespace internal
  50. using pointer_stable = internal::pointer_stable_mixin<internal::value_type<int>>;
  51. using pointer_stable_non_trivially_destructible = internal::pointer_stable_mixin<internal::non_trivially_destructible_mixin<internal::value_type<int>>>;
  52. using non_default_constructible = internal::non_default_constructible_mixin<internal::value_type<int>>;
  53. using non_trivially_destructible = internal::non_trivially_destructible_mixin<internal::value_type<int>>;
  54. using non_comparable = internal::non_comparable_mixin<internal::empty_type>;
  55. using non_movable = internal::non_movable_mixin<internal::value_type<int>>;
  56. using boxed_int = internal::value_type<int>;
  57. using boxed_char = internal::value_type<char>;
  58. using empty = internal::empty_type;
  59. struct other_empty: internal::empty_type {};
  60. static_assert(std::is_trivially_destructible_v<test::pointer_stable>, "Not a trivially destructible type");
  61. static_assert(!std::is_trivially_destructible_v<test::non_trivially_destructible>, "Trivially destructible type");
  62. static_assert(!std::is_trivially_destructible_v<test::pointer_stable_non_trivially_destructible>, "Trivially destructible type");
  63. static_assert(!std::is_move_constructible_v<test::non_movable> && !std::is_move_assignable_v<test::non_movable>, "Movable type");
  64. } // namespace test
  65. #endif