|
- #pragma once
-
- #include <queue>
- #include <ecs/config.h>
- #include <ecs/core/utils/bitset.h>
-
- namespace ecs {
- namespace core {
- namespace entity {
- namespace storage {
-
- /**
- * struct to represent an entity handle
- */
- struct entity_handle
- {
- private:
- uint32_t _index;
- uint32_t _counter;
-
- public:
- /**
- * constructor
- *
- * @param i index the entity is stored at
- * @param c counter to check the reusage of the entity
- */
- inline entity_handle(uint32_t i, uint32_t c);
-
- /**
- * get the index of the entity
- *
- * @return index of the entity
- */
- inline auto index() const;
-
- /**
- * get the counter of the entity
- *
- * @return counter of the entity
- */
- inline auto counter() const;
-
- /**
- * compare with other handle
- *
- * @param other handle to compare with
- *
- * @retval -1 if this is smaller than other
- * @retval 0 if this is equal to other
- * @retval -1 if this is larger than other
- */
- inline int compare(const entity_handle& other) const;
-
- /* compare operators */
-
- inline bool operator <=(const entity_handle& other) const
- { return compare(other) <= 0; }
-
- inline bool operator <(const entity_handle& other) const
- { return compare(other) < 0; }
-
- inline bool operator ==(const entity_handle& other) const
- { return compare(other) == 0; }
-
- inline bool operator !=(const entity_handle& other) const
- { return compare(other) != 0; }
-
- inline bool operator >(const entity_handle& other) const
- { return compare(other) > 0; }
-
- inline bool operator >=(const entity_handle& other) const
- { return compare(other) >= 0; }
- };
-
- /**
- * struct to manage the meta data for each entity
- *
- * @tparam T_settings settings type the environment is configured with
- * @tparam T_storage_meta_data meta data type of the component storagte container
- */
- template <typename T_settings, typename T_storage_meta_data>
- struct entity_meta_data
- : private T_storage_meta_data
- {
- public:
- using bitset_type = utils::bitset<T_settings>;
- using storage_meta_data_type = T_storage_meta_data;
- using counter_type = uint32_t;
-
- private:
- bitset_type _bitset;
- counter_type _counter;
-
- public:
- /**
- * constructor
- */
- inline entity_meta_data();
-
- /**
- * get the bitset of components stored for this entity
- *
- * @return component bitset
- */
- inline auto& bitset();
-
- /**
- * get the bitset of components stored for this entity
- *
- * @return component bitset
- */
- inline const auto& bitset() const;
-
- /**
- * get the current reusage counter of the entity
- *
- * @return current reusage counter of the entity
- */
- inline auto counter() const;
-
- /**
- * get the storage meta data for this entity
- *
- * @return storage meta data for this entity
- */
- inline auto& storage_meta_data();
-
- /**
- * reset all meta data to their defaults
- */
- inline void reset();
- };
-
- /**
- * basic entity storage container
- *
- * @tparam T_container_builder type to build the inner storage container
- * @tparam T_settings settings type the environemtn is configured with
- * @tparam T_storage_meta_data meta data type of the component storage
- */
- template<typename T_container_builder, typename T_settings, typename T_storage_meta_data>
- struct base
- {
- public:
- using entity_handle_type = entity_handle;
- using entity_meta_data_type = entity_meta_data<T_settings, T_storage_meta_data>;
-
- private:
- using container_type = typename T_container_builder::template type<entity_meta_data_type>;
- using queue_type = std::queue<uint32_t>;
-
- static constexpr decltype(auto) grow_size = 64;
-
- protected:
- container_type _container;
- queue_type _free_ids;
-
- private:
- /**
- * grow the container to the given size
- *
- * @param count new size of the container
- */
- inline void grow(size_t count);
-
- public:
- /**
- * check if the given entity handle is valid
- *
- * @param handle handle to check
- *
- * @retval TRUE if handle is valid
- * @retval FALSE if handle is invalid
- */
- inline bool is_valid(const entity_handle& handle) const;
-
- /**
- * create a new entity inside the container
- *
- * @return handle of the new created entity
- */
- inline decltype(auto) claim();
-
- /**
- * release an entiy handle
- *
- * @param handle entity handle to release
- */
- inline void reclaim(const entity_handle& handle);
-
- /**
- * remove all stored entities
- */
- inline void clear();
-
- /**
- * get the meta data stored for the given handle
- *
- * @param handle entity handle to get meta data for
- *
- * @return meta data for the given entity handle
- */
- inline auto& meta_data(const entity_handle& handle);
- };
-
- } } } }
|