EnTT 3.14.0
Loading...
Searching...
No Matches
helper.hpp
1#ifndef ENTT_ENTITY_HELPER_HPP
2#define ENTT_ENTITY_HELPER_HPP
3
4#include <memory>
5#include <type_traits>
6#include <utility>
7#include "../core/fwd.hpp"
8#include "../core/type_traits.hpp"
9#include "component.hpp"
10#include "fwd.hpp"
11#include "group.hpp"
12#include "storage.hpp"
13#include "view.hpp"
14
15namespace entt {
16
21template<typename Registry>
22class as_view {
23 template<typename... Get, typename... Exclude>
24 [[nodiscard]] auto dispatch(get_t<Get...>, exclude_t<Exclude...>) const {
26 }
27
28public:
32 using entity_type = typename registry_type::entity_type;
33
40
47 template<typename Get, typename Exclude>
48 operator basic_view<Get, Exclude>() const {
49 return dispatch(Get{}, Exclude{});
50 }
51
52private:
53 registry_type &reg;
54};
55
60template<typename Registry>
61class as_group {
62 template<typename... Owned, typename... Get, typename... Exclude>
64 if constexpr(std::is_const_v<registry_type>) {
65 return reg.template group_if_exists<typename Owned::element_type...>(get_t<typename Get::element_type...>{}, exclude_t<typename Exclude::element_type...>{});
66 } else {
68 }
69 }
70
71public:
75 using entity_type = typename registry_type::entity_type;
76
83
91 template<typename Owned, typename Get, typename Exclude>
93 return dispatch(Owned{}, Get{}, Exclude{});
94 }
95
96private:
97 registry_type &reg;
98};
99
107template<auto Member, typename Registry = std::decay_t<nth_argument_t<0u, decltype(Member)>>>
108void invoke(Registry &reg, const typename Registry::entity_type entt) {
109 static_assert(std::is_member_function_pointer_v<decltype(Member)>, "Invalid pointer to non-static member function");
110 (reg.template get<member_class_t<decltype(Member)>>(entt).*Member)(reg, entt);
111}
112
125template<typename... Args>
126typename basic_storage<Args...>::entity_type to_entity(const basic_storage<Args...> &storage, const typename basic_storage<Args...>::value_type &instance) {
127 using traits_type = component_traits<typename basic_storage<Args...>::value_type>;
128 static_assert(traits_type::page_size != 0u, "Unexpected page size");
129 const typename basic_storage<Args...>::base_type &base = storage;
130 const auto *addr = std::addressof(instance);
131
132 for(auto it = base.rbegin(), last = base.rend(); it < last; it += traits_type::page_size) {
133 if(const auto dist = (addr - std::addressof(storage.get(*it))); dist >= 0 && dist < static_cast<decltype(dist)>(traits_type::page_size)) {
134 return *(it + dist);
135 }
136 }
137
138 return null;
139}
140
142template<typename...>
144
149template<typename Registry>
153
159 : bucket{&ref} {}
160
167 template<typename Type>
169 return sigh_helper<registry_type, Type>{*bucket, id};
170 }
171
177 return *bucket;
178 }
179
180private:
181 registry_type *bucket;
182};
183
189template<typename Registry, typename Type>
193
202
210 template<auto Candidate, typename... Args>
211 auto on_construct(Args &&...args) {
212 this->registry().template on_construct<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
213 return *this;
214 }
215
223 template<auto Candidate, typename... Args>
224 auto on_update(Args &&...args) {
225 this->registry().template on_update<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
226 return *this;
227 }
228
236 template<auto Candidate, typename... Args>
237 auto on_destroy(Args &&...args) {
238 this->registry().template on_destroy<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
239 return *this;
240 }
241
242private:
243 id_type name;
244};
245
250template<typename Registry>
252
253} // namespace entt
254
255#endif
Converts a registry to a group.
Definition helper.hpp:61
typename registry_type::entity_type entity_type
Underlying entity identifier.
Definition helper.hpp:75
Registry registry_type
Type of registry to convert.
Definition helper.hpp:73
as_group(registry_type &source) noexcept
Constructs a converter for a given registry.
Definition helper.hpp:81
Converts a registry to a view.
Definition helper.hpp:22
as_view(registry_type &source) noexcept
Constructs a converter for a given registry.
Definition helper.hpp:38
typename registry_type::entity_type entity_type
Underlying entity identifier.
Definition helper.hpp:32
Registry registry_type
Type of registry to convert.
Definition helper.hpp:30
Storage implementation.
Definition storage.hpp:230
const value_type & get(const entity_type entt) const noexcept
Returns the object assigned to an entity.
Definition storage.hpp:639
element_type value_type
Type of the objects assigned to entities.
Definition storage.hpp:400
View implementation.
Definition fwd.hpp:42
EnTT default namespace.
Definition dense_map.hpp:22
typename member_class< Member >::type member_class_t
Helper type.
constexpr entt_traits< Entity >::entity_type to_entity(const Entity value) noexcept
Returns the entity part once converted to the underlying type.
Definition entity.hpp:188
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:375
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:14
constexpr get_t< Type... > get
Variable template for lists of observed elements.
Definition fwd.hpp:168
basic_registry<> registry
Alias declaration for the most common use case.
Definition fwd.hpp:93
void invoke(Registry &reg, const typename Registry::entity_type entt)
Helper to create a listener that directly invokes a member function.
Definition helper.hpp:108
sigh_helper(Registry &) -> sigh_helper< Registry >
Deduction guide.
@ ref
Aliasing mode, the object points to a non-const element.
basic_storage< Type > storage
Alias declaration for the most common use case.
Definition fwd.hpp:76
Common way to access various properties of components.
Definition component.hpp:42
Alias for exclusion lists.
Definition fwd.hpp:141
Alias for lists of observed elements.
Definition fwd.hpp:158
Alias for lists of owned elements.
Definition fwd.hpp:175
auto on_destroy(Args &&...args)
Forwards the call to on_destroy on the underlying storage.
Definition helper.hpp:237
auto on_construct(Args &&...args)
Forwards the call to on_construct on the underlying storage.
Definition helper.hpp:211
Registry registry_type
Registry type.
Definition helper.hpp:192
auto on_update(Args &&...args)
Forwards the call to on_update on the underlying storage.
Definition helper.hpp:224
sigh_helper(registry_type &ref, const id_type id=type_hash< Type >::value())
Constructs a helper for a given registry.
Definition helper.hpp:199
sigh_helper(registry_type &ref)
Constructs a helper for a given registry.
Definition helper.hpp:158
Registry registry_type
Registry type.
Definition helper.hpp:152
registry_type & registry() noexcept
Returns a reference to the underlying registry.
Definition helper.hpp:176
auto with(const id_type id=type_hash< Type >::value()) noexcept
Binds a properly initialized helper to a given signal type.
Definition helper.hpp:168
Primary template isn't defined on purpose.
Definition helper.hpp:143
Type hash.
Definition type_info.hpp:92