EnTT 3.13.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 "../signal/delegate.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 auto dispatch(get_t<Get...>, exclude_t<Exclude...>) const {
26 }
27
28public:
30 using registry_type = Registry;
32 using entity_type = typename registry_type::entity_type;
33
38 as_view(registry_type &source) noexcept
39 : reg{source} {}
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::value_type...>(get_t<typename Get::value_type...>{}, exclude_t<typename Exclude::value_type...>{});
66 } else {
68 }
69 }
70
71public:
73 using registry_type = Registry;
75 using entity_type = typename registry_type::entity_type;
76
81 as_group(registry_type &source) noexcept
82 : reg{source} {}
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 delegate<void(Registry &, const typename Registry::entity_type)> func;
111 func.template connect<Member>(reg.template get<member_class_t<decltype(Member)>>(entt));
112 func(reg, entt);
113}
114
127template<typename... Args>
128auto to_entity(const basic_storage<Args...> &storage, const typename basic_storage<Args...>::value_type &instance) -> typename basic_storage<Args...>::entity_type {
129 constexpr auto page_size = basic_storage<Args...>::traits_type::page_size;
130 const typename basic_storage<Args...>::base_type &base = storage;
131 const auto *addr = std::addressof(instance);
132
133 for(auto it = base.rbegin(), last = base.rend(); it < last; it += page_size) {
134 if(const auto dist = (addr - std::addressof(storage.get(*it))); dist >= 0 && dist < static_cast<decltype(dist)>(page_size)) {
135 return *(it + dist);
136 }
137 }
138
139 return null;
140}
141
150template<typename... Args, typename Component>
151[[deprecated("use storage based to_entity instead")]] typename basic_registry<Args...>::entity_type to_entity(const basic_registry<Args...> &reg, const Component &instance) {
152 if(const auto *storage = reg.template storage<Component>(); storage) {
153 return to_entity(*storage, instance);
154 }
155
156 return null;
157}
158
160template<typename...>
162
167template<typename Registry>
170 using registry_type = Registry;
171
177 : bucket{&ref} {}
178
185 template<typename Type>
186 auto with(const id_type id = type_hash<Type>::value()) noexcept {
187 return sigh_helper<registry_type, Type>{*bucket, id};
188 }
189
194 [[nodiscard]] registry_type &registry() noexcept {
195 return *bucket;
196 }
197
198private:
199 registry_type *bucket;
200};
201
207template<typename Registry, typename Type>
210 using registry_type = Registry;
211
218 : sigh_helper<Registry>{ref},
219 name{id} {}
220
228 template<auto Candidate, typename... Args>
229 auto on_construct(Args &&...args) {
230 this->registry().template on_construct<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
231 return *this;
232 }
233
241 template<auto Candidate, typename... Args>
242 auto on_update(Args &&...args) {
243 this->registry().template on_update<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
244 return *this;
245 }
246
254 template<auto Candidate, typename... Args>
255 auto on_destroy(Args &&...args) {
256 this->registry().template on_destroy<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
257 return *this;
258 }
259
260private:
261 id_type name;
262};
263
268template<typename Registry>
270
271} // namespace entt
272
273#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
Fast and reliable entity-component system.
Definition registry.hpp:233
Basic storage implementation.
Definition storage.hpp:229
const value_type & get(const entity_type entt) const noexcept
Returns the object assigned to an entity.
Definition storage.hpp:627
Type value_type
Type of the objects assigned to entities.
Definition storage.hpp:394
View implementation.
Definition fwd.hpp:38
Basic delegate implementation.
Definition delegate.hpp:51
EnTT default namespace.
Definition dense_map.hpp:21
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:181
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:13
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:363
typename member_class< Member >::type member_class_t
Helper type.
basic_storage< Type > storage
Alias declaration for the most common use case.
Definition fwd.hpp:72
constexpr get_t< Type... > get
Variable template for lists of observed components.
Definition fwd.hpp:157
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
basic_registry<> registry
Alias declaration for the most common use case.
Definition fwd.hpp:82
@ ref
Aliasing mode, the object points to a non-const element.
Alias for exclusion lists.
Definition fwd.hpp:130
Alias for lists of observed components.
Definition fwd.hpp:147
Alias for lists of owned components.
Definition fwd.hpp:164
auto on_destroy(Args &&...args)
Forwards the call to on_destroy on the underlying storage.
Definition helper.hpp:255
auto on_construct(Args &&...args)
Forwards the call to on_construct on the underlying storage.
Definition helper.hpp:229
Registry registry_type
Registry type.
Definition helper.hpp:210
auto on_update(Args &&...args)
Forwards the call to on_update on the underlying storage.
Definition helper.hpp:242
sigh_helper(registry_type &ref, const id_type id=type_hash< Type >::value())
Constructs a helper for a given registry.
Definition helper.hpp:217
sigh_helper(registry_type &ref)
Constructs a helper for a given registry.
Definition helper.hpp:176
Registry registry_type
Registry type.
Definition helper.hpp:170
registry_type & registry() noexcept
Returns a reference to the underlying registry.
Definition helper.hpp:194
auto with(const id_type id=type_hash< Type >::value()) noexcept
Binds a properly initialized helper to a given signal type.
Definition helper.hpp:186
Primary template isn't defined on purpose.
Definition helper.hpp:161
Type hash.
Definition type_info.hpp:92