entity_copy.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <gtest/gtest.h>
  2. #include <entt/core/hashed_string.hpp>
  3. #include <entt/core/utility.hpp>
  4. #include <entt/entity/registry.hpp>
  5. #include <entt/entity/storage.hpp>
  6. #include <entt/meta/factory.hpp>
  7. #include <entt/meta/meta.hpp>
  8. #include <entt/meta/policy.hpp>
  9. #include <entt/meta/resolve.hpp>
  10. enum class my_entity : entt::id_type {};
  11. template<typename Type>
  12. // NOLINTNEXTLINE(*-exception-escape)
  13. struct meta_mixin: Type {
  14. using allocator_type = typename Type::allocator_type;
  15. using element_type = typename Type::element_type;
  16. explicit meta_mixin(const allocator_type &allocator);
  17. };
  18. template<typename Type, typename Entity>
  19. struct entt::storage_type<Type, Entity> {
  20. using type = meta_mixin<basic_storage<Type, Entity>>;
  21. };
  22. template<typename Type>
  23. meta_mixin<Type>::meta_mixin(const allocator_type &allocator)
  24. : Type{allocator} {
  25. using namespace entt::literals;
  26. entt::meta_factory<element_type>{}
  27. // cross registry, same type
  28. .template func<entt::overload<entt::storage_for_t<element_type, entt::entity> &(const entt::id_type)>(&entt::basic_registry<entt::entity>::storage<element_type>), entt::as_ref_t>("storage"_hs)
  29. // cross registry, different types
  30. .template func<entt::overload<entt::storage_for_t<element_type, my_entity> &(const entt::id_type)>(&entt::basic_registry<my_entity>::storage<element_type>), entt::as_ref_t>("storage"_hs);
  31. }
  32. template<typename Type>
  33. struct EntityCopy: testing::Test {
  34. using type = Type;
  35. };
  36. using EntityCopyTypes = ::testing::Types<entt::basic_registry<entt::entity>, entt::basic_registry<my_entity>>;
  37. TYPED_TEST_SUITE(EntityCopy, EntityCopyTypes, );
  38. TEST(EntityCopy, SameRegistry) {
  39. using namespace entt::literals;
  40. entt::registry registry{};
  41. auto &&custom = registry.storage<double>("custom"_hs);
  42. const auto src = registry.create();
  43. const auto dst = registry.create();
  44. custom.emplace(src, 1.);
  45. registry.emplace<int>(src, 2);
  46. registry.emplace<char>(src, 'c');
  47. ASSERT_EQ(registry.storage<entt::entity>().size(), 2u);
  48. ASSERT_TRUE(custom.contains(src));
  49. ASSERT_FALSE(custom.contains(dst));
  50. ASSERT_TRUE((registry.all_of<int, char>(src)));
  51. ASSERT_FALSE((registry.any_of<int, char>(dst)));
  52. for(auto [id, storage]: registry.storage()) {
  53. // discard the custom storage because why not, this is just an example after all
  54. if(id != "custom"_hs && storage.contains(src)) {
  55. storage.push(dst, storage.value(src));
  56. }
  57. }
  58. ASSERT_EQ(registry.storage<entt::entity>().size(), 2u);
  59. ASSERT_TRUE(custom.contains(src));
  60. ASSERT_FALSE(custom.contains(dst));
  61. ASSERT_TRUE((registry.all_of<int, char>(src)));
  62. ASSERT_TRUE((registry.all_of<int, char>(dst)));
  63. ASSERT_EQ(registry.get<int>(dst), 2);
  64. ASSERT_EQ(registry.get<char>(dst), 'c');
  65. }
  66. TYPED_TEST(EntityCopy, CrossRegistry) {
  67. using namespace entt::literals;
  68. entt::basic_registry<entt::entity> src{};
  69. // other registry type, see typed test suite
  70. typename TestFixture::type dst{};
  71. const auto entity = src.create();
  72. const auto copy = dst.create();
  73. src.emplace<int>(entity, 2);
  74. src.emplace<char>(entity, 'c');
  75. ASSERT_EQ(src.storage<entt::entity>().size(), 1u);
  76. ASSERT_EQ(dst.template storage<typename TestFixture::type::entity_type>().size(), 1u);
  77. ASSERT_TRUE((src.all_of<int, char>(entity)));
  78. ASSERT_FALSE((dst.template all_of<int, char>(copy)));
  79. for(auto [id, storage]: src.storage()) {
  80. if(storage.contains(entity)) {
  81. auto *other = dst.storage(id);
  82. if(!other) {
  83. using namespace entt::literals;
  84. entt::resolve(storage.info()).invoke("storage"_hs, nullptr, entt::forward_as_meta(dst), id);
  85. other = dst.storage(id);
  86. }
  87. other->push(copy, storage.value(entity));
  88. }
  89. }
  90. ASSERT_EQ(src.storage<entt::entity>().size(), 1u);
  91. ASSERT_EQ(dst.template storage<typename TestFixture::type::entity_type>().size(), 1u);
  92. ASSERT_TRUE((src.all_of<int, char>(entity)));
  93. ASSERT_TRUE((dst.template all_of<int, char>(copy)));
  94. ASSERT_EQ(dst.template get<int>(copy), 2);
  95. ASSERT_EQ(dst.template get<char>(copy), 'c');
  96. }