component_pool.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef ENTT_COMPONENT_POOL_HPP
  2. #define ENTT_COMPONENT_POOL_HPP
  3. #include <type_traits>
  4. #include <utility>
  5. #include <vector>
  6. #include <tuple>
  7. #include <memory>
  8. #include <cstddef>
  9. #include <cassert>
  10. #include <algorithm>
  11. namespace entt {
  12. template<typename, typename, typename...>
  13. class ComponentPool;
  14. template<typename Entity, typename Component>
  15. class ComponentPool<Entity, Component> final {
  16. public:
  17. using component_type = Component;
  18. using size_type = std::uint32_t;
  19. using entity_type = Entity;
  20. private:
  21. bool valid(entity_type entity) const noexcept {
  22. return entity < reverse.size() && reverse[entity] < direct.size() && direct[reverse[entity]] == entity;
  23. }
  24. public:
  25. explicit ComponentPool(size_type dim = 4098) noexcept {
  26. assert(!(dim < 0));
  27. data.reserve(dim);
  28. }
  29. ComponentPool(ComponentPool &&) = default;
  30. ~ComponentPool() noexcept {
  31. assert(empty());
  32. }
  33. ComponentPool & operator=(ComponentPool &&) = default;
  34. bool empty() const noexcept {
  35. return data.empty();
  36. }
  37. size_type capacity() const noexcept {
  38. return data.capacity();
  39. }
  40. size_type size() const noexcept {
  41. return data.size();
  42. }
  43. const entity_type * entities() const noexcept {
  44. return direct.data();
  45. }
  46. bool has(entity_type entity) const noexcept {
  47. return valid(entity);
  48. }
  49. const component_type & get(entity_type entity) const noexcept {
  50. assert(valid(entity));
  51. return data[reverse[entity]];
  52. }
  53. component_type & get(entity_type entity) noexcept {
  54. return const_cast<component_type &>(const_cast<const ComponentPool *>(this)->get(entity));
  55. }
  56. template<typename... Args>
  57. component_type & construct(entity_type entity, Args... args) {
  58. assert(!valid(entity));
  59. if(!(entity < reverse.size())) {
  60. reverse.resize(entity+1);
  61. }
  62. reverse[entity] = direct.size();
  63. direct.emplace_back(entity);
  64. data.push_back({ args... });
  65. return data.back();
  66. }
  67. void destroy(entity_type entity) {
  68. assert(valid(entity));
  69. auto last = direct.size() - 1;
  70. reverse[direct[last]] = reverse[entity];
  71. direct[reverse[entity]] = direct[last];
  72. data[reverse[entity]] = std::move(data[last]);
  73. direct.pop_back();
  74. data.pop_back();
  75. }
  76. void reset() {
  77. data.clear();
  78. reverse.resize(0);
  79. direct.clear();
  80. }
  81. private:
  82. std::vector<component_type> data;
  83. std::vector<size_type> reverse;
  84. std::vector<entity_type> direct;
  85. };
  86. template<typename Entity, typename Component, typename... Components>
  87. class ComponentPool final {
  88. template<typename Comp>
  89. using Pool = ComponentPool<Entity, Comp>;
  90. public:
  91. using size_type = typename Pool<Component>::size_type;
  92. using entity_type = typename Pool<Component>::entity_type;
  93. explicit ComponentPool(size_type dim = 4098) noexcept
  94. #ifdef WIN32
  95. : pools(Pool<Component>{dim}, Pool<Components>{dim}...)
  96. #else
  97. : pools{Pool<Component>{dim}, Pool<Components>{dim}...}
  98. #endif
  99. {
  100. assert(!(dim < 0));
  101. }
  102. ComponentPool(const ComponentPool &) = delete;
  103. ComponentPool(ComponentPool &&) = delete;
  104. ComponentPool & operator=(const ComponentPool &) = delete;
  105. ComponentPool & operator=(ComponentPool &&) = delete;
  106. template<typename Comp>
  107. bool empty() const noexcept {
  108. return std::get<Pool<Comp>>(pools).empty();
  109. }
  110. template<typename Comp>
  111. size_type capacity() const noexcept {
  112. return std::get<Pool<Comp>>(pools).capacity();
  113. }
  114. template<typename Comp>
  115. size_type size() const noexcept {
  116. return std::get<Pool<Comp>>(pools).size();
  117. }
  118. template<typename Comp>
  119. const entity_type * entities() const noexcept {
  120. return std::get<Pool<Comp>>(pools).entities();
  121. }
  122. template<typename Comp>
  123. bool has(entity_type entity) const noexcept {
  124. return std::get<Pool<Comp>>(pools).has(entity);
  125. }
  126. template<typename Comp>
  127. const Comp & get(entity_type entity) const noexcept {
  128. return std::get<Pool<Comp>>(pools).get(entity);
  129. }
  130. template<typename Comp>
  131. Comp & get(entity_type entity) noexcept {
  132. return const_cast<Comp &>(const_cast<const ComponentPool *>(this)->get<Comp>(entity));
  133. }
  134. template<typename Comp, typename... Args>
  135. Comp & construct(entity_type entity, Args... args) {
  136. return std::get<Pool<Comp>>(pools).construct(entity, args...);
  137. }
  138. template<typename Comp>
  139. void destroy(entity_type entity) {
  140. std::get<Pool<Comp>>(pools).destroy(entity);
  141. }
  142. template<typename Comp>
  143. void reset() {
  144. std::get<Pool<Comp>>(pools).reset();
  145. }
  146. void reset() {
  147. using accumulator_type = int[];
  148. std::get<Pool<Component>>(pools).reset();
  149. accumulator_type accumulator = { (std::get<Pool<Components>>(pools).reset(), 0)... };
  150. (void)accumulator;
  151. }
  152. private:
  153. std::tuple<Pool<Component>, Pool<Components>...> pools;
  154. };
  155. }
  156. #endif // ENTT_COMPONENT_POOL_HPP