component_pool.hpp 5.0 KB

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