1 #ifndef ENTT_ENTITY_OBSERVER_HPP
2 #define ENTT_ENTITY_OBSERVER_HPP
9 #include "../config/config.h"
10 #include "../core/type_traits.hpp"
11 #include "../signal/delegate.hpp"
14 #include "registry.hpp"
15 #include "storage.hpp"
16 #include "utility.hpp"
49 template<
typename... AllOf,
typename... NoneOf>
59 template<
typename AnyOf>
60 static constexpr
auto update() ENTT_NOEXCEPT {
73 template<
typename... Reject,
typename... Require,
typename... Rule,
typename... Other>
84 template<
typename... AllOf,
typename... NoneOf>
94 template<
typename AnyOf>
95 static constexpr
auto update() ENTT_NOEXCEPT {
105 template<
typename... AllOf,
typename... NoneOf>
164 template<
typename Entity>
166 using payload_type = std::uint32_t;
169 struct matcher_handler;
171 template<
typename... Reject,
typename... Require,
typename AnyOf>
173 template<std::
size_t Index>
175 if(reg.template all_of<Require...>(
entt) && !reg.template any_of<Reject...>(
entt)) {
180 obs.storage.
get(
entt) |= (1 << Index);
184 template<std::
size_t Index>
191 template<std::
size_t Index>
193 (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
194 (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
195 reg.template on_update<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
196 reg.template on_destroy<AnyOf>().template connect<&discard_if<Index>>(obs);
200 (reg.template on_destroy<Require>().disconnect(obs), ...);
201 (reg.template on_construct<Reject>().disconnect(obs), ...);
202 reg.template on_update<AnyOf>().disconnect(obs);
203 reg.template on_destroy<AnyOf>().disconnect(obs);
207 template<
typename... Reject,
typename... Require,
typename... NoneOf,
typename... AllOf>
209 template<std::size_t Index,
typename... Ignore>
211 auto condition = [®,
entt]() {
212 if constexpr(
sizeof...(Ignore) == 0) {
213 return reg.template all_of<AllOf..., Require...>(
entt) && !reg.template any_of<NoneOf..., Reject...>(
entt);
215 return reg.template all_of<AllOf..., Require...>(
entt) && ((std::is_same_v<Ignore..., NoneOf> || !reg.template any_of<NoneOf>(
entt)) && ...) && !reg.template any_of<Reject...>(
entt);
224 obs.storage.
get(
entt) |= (1 << Index);
228 template<std::
size_t Index>
235 template<std::
size_t Index>
237 (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
238 (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
239 (reg.template on_construct<AllOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
240 (reg.template on_destroy<NoneOf>().template connect<&maybe_valid_if<Index, NoneOf>>(obs), ...);
241 (reg.template on_destroy<AllOf>().template connect<&discard_if<Index>>(obs), ...);
242 (reg.template on_construct<NoneOf>().template connect<&discard_if<Index>>(obs), ...);
246 (reg.template on_destroy<Require>().disconnect(obs), ...);
247 (reg.template on_construct<Reject>().disconnect(obs), ...);
248 (reg.template on_construct<AllOf>().disconnect(obs), ...);
249 (reg.template on_destroy<NoneOf>().disconnect(obs), ...);
250 (reg.template on_destroy<AllOf>().disconnect(obs), ...);
251 (reg.template on_construct<NoneOf>().disconnect(obs), ...);
255 template<
typename... Matcher>
257 (matcher_handler<Matcher>::disconnect(obs, reg), ...);
260 template<
typename... Matcher, std::size_t... Index>
262 static_assert(
sizeof...(Matcher) < std::numeric_limits<payload_type>::digits,
"Too many matchers");
263 (matcher_handler<Matcher>::template connect<Index>(*
this, reg), ...);
290 template<
typename... Matcher>
293 connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
316 template<
typename... Matcher>
319 connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
343 [[nodiscard]]
bool empty() const ENTT_NOEXCEPT {
372 return storage.basic_sparse_set<entity_type>::begin();
386 return storage.basic_sparse_set<entity_type>::end();
407 template<
typename Func>
409 for(
const auto entity: *
this) {
423 template<
typename Func>
425 std::as_const(*this).each(std::move(func));
basic_observer & operator=(const basic_observer &)=delete
Default copy assignment operator, deleted on purpose.
void disconnect()
Disconnects an observer from the registry it keeps track of.
std::size_t size_type
Unsigned integer type.
void each(Func func) const
Iterates entities and applies the given function object to them.
iterator begin() const
Returns an iterator to the first entity of the observer.
typename basic_sparse_set< Entity >::iterator iterator
Random access iterator type.
void each(Func func)
Iterates entities and applies the given function object to them, then clears the observer.
bool empty() const
Checks whether an observer is empty.
iterator end() const
Returns an iterator that is past the last entity of the observer.
void connect(basic_registry< entity_type > ®, basic_collector< Matcher... >)
Connects an observer to a given registry.
basic_observer & operator=(basic_observer &&)=delete
Default move assignment operator, deleted on purpose.
const entity_type * data() const
Direct access to the list of entities of the observer.
basic_observer(const basic_observer &)=delete
Default copy constructor, deleted on purpose.
size_type size() const
Returns the number of elements in an observer.
basic_observer(basic_registry< entity_type > ®, basic_collector< Matcher... >)
Creates an observer and connects it to a given registry.
basic_observer(basic_observer &&)=delete
Default move constructor, deleted on purpose.
void clear()
Clears the underlying container.
basic_observer()
Default constructor.
Entity entity_type
Underlying entity identifier.
~basic_observer()=default
Default destructor.
Fast and reliable entity-component system.
void erase(const entity_type entt)
Erases an entity from a sparse set.
bool empty() const
Checks whether a sparse set is empty.
void clear()
Clears a sparse set.
basic_iterator iterator
Random access iterator type.
size_type size() const
Returns the number of elements in a sparse set.
bool contains(const entity_type entt) const
Checks if a sparse set contains an entity.
pointer data() const
Direct access to the internal packed array.
Basic storage implementation.
value_type & emplace(const entity_type entt, Args &&...args)
Assigns an entity to a storage and constructs its object.
const value_type & get(const entity_type entt) const
Returns the object assigned to an entity.
Basic delegate implementation.
entity
Default entity identifier.
constexpr basic_collector collector
Variable template used to ease the definition of collectors.
static constexpr auto where(exclude_t< NoneOf... >={})
Updates the filter of the last added matcher.
static constexpr auto group(exclude_t< NoneOf... >={})
Adds a grouping matcher to the collector.
static constexpr auto update()
Adds an observing matcher to the collector.
static constexpr auto group(exclude_t< NoneOf... >={})
Adds a grouping matcher to the collector.
static constexpr auto update()
Adds an observing matcher to the collector.
Alias for exclusion lists.
A class to use to push around lists of types, nothing more.