entity_copy.cpp 3.8 KB

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