handle.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <tuple>
  2. #include <type_traits>
  3. #include <utility>
  4. #include <gtest/gtest.h>
  5. #include <entt/entity/entity.hpp>
  6. #include <entt/entity/handle.hpp>
  7. #include <entt/entity/registry.hpp>
  8. TEST(BasicHandle, Assumptions) {
  9. static_assert(std::is_trivially_copyable_v<entt::handle>);
  10. static_assert(std::is_trivially_assignable_v<entt::handle, entt::handle>);
  11. static_assert(std::is_trivially_destructible_v<entt::handle>);
  12. static_assert(std::is_trivially_copyable_v<entt::const_handle>);
  13. static_assert(std::is_trivially_assignable_v<entt::const_handle, entt::const_handle>);
  14. static_assert(std::is_trivially_destructible_v<entt::const_handle>);
  15. }
  16. TEST(BasicHandle, DeductionGuide) {
  17. static_assert(std::is_same_v<decltype(entt::basic_handle{std::declval<entt::registry &>(), {}}), entt::basic_handle<entt::entity>>);
  18. static_assert(std::is_same_v<decltype(entt::basic_handle{std::declval<const entt::registry &>(), {}}), entt::basic_handle<const entt::entity>>);
  19. }
  20. TEST(BasicHandle, Construction) {
  21. entt::registry registry;
  22. const auto entity = registry.create();
  23. entt::handle handle{registry, entity};
  24. entt::const_handle chandle{std::as_const(registry), entity};
  25. ASSERT_FALSE(entt::null == handle.entity());
  26. ASSERT_EQ(entity, handle);
  27. ASSERT_TRUE(handle);
  28. ASSERT_FALSE(entt::null == chandle.entity());
  29. ASSERT_EQ(entity, chandle);
  30. ASSERT_TRUE(chandle);
  31. ASSERT_EQ(handle, chandle);
  32. static_assert(std::is_same_v<entt::registry *, decltype(handle.registry())>);
  33. static_assert(std::is_same_v<const entt::registry *, decltype(chandle.registry())>);
  34. }
  35. TEST(BasicHandle, Invalidation) {
  36. entt::handle handle;
  37. ASSERT_FALSE(handle);
  38. ASSERT_EQ(handle.registry(), nullptr);
  39. ASSERT_EQ(handle.entity(), entt::entity{entt::null});
  40. entt::registry registry;
  41. const auto entity = registry.create();
  42. handle = {registry, entity};
  43. ASSERT_TRUE(handle);
  44. ASSERT_NE(handle.registry(), nullptr);
  45. ASSERT_NE(handle.entity(), entt::entity{entt::null});
  46. handle = {};
  47. ASSERT_FALSE(handle);
  48. ASSERT_EQ(handle.registry(), nullptr);
  49. ASSERT_EQ(handle.entity(), entt::entity{entt::null});
  50. }
  51. TEST(BasicHandle, Destruction) {
  52. using traits_type = entt::entt_traits<entt::entity>;
  53. entt::registry registry;
  54. const auto entity = registry.create();
  55. entt::handle handle{registry, entity};
  56. ASSERT_TRUE(handle);
  57. ASSERT_TRUE(handle.valid());
  58. ASSERT_NE(handle.registry(), nullptr);
  59. ASSERT_EQ(handle.entity(), entity);
  60. handle.destroy(traits_type::to_version(entity));
  61. ASSERT_FALSE(handle);
  62. ASSERT_FALSE(handle.valid());
  63. ASSERT_NE(handle.registry(), nullptr);
  64. ASSERT_EQ(handle.entity(), entity);
  65. ASSERT_EQ(registry.current(entity), typename entt::registry::version_type{});
  66. handle = entt::handle{registry, registry.create()};
  67. ASSERT_TRUE(handle);
  68. ASSERT_TRUE(handle.valid());
  69. ASSERT_NE(handle.registry(), nullptr);
  70. ASSERT_EQ(handle.entity(), entity);
  71. handle.destroy();
  72. ASSERT_FALSE(handle);
  73. ASSERT_FALSE(handle.valid());
  74. ASSERT_NE(handle.registry(), nullptr);
  75. ASSERT_EQ(handle.entity(), entity);
  76. ASSERT_NE(registry.current(entity), typename entt::registry::version_type{});
  77. }
  78. TEST(BasicHandle, Comparison) {
  79. entt::registry registry;
  80. const auto entity = registry.create();
  81. entt::handle handle{registry, entity};
  82. entt::const_handle chandle = handle;
  83. ASSERT_NE(handle, entt::handle{});
  84. ASSERT_FALSE(handle == entt::handle{});
  85. ASSERT_TRUE(handle != entt::handle{});
  86. ASSERT_NE(chandle, entt::const_handle{});
  87. ASSERT_FALSE(chandle == entt::const_handle{});
  88. ASSERT_TRUE(chandle != entt::const_handle{});
  89. ASSERT_EQ(handle, chandle);
  90. ASSERT_TRUE(handle == chandle);
  91. ASSERT_FALSE(handle != chandle);
  92. ASSERT_EQ(entt::handle{}, entt::const_handle{});
  93. ASSERT_TRUE(entt::handle{} == entt::const_handle{});
  94. ASSERT_FALSE(entt::handle{} != entt::const_handle{});
  95. handle = {};
  96. chandle = {};
  97. ASSERT_EQ(handle, entt::handle{});
  98. ASSERT_TRUE(handle == entt::handle{});
  99. ASSERT_FALSE(handle != entt::handle{});
  100. ASSERT_EQ(chandle, entt::const_handle{});
  101. ASSERT_TRUE(chandle == entt::const_handle{});
  102. ASSERT_FALSE(chandle != entt::const_handle{});
  103. entt::registry other;
  104. const auto entt = other.create();
  105. handle = {registry, entity};
  106. chandle = {other, entt};
  107. ASSERT_NE(handle, chandle);
  108. ASSERT_FALSE(chandle == handle);
  109. ASSERT_TRUE(chandle != handle);
  110. ASSERT_EQ(handle.entity(), chandle.entity());
  111. ASSERT_NE(handle.registry(), chandle.registry());
  112. }
  113. TEST(BasicHandle, Component) {
  114. entt::registry registry;
  115. const auto entity = registry.create();
  116. entt::handle_view<int, char, double> handle{registry, entity};
  117. ASSERT_EQ(3, handle.emplace<int>(3));
  118. ASSERT_EQ('c', handle.emplace_or_replace<char>('c'));
  119. ASSERT_EQ(.3, handle.emplace_or_replace<double>(.3));
  120. const auto &patched = handle.patch<int>([](auto &comp) { comp = 42; });
  121. ASSERT_EQ(42, patched);
  122. ASSERT_EQ('a', handle.replace<char>('a'));
  123. ASSERT_TRUE((handle.all_of<int, char, double>()));
  124. ASSERT_EQ((std::make_tuple(42, 'a', .3)), (handle.get<int, char, double>()));
  125. handle.erase<char, double>();
  126. ASSERT_TRUE(registry.storage<char>().empty());
  127. ASSERT_TRUE(registry.storage<double>().empty());
  128. ASSERT_EQ(0u, (handle.remove<char, double>()));
  129. handle.visit([&handle](const auto id, const auto &pool) {
  130. ASSERT_EQ(id, entt::type_id<int>().hash());
  131. ASSERT_TRUE(pool.contains(handle.entity()));
  132. });
  133. ASSERT_TRUE((handle.any_of<int, char, double>()));
  134. ASSERT_FALSE((handle.all_of<int, char, double>()));
  135. ASSERT_FALSE(handle.orphan());
  136. ASSERT_EQ(1u, (handle.remove<int>()));
  137. ASSERT_TRUE(registry.storage<int>().empty());
  138. ASSERT_TRUE(handle.orphan());
  139. ASSERT_EQ(42, handle.get_or_emplace<int>(42));
  140. ASSERT_EQ(42, handle.get_or_emplace<int>(1));
  141. ASSERT_EQ(42, handle.get<int>());
  142. ASSERT_EQ(42, *handle.try_get<int>());
  143. ASSERT_EQ(nullptr, handle.try_get<char>());
  144. ASSERT_EQ(nullptr, std::get<1>(handle.try_get<int, char, double>()));
  145. }
  146. TEST(BasicHandle, FromEntity) {
  147. entt::registry registry;
  148. const auto entity = registry.create();
  149. registry.emplace<int>(entity, 42);
  150. registry.emplace<char>(entity, 'c');
  151. entt::handle handle{registry, entity};
  152. ASSERT_TRUE(handle);
  153. ASSERT_EQ(entity, handle.entity());
  154. ASSERT_TRUE((handle.all_of<int, char>()));
  155. ASSERT_EQ(handle.get<int>(), 42);
  156. ASSERT_EQ(handle.get<char>(), 'c');
  157. }
  158. TEST(BasicHandle, Lifetime) {
  159. entt::registry registry;
  160. const auto entity = registry.create();
  161. auto *handle = new entt::handle{registry, entity};
  162. handle->emplace<int>();
  163. ASSERT_FALSE(registry.storage<int>().empty());
  164. ASSERT_FALSE(registry.empty());
  165. registry.each([handle](const auto e) {
  166. ASSERT_EQ(handle->entity(), e);
  167. });
  168. delete handle;
  169. ASSERT_FALSE(registry.storage<int>().empty());
  170. ASSERT_FALSE(registry.empty());
  171. }
  172. TEST(BasicHandle, ImplicitConversions) {
  173. entt::registry registry;
  174. const entt::handle handle{registry, registry.create()};
  175. const entt::const_handle chandle = handle;
  176. const entt::handle_view<int, char> vhandle = handle;
  177. const entt::const_handle_view<int> cvhandle = vhandle;
  178. handle.emplace<int>(42);
  179. ASSERT_EQ(handle.get<int>(), chandle.get<int>());
  180. ASSERT_EQ(chandle.get<int>(), vhandle.get<int>());
  181. ASSERT_EQ(vhandle.get<int>(), cvhandle.get<int>());
  182. ASSERT_EQ(cvhandle.get<int>(), 42);
  183. }