2#ifndef LIBSBX_STORAGE_HPP_
3#define LIBSBX_STORAGE_HPP_
12#include <libsbx/utility/type_name.hpp>
14#include <libsbx/memory/concepts.hpp>
15#include <libsbx/memory/observer_ptr.hpp>
16#include <libsbx/memory/iterable_adaptor.hpp>
19#include <libsbx/ecs/component.hpp>
20#include <libsbx/ecs/meta.hpp>
22#include <libsbx/ecs/detail/storage_iterator.hpp>
26template<
typename Type,
typename Entity, memory::allocator_for<Type> Allocator = std::allocator<Type>>
29 using allocator_traits = std::allocator_traits<Allocator>;
30 using container_type = std::vector<typename allocator_traits::pointer, memory::rebound_allocator_t<Allocator, typename allocator_traits::pointer>>;
37 using allocator_type = Allocator;
39 using element_type = Type;
40 using value_type = element_type;
41 using entity_type = Entity;
42 using size_type = std::size_t;
43 using difference_type = std::ptrdiff_t;
44 using pointer =
typename container_type::pointer;
45 using const_pointer =
typename allocator_traits::template rebind_traits<typename allocator_traits::const_pointer>::const_pointer;
48 using reverse_iterator = std::reverse_iterator<iterator>;
49 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
53 static constexpr auto storage_policy =
static_cast<deletion_policy>(component_traits::in_place_delete);
71 constexpr auto get_allocator()
const noexcept -> allocator_type;
75 [[nodiscard]]
auto get(
const entity_type entity)
const noexcept ->
const value_type&;
77 [[nodiscard]]
auto get(
const entity_type entity)
noexcept -> value_type&;
79 [[nodiscard]]
auto get_as_tuple(
const entity_type entity)
const noexcept -> std::tuple<const value_type&>;
81 [[nodiscard]]
auto get_as_tuple(
const entity_type entity)
noexcept -> std::tuple<value_type&>;
87 [[nodiscard]] auto begin() noexcept ->
iterator;
93 [[nodiscard]] auto end() noexcept ->
iterator;
95 template<typename... Args>
96 requires (std::is_constructible_v<value_type, Args...>)
97 auto emplace(const entity_type entity, Args&&... args) -> value_type&;
99 template<typename Function>
100 requires (std::is_invocable_v<Function, value_type&>)
101 auto patch(const entity_type entity, Function&& function) -> value_type&;
103 template<typename Iterator>
104 auto insert(
iterator first,
iterator last, const value_type& value = value_type{}) -> iterator;
106 [[nodiscard]]
auto each() noexcept -> iterable;
108 [[nodiscard]] auto each() const noexcept -> const_iterable;
110 template<typename Callable>
111 requires (std::is_invocable_r_v<
void, Callable, const entity_type, Type&>)
112 auto add_meta(const utility::hashed_string& tag, Callable&& callable) ->
void;
116 auto call(const utility::hashed_string& tag, const entity_type entity) ->
void override;
118 auto pop(underlying_iterator first, underlying_iterator last) ->
void override;
120 auto pop_all() ->
void override;
122 auto try_emplace([[maybe_unused]] const entity_type entity, [[maybe_unused]] const
bool force_back) -> underlying_iterator override;
126 auto _swap_or_move([[maybe_unused]] const std::
size_t from, [[maybe_unused]] const std::
size_t to) ->
void override;
128 auto _element_at(const std::
size_t position) const -> const value_type&;
130 auto _element_at(const std::
size_t position) -> value_type&;
132 auto _assure_at_least(const std::
size_t position);
134 template<typename... Args>
135 requires (std::is_constructible_v<value_type, Args...>)
136 auto _emplace_element(const entity_type entity, const
bool force_back, Args&&... args);
138 auto _shrink_to_size(const size_type
size) ->
void;
140 void _swap_at(const size_type lhs, const size_type rhs);
142 void _move_to(const size_type lhs, const size_type rhs);
144 container_type _container;
148template<typename Type, typename Entity, memory::allocator_for<Type> Allocator>
152 using allocator_traits = std::allocator_traits<Allocator>;
157 using allocator_type = Allocator;
159 using element_type = Type;
160 using value_type = void;
161 using entity_type = Entity;
162 using size_type = std::size_t;
163 using difference_type = std::ptrdiff_t;
170 static constexpr auto storage_policy =
static_cast<deletion_policy>(component_traits::in_place_delete);
188 [[nodiscard]]
constexpr auto get_allocator()
const noexcept -> allocator_type;
190 auto get([[maybe_unused]]
const entity_type entity)
const noexcept -> void;
192 [[nodiscard]]
auto get_as_tuple([[maybe_unused]]
const entity_type entity)
const noexcept -> std::tuple<>;
194 template<
typename... Args>
195 auto emplace(
const entity_type entity, Args&& ...) -> void;
197 template<
typename Function>
198 requires (std::is_invocable_v<Function>)
199 void patch([[maybe_unused]]
const entity_type entity, Function&& function);
201 [[nodiscard]]
auto each()
noexcept ->
iterable;
207template<
typename Entity, memory::allocator_for<Entity> Allocator>
210 using allocator_traits = std::allocator_traits<Allocator>;
216 using allocator_type = Allocator;
218 using value_type = void;
219 using entity_type = Entity;
220 using element_type = Entity;
221 using size_type = std::size_t;
222 using difference_type = std::ptrdiff_t;
226 static constexpr auto storage_policy = deletion_policy::swap_only;
242 auto get([[maybe_unused]]
const entity_type entity)
const noexcept -> void;
244 [[nodiscard]]
auto get_as_tuple([[maybe_unused]]
const entity_type entity)
const noexcept -> std::tuple<>;
246 auto generate() -> entity_type;
248 auto generate(
const entity_type hint) -> entity_type;
250 [[nodiscard]]
auto each()
noexcept ->
iterable;
256 auto pop_all() ->
void override;
258 auto try_emplace(
const entity_type hint,
const bool) -> underlying_iterator
override;
262 auto _from_placeholder()
noexcept;
264 auto _next()
noexcept;
266 size_type _placeholder;
272#include <libsbx/ecs/storage.ipp>
Sparse set container for entity identifiers.
Definition: sparse_set.hpp:70
virtual auto capacity() const noexcept -> size_type
Returns dense storage capacity.
Definition: sparse_set.ipp:74
auto size() const noexcept -> size_type
Returns the number of stored entities.
Definition: sparse_set.ipp:84
Definition: storage.hpp:27
auto reserve(const size_type capacity) -> void override
Reserves dense storage capacity.
Definition: sparse_set_iterator.hpp:10
Definition: storage_iterator.hpp:16
Definition: iterable_adaptor.hpp:11
Sparse set container for ECS entity storage.
deletion_policy
Deletion behavior policy for sparse sets.
Definition: sparse_set.hpp:56
Definition: component.hpp:33
Entity traits.
Definition: entity.hpp:73