entity_copy.cpp 4.1 KB

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