poly_storage.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <cstddef>
  2. #include <gtest/gtest.h>
  3. #include <entt/core/utility.hpp>
  4. #include <entt/entity/poly_storage.hpp>
  5. #include <entt/entity/registry.hpp>
  6. template<typename... Type>
  7. entt::type_list<Type...> as_type_list(const entt::type_list<Type...> &);
  8. template<typename Entity>
  9. struct PolyStorage
  10. : entt::type_list_cat_t<
  11. decltype(as_type_list(std::declval<entt::Storage<Entity>>())),
  12. entt::type_list<
  13. void(const Entity *, const Entity *, void *),
  14. void(entt::basic_registry<Entity> &, const Entity, const void *),
  15. const void *(const Entity) const,
  16. void(entt::basic_registry<Entity> &) const>> {
  17. using entity_type = Entity;
  18. using size_type = std::size_t;
  19. template<typename Base>
  20. struct type: entt::Storage<Entity>::template type<Base> {
  21. static constexpr auto base = decltype(as_type_list(std::declval<entt::Storage<Entity>>()))::size;
  22. void erase(entt::basic_registry<Entity> &owner, const entity_type *first, const entity_type *last) {
  23. entt::poly_call<base + 0>(*this, first, last, &owner);
  24. }
  25. void emplace(entt::basic_registry<Entity> &owner, const entity_type entity, const void *instance) {
  26. entt::poly_call<base + 1>(*this, owner, entity, instance);
  27. }
  28. const void *get(const entity_type entity) const {
  29. return entt::poly_call<base + 2>(*this, entity);
  30. }
  31. void copy_to(entt::basic_registry<Entity> &other) const {
  32. entt::poly_call<base + 3>(*this, other);
  33. }
  34. };
  35. template<typename Type>
  36. struct members {
  37. static void emplace(Type &self, entt::basic_registry<Entity> &owner, const entity_type entity, const void *instance) {
  38. self.emplace(owner, entity, *static_cast<const typename Type::value_type *>(instance));
  39. }
  40. static const typename Type::value_type *get(const Type &self, const entity_type entity) {
  41. return &self.get(entity);
  42. }
  43. static void copy_to(const Type &self, entt::basic_registry<entity_type> &other) {
  44. const entt::sparse_set &base = self;
  45. other.template insert<typename Type::value_type>(base.rbegin(), base.rend(), self.rbegin());
  46. }
  47. };
  48. template<typename Type>
  49. using impl = entt::value_list_cat_t<
  50. typename entt::Storage<Entity>::template impl<Type>,
  51. entt::value_list<
  52. &Type::template erase<const entity_type *>,
  53. &members<Type>::emplace,
  54. &members<Type>::get,
  55. &members<Type>::copy_to>>;
  56. };
  57. template<typename Entity>
  58. struct entt::poly_storage_traits<Entity> {
  59. using storage_type = entt::poly<PolyStorage<Entity>>;
  60. };
  61. TEST(PolyStorage, CopyEntity) {
  62. entt::registry registry;
  63. const auto entity = registry.create();
  64. const auto other = registry.create();
  65. registry.emplace<int>(entity, 42);
  66. registry.emplace<char>(entity, 'c');
  67. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  68. ASSERT_FALSE((registry.any_of<int, char>(other)));
  69. registry.visit(entity, [&](const auto &info) {
  70. auto &&storage = registry.storage(info);
  71. storage->emplace(registry, other, storage->get(entity));
  72. });
  73. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  74. ASSERT_TRUE((registry.all_of<int, char>(other)));
  75. ASSERT_EQ(registry.get<int>(entity), registry.get<int>(other));
  76. ASSERT_EQ(registry.get<char>(entity), registry.get<char>(other));
  77. }
  78. TEST(PolyStorage, CopyRegistry) {
  79. entt::registry registry;
  80. entt::registry other;
  81. entt::entity entities[10u];
  82. registry.create(std::begin(entities), std::end(entities));
  83. registry.insert<int>(std::begin(entities), std::end(entities), 42);
  84. registry.insert<char>(std::begin(entities), std::end(entities), 'c');
  85. ASSERT_EQ(registry.size(), 10u);
  86. ASSERT_EQ(other.size(), 0u);
  87. other.assign(registry.data(), registry.data() + registry.size(), registry.released());
  88. registry.visit([&](const auto &info) { std::as_const(registry).storage(info)->copy_to(other); });
  89. ASSERT_EQ(registry.size(), other.size());
  90. ASSERT_EQ((registry.view<int, char>().size_hint()), (other.view<int, char>().size_hint()));
  91. ASSERT_NE((other.view<int, char>().size_hint()), 0u);
  92. for(const auto entity: registry.view<int, char>()) {
  93. ASSERT_EQ((registry.get<int, char>(entity)), (other.get<int, char>(entity)));
  94. }
  95. }
  96. TEST(PolyStorage, Constness) {
  97. entt::registry registry;
  98. const auto &cregistry = registry;
  99. entt::entity entity[1];
  100. entity[0] = registry.create();
  101. registry.emplace<int>(entity[0], 42);
  102. // cannot invoke erase on a const storage, let's copy the returned value
  103. auto cstorage = cregistry.storage(entt::type_id<int>());
  104. ASSERT_DEATH(cstorage->erase(registry, std::begin(entity), std::end(entity)), "");
  105. ASSERT_TRUE(registry.all_of<int>(entity[0]));
  106. auto &&storage = registry.storage(entt::type_id<int>());
  107. storage->erase(registry, std::begin(entity), std::end(entity));
  108. ASSERT_FALSE(registry.all_of<int>(entity[0]));
  109. }