You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.5 KiB

  1. #pragma once
  2. #include <queue>
  3. #include <ecs/config.h>
  4. #include <ecs/core/utils/bitset.h>
  5. namespace ecs {
  6. namespace core {
  7. namespace entity {
  8. namespace storage {
  9. /**
  10. * struct to represent an entity handle
  11. */
  12. struct entity_handle
  13. {
  14. private:
  15. uint32_t _index;
  16. uint32_t _counter;
  17. public:
  18. /**
  19. * constructor
  20. *
  21. * @param i index the entity is stored at
  22. * @param c counter to check the reusage of the entity
  23. */
  24. inline entity_handle(uint32_t i, uint32_t c);
  25. /**
  26. * get the index of the entity
  27. *
  28. * @return index of the entity
  29. */
  30. inline auto index() const;
  31. /**
  32. * get the counter of the entity
  33. *
  34. * @return counter of the entity
  35. */
  36. inline auto counter() const;
  37. /**
  38. * compare with other handle
  39. *
  40. * @param other handle to compare with
  41. *
  42. * @retval -1 if this is smaller than other
  43. * @retval 0 if this is equal to other
  44. * @retval -1 if this is larger than other
  45. */
  46. inline int compare(const entity_handle& other) const;
  47. /* compare operators */
  48. inline bool operator <=(const entity_handle& other) const
  49. { return compare(other) <= 0; }
  50. inline bool operator <(const entity_handle& other) const
  51. { return compare(other) < 0; }
  52. inline bool operator ==(const entity_handle& other) const
  53. { return compare(other) == 0; }
  54. inline bool operator !=(const entity_handle& other) const
  55. { return compare(other) != 0; }
  56. inline bool operator >(const entity_handle& other) const
  57. { return compare(other) > 0; }
  58. inline bool operator >=(const entity_handle& other) const
  59. { return compare(other) >= 0; }
  60. };
  61. /**
  62. * struct to manage the meta data for each entity
  63. *
  64. * @tparam T_settings settings type the environment is configured with
  65. * @tparam T_storage_meta_data meta data type of the component storagte container
  66. */
  67. template <typename T_settings, typename T_storage_meta_data>
  68. struct entity_meta_data
  69. : private T_storage_meta_data
  70. {
  71. public:
  72. using bitset_type = utils::bitset<T_settings>;
  73. using storage_meta_data_type = T_storage_meta_data;
  74. using counter_type = uint32_t;
  75. private:
  76. bitset_type _bitset;
  77. counter_type _counter;
  78. public:
  79. /**
  80. * constructor
  81. */
  82. inline entity_meta_data();
  83. /**
  84. * get the bitset of components stored for this entity
  85. *
  86. * @return component bitset
  87. */
  88. inline auto& bitset();
  89. /**
  90. * get the bitset of components stored for this entity
  91. *
  92. * @return component bitset
  93. */
  94. inline const auto& bitset() const;
  95. /**
  96. * get the current reusage counter of the entity
  97. *
  98. * @return current reusage counter of the entity
  99. */
  100. inline auto counter() const;
  101. /**
  102. * get the storage meta data for this entity
  103. *
  104. * @return storage meta data for this entity
  105. */
  106. inline auto& storage_meta_data();
  107. /**
  108. * reset all meta data to their defaults
  109. */
  110. inline void reset();
  111. };
  112. /**
  113. * basic entity storage container
  114. *
  115. * @tparam T_container_builder type to build the inner storage container
  116. * @tparam T_settings settings type the environemtn is configured with
  117. * @tparam T_storage_meta_data meta data type of the component storage
  118. */
  119. template<typename T_container_builder, typename T_settings, typename T_storage_meta_data>
  120. struct base
  121. {
  122. public:
  123. using entity_handle_type = entity_handle;
  124. using entity_meta_data_type = entity_meta_data<T_settings, T_storage_meta_data>;
  125. private:
  126. using container_type = typename T_container_builder::template type<entity_meta_data_type>;
  127. using queue_type = std::queue<uint32_t>;
  128. static constexpr decltype(auto) grow_size = 64;
  129. protected:
  130. container_type _container;
  131. queue_type _free_ids;
  132. private:
  133. /**
  134. * grow the container to the given size
  135. *
  136. * @param count new size of the container
  137. */
  138. inline void grow(size_t count);
  139. public:
  140. /**
  141. * check if the given entity handle is valid
  142. *
  143. * @param handle handle to check
  144. *
  145. * @retval TRUE if handle is valid
  146. * @retval FALSE if handle is invalid
  147. */
  148. inline bool is_valid(const entity_handle& handle) const;
  149. /**
  150. * create a new entity inside the container
  151. *
  152. * @return handle of the new created entity
  153. */
  154. inline decltype(auto) claim();
  155. /**
  156. * release an entiy handle
  157. *
  158. * @param handle entity handle to release
  159. */
  160. inline void reclaim(const entity_handle& handle);
  161. /**
  162. * remove all stored entities
  163. */
  164. inline void clear();
  165. /**
  166. * get the meta data stored for the given handle
  167. *
  168. * @param handle entity handle to get meta data for
  169. *
  170. * @return meta data for the given entity handle
  171. */
  172. inline auto& meta_data(const entity_handle& handle);
  173. };
  174. } } } }