entity_copy.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <gtest/gtest.h>
  2. #include <entt/core/hashed_string.hpp>
  3. #include <entt/entity/registry.hpp>
  4. enum class my_entity : entt::id_type {};
  5. TEST(Example, EntityCopy) {
  6. using namespace entt::literals;
  7. entt::registry registry{};
  8. auto &&custom = registry.storage<double>("custom"_hs);
  9. const auto src = registry.create();
  10. const auto dst = registry.create();
  11. const auto other = registry.create();
  12. custom.emplace(src, 1.);
  13. registry.emplace<int>(src, 42);
  14. registry.emplace<char>(src, 'c');
  15. registry.emplace<double>(other, 3.);
  16. ASSERT_TRUE(custom.contains(src));
  17. ASSERT_FALSE(registry.all_of<double>(src));
  18. ASSERT_TRUE((registry.all_of<int, char>(src)));
  19. ASSERT_FALSE((registry.any_of<int, char, double>(dst)));
  20. ASSERT_FALSE(custom.contains(dst));
  21. for(auto [id, storage]: registry.storage()) {
  22. // discard the custom storage because why not, this is just an example after all
  23. if(id != "custom"_hs && storage.contains(src)) {
  24. storage.emplace(dst, storage.get(src));
  25. }
  26. }
  27. ASSERT_TRUE((registry.all_of<int, char>(dst)));
  28. ASSERT_FALSE((registry.all_of<double>(dst)));
  29. ASSERT_FALSE(custom.contains(dst));
  30. ASSERT_EQ(registry.get<int>(dst), 42);
  31. ASSERT_EQ(registry.get<char>(dst), 'c');
  32. }
  33. TEST(Example, DifferentRegistryTypes) {
  34. using namespace entt::literals;
  35. entt::basic_registry<entt::entity> registry{};
  36. entt::basic_registry<my_entity> other{};
  37. /*
  38. TODO These are currently needed to ensure that the source and
  39. target registries have the proper storage initialized
  40. prior to copying, as this isn't done automatically
  41. when emplacing storages (as is done below).
  42. There is an open issue about this, and these two
  43. lines should be removed when a fix is properly landed.
  44. https://github.com/skypjack/entt/issues/827
  45. */
  46. static_cast<void>(registry.storage<double>());
  47. static_cast<void>(other.storage<int>());
  48. const auto src = registry.create();
  49. const auto dst = other.create();
  50. registry.emplace<int>(src, 42);
  51. registry.emplace<char>(src, 'c');
  52. for(auto [id, storage]: registry.storage()) {
  53. if(auto it = other.storage(id); it != other.storage().end() && storage.contains(src)) {
  54. it->second.emplace(dst, storage.get(src));
  55. }
  56. }
  57. ASSERT_TRUE((registry.all_of<int, char>(src)));
  58. ASSERT_FALSE(other.all_of<char>(dst));
  59. ASSERT_TRUE(other.all_of<int>(dst));
  60. ASSERT_EQ(other.get<int>(dst), 42);
  61. }