EnTT 3.15.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:
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>
63 [[nodiscard]] auto dispatch(owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>) const {
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:
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 (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, typename basic_storage<Args...>::entity_type>;
128 static_assert(traits_type::page_size != 0u, "Unexpected page size");
129 const auto *page = storage.raw();
130
131 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
132 for(std::size_t pos{}, count = storage.size(); pos < count; pos += traits_type::page_size, ++page) {
133 if(const auto dist = (std::addressof(instance) - *page); dist >= 0 && dist < static_cast<decltype(dist)>(traits_type::page_size)) {
134 return *(static_cast<const typename basic_storage<Args...>::base_type &>(storage).rbegin() + static_cast<decltype(dist)>(pos) + dist);
135 }
136 }
137 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
138
139 return null;
140}
141
143template<typename...>
145
150template<typename Registry>
153 using registry_type = Registry;
154
160 : bucket{&ref} {}
161
168 template<typename Type>
169 auto with(const id_type id = type_hash<Type>::value()) noexcept {
170 return sigh_helper<registry_type, Type>{*bucket, id};
171 }
172
177 [[nodiscard]] registry_type &registry() noexcept {
178 return *bucket;
179 }
180
181private:
182 registry_type *bucket;
183};
184
190template<typename Registry, typename Type>
193 using registry_type = Registry;
194
201 : sigh_helper<Registry>{ref},
202 name{id} {}
203
211 template<auto Candidate, typename... Args>
212 auto on_construct(Args &&...args) {
213 this->registry().template on_construct<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
214 return *this;
215 }
216
224 template<auto Candidate, typename... Args>
225 auto on_update(Args &&...args) {
226 this->registry().template on_update<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
227 return *this;
228 }
229
237 template<auto Candidate, typename... Args>
238 auto on_destroy(Args &&...args) {
239 this->registry().template on_destroy<Type>(name).template connect<Candidate>(std::forward<Args>(args)...);
240 return *this;
241 }
242
243private:
244 id_type name;
245};
246
251template<typename Registry>
253
254} // namespace entt
255
256#endif
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
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:229
const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the beginning.
Definition storage.hpp:610
element_type value_type
Type of the objects assigned to entities.
Definition storage.hpp:404
EnTT default namespace.
Definition dense_map.hpp:22
typename member_class< Member >::type member_class_t
Helper type.
basic_view(Type &...storage) -> basic_view< get_t< Type... >, exclude_t<> >
Deduction guide.
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 null_t null
Compile-time constant for null entities.
Definition entity.hpp:375
basic_view< type_list_transform_t< Get, storage_for >, type_list_transform_t< Exclude, storage_for > > view
Alias declaration for the most common use case.
Definition fwd.hpp:277
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
basic_group< type_list_transform_t< Owned, storage_for >, type_list_transform_t< Get, storage_for >, type_list_transform_t< Exclude, storage_for > > group
Alias declaration for the most common use case.
Definition fwd.hpp:286
constexpr get_t< Type... > get
Variable template for lists of observed elements.
Definition fwd.hpp:167
basic_registry<> registry
Alias declaration for the most common use case.
Definition fwd.hpp:95
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.
Definition fwd.hpp:19
basic_storage< Type > storage
Alias declaration for the most common use case.
Definition fwd.hpp:78
Common way to access various properties of components.
Definition component.hpp:43
Alias for exclusion lists.
Definition fwd.hpp:140
Alias for lists of observed elements.
Definition fwd.hpp:157
Alias for lists of owned elements.
Definition fwd.hpp:174
auto on_destroy(Args &&...args)
Forwards the call to on_destroy on the underlying storage.
Definition helper.hpp:238
auto on_construct(Args &&...args)
Forwards the call to on_construct on the underlying storage.
Definition helper.hpp:212
Registry registry_type
Registry type.
Definition helper.hpp:193
auto on_update(Args &&...args)
Forwards the call to on_update on the underlying storage.
Definition helper.hpp:225
sigh_helper(registry_type &ref, const id_type id=type_hash< Type >::value())
Constructs a helper for a given registry.
Definition helper.hpp:200
sigh_helper(registry_type &ref)
Constructs a helper for a given registry.
Definition helper.hpp:159
Registry registry_type
Registry type.
Definition helper.hpp:153
registry_type & registry() noexcept
Returns a reference to the underlying registry.
Definition helper.hpp:177
auto with(const id_type id=type_hash< Type >::value()) noexcept
Binds a properly initialized helper to a given signal type.
Definition helper.hpp:169
Primary template isn't defined on purpose.
Definition helper.hpp:144
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.