value_type.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. template<typename Type>
  40. struct new_delete_mixin: Type {
  41. static void *operator new(std::size_t count) {
  42. return ::operator new(count);
  43. }
  44. static void operator delete(void *ptr) {
  45. ::operator delete(ptr);
  46. }
  47. };
  48. struct empty_type {};
  49. struct aggregate_type {
  50. [[nodiscard]] constexpr bool operator==(const aggregate_type &) const noexcept = default;
  51. [[nodiscard]] constexpr auto operator<=>(const aggregate_type &) const noexcept = default;
  52. int value{};
  53. };
  54. template<typename Type>
  55. struct value_type {
  56. constexpr value_type() = default;
  57. constexpr value_type(Type elem): value{elem} {}
  58. [[nodiscard]] constexpr bool operator==(const value_type &) const noexcept = default;
  59. [[nodiscard]] constexpr auto operator<=>(const value_type &) const noexcept = default;
  60. operator Type() const noexcept {
  61. return value;
  62. }
  63. Type value{};
  64. };
  65. } // namespace internal
  66. using pointer_stable = internal::pointer_stable_mixin<internal::value_type<int>>;
  67. using pointer_stable_non_trivially_destructible = internal::pointer_stable_mixin<internal::non_trivially_destructible_mixin<internal::value_type<int>>>;
  68. using non_default_constructible = internal::non_default_constructible_mixin<internal::value_type<int>>;
  69. using non_trivially_destructible = internal::non_trivially_destructible_mixin<internal::value_type<int>>;
  70. using non_comparable = internal::non_comparable_mixin<internal::empty_type>;
  71. using non_movable = internal::non_movable_mixin<internal::value_type<int>>;
  72. using new_delete = internal::new_delete_mixin<internal::value_type<int>>;
  73. using boxed_int = internal::value_type<int>;
  74. using boxed_char = internal::value_type<char>;
  75. using empty = internal::empty_type;
  76. struct other_empty: internal::empty_type {};
  77. using aggregate = internal::aggregate_type;
  78. static_assert(std::is_trivially_destructible_v<pointer_stable>, "Not a trivially destructible type");
  79. static_assert(!std::is_trivially_destructible_v<non_trivially_destructible>, "Trivially destructible type");
  80. static_assert(!std::is_trivially_destructible_v<pointer_stable_non_trivially_destructible>, "Trivially destructible type");
  81. static_assert(!std::is_move_constructible_v<non_movable> && !std::is_move_assignable_v<non_movable>, "Movable type");
  82. static_assert(std::is_aggregate_v<aggregate>, "Not an aggregate type");
  83. } // namespace test
  84. #endif