15 #ifndef _ENTITY_COMPONENT_SATURATEDPOOL_H_INCLUDED_
16 #define _ENTITY_COMPONENT_SATURATEDPOOL_H_INCLUDED_
18 #include <boost/bind/bind.hpp>
19 #include <boost/bind/placeholders.hpp>
20 #include <boost/iterator/iterator_facade.hpp>
21 #include <boost/signals2.hpp>
22 #include <boost/signals2/connection.hpp>
29 #include "entity/config.hpp"
36 struct forward_traversal_tag;
42 namespace entity {
namespace component
44 template<
typename ComponentPool>
46 template<
typename ComponentPool>
47 class destruction_queue;
55 : boost::iterator_facade<
58 , boost::forward_traversal_tag
71 friend class boost::iterator_core_access;
74 typedef T* parent_iterator;
88 bool equal(iterator_impl
const& other)
const
90 return entity_index_ == other.entity_index_;
93 T& dereference()
const
95 return parent_->components_[entity_index_];
109 template<
typename EntityListIterator>
111 : boost::iterator_facade<
112 entity_iterator<EntityListIterator>
114 , boost::forward_traversal_tag
122 return *entity_iter_;
137 , entity_iter_(
std::move(entity_iter))
147 return entity_iter_ == other.entity_iter_;
150 T& dereference()
const
156 EntityListIterator entity_iter_;
181 data_ = data_begin_ + e.index();
185 value_type&
get()
const
200 : data_begin_(&parent->components_[0])
201 , data_(&parent->components_[0])
204 value_type* data_begin_;
212 components_.resize(owner_pool.
size(), default_value);
214 slots_.entity_destroy_handler =
217 &saturated_pool::handle_destroy_entity,
224 slots_.entity_swap_handler =
227 &saturated_pool::handle_swap_entity,
236 #if ENTITY_SUPPORT_VARIADICS
237 template<
typename... Args>
240 slots_.entity_create_handler =
242 std::function<
void(
entity)>(
243 [
this, constructor_args...](
entity e)
245 create(e, constructor_args...);
254 slots_.entity_create_handler =
267 #if ENTITY_SUPPORT_VARIADICS
268 template<
typename... Args>
271 components_.emplace(components_.begin() + e.index(), std::forward<Args>(args)...);
272 return &components_[e.index()];
277 components_.emplace(components_.begin() + e.index(), std::move(original));
278 return &components_[e.index()];
284 components_.erase(components_.begin() + e.index());
289 return get_component(e);
294 return get_component(e);
304 return iterator(
this, components_.size());
314 return components_.size();
328 boost::signals2::scoped_connection entity_create_handler;
329 boost::signals2::scoped_connection entity_destroy_handler;
330 boost::signals2::scoped_connection entity_swap_handler;
333 T* get_component(
entity e)
335 return &components_[e.index()];
338 T
const* get_component(
entity e)
const
340 return &components_[e.index()];
345 template<
typename Iter>
346 void create_range(Iter first, Iter last)
350 create(first->first.lock().get(), std::move(first->second));
355 template<
typename Iter>
356 void destroy_range(Iter current, Iter last)
358 while(current != last)
360 destroy(current->lock().get());
367 void handle_destroy_entity(
entity e)
375 swap(components_[a.index()], components_[b.index()]);
378 std::vector<type> components_;
383 #endif // _ENTITY_COMPONENT_SATURATEDPOOL_H_INCLUDED_
boost::signals2::signal< void(entity)> on_entity_destroy
T * create(entity e, Args &&...args)
saturated_pool(entity_pool &owner_pool, T const &default_value=T())
void swap(unique_entity &a, unique_entity &b)
entity_index_t index() const
std::size_t entity_index_t
void auto_create_components(entity_pool &owner_pool, Args...constructor_args)
boost::signals2::signal< void(entity)> on_entity_create
entity get_entity() const
bool is_entity(entity) const
friend class boost::iterator_core_access
entity make_entity(entity_index_t idx) BOOST_NOEXCEPT_OR_NOTHROW
friend class saturated_pool
boost::signals2::signal< void(entity, entity)> on_entity_swap