EnTT: src/entt/entity/helper.hpp Source File

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>
22 class as_view {
23 template<typename... Get, typename... Exclude>
24 [[nodiscard]] auto dispatch(get_t<Get...>, exclude_t<Exclude...>) const {
25 return reg->template view<constness_as_t<typename Get::element_type, Get>...>(exclude_t<constness_as_t<typename Exclude::element_type, Exclude>...>{});
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>
61 class 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 {
67 return reg->template group<constness_as_t<typename Owned::element_type, Owned>...>(get_t<constness_as_t<typename Get::element_type, Get>...>{}, exclude_t<constness_as_t<typename Exclude::element_type, Exclude>...>{});
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>
92 operator basic_group<Owned, Get, Exclude>() const {
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)>>>
108 void 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>
126 typename 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...>
144 struct sigh_helper;
145
150template<typename Registry>
151 struct sigh_helper<Registry> {
153 using registry_type = Registry;
154
159 sigh_helper(registry_type &ref)
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>
191 struct sigh_helper<Registry, Type> final: sigh_helper<Registry> {
193 using registry_type = Registry;
194
200 sigh_helper(registry_type &ref, const id_type id = type_hash<Type>::value())
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>
252sigh_helper(Registry &) -> sigh_helper<Registry>;
253
254} // namespace entt
255
256#endif
entt::as_group::entity_type
typename registry_type::entity_type entity_type
Underlying entity identifier.
Definition helper.hpp:75
entt::as_group::registry_type
Registry registry_type
Type of registry to convert.
Definition helper.hpp:73
entt::as_group::as_group
as_group(registry_type &source) noexcept
Constructs a converter for a given registry.
Definition helper.hpp:81
entt::as_view::as_view
as_view(registry_type &source) noexcept
Constructs a converter for a given registry.
Definition helper.hpp:38
entt::as_view::entity_type
typename registry_type::entity_type entity_type
Underlying entity identifier.
Definition helper.hpp:32
entt::as_view::registry_type
Registry registry_type
Type of registry to convert.
Definition helper.hpp:30
entt::basic_group
Group.
Definition fwd.hpp:53
entt::basic_storage
Storage implementation.
Definition storage.hpp:229
entt::basic_storage::rbegin
const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the beginning.
Definition storage.hpp:610
entt::basic_storage::value_type
element_type value_type
Type of the objects assigned to entities.
Definition storage.hpp:404
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::member_class_t
typename member_class< Member >::type member_class_t
Helper type.
Definition type_traits.hpp:870
entt::basic_view
basic_view(Type &...storage) -> basic_view< get_t< Type... >, exclude_t<> >
Deduction guide.
entt::to_entity
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
entt::null
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:375
entt::view
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
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
entt::group
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
entt::get
constexpr get_t< Type... > get
Variable template for lists of observed elements.
Definition fwd.hpp:167
entt::registry
basic_registry<> registry
Alias declaration for the most common use case.
Definition fwd.hpp:95
entt::invoke
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
entt::sigh_helper
sigh_helper(Registry &) -> sigh_helper< Registry >
Deduction guide.
entt::any_policy::ref
@ ref
Aliasing mode, the object points to a non-const element.
Definition fwd.hpp:19
entt::storage
basic_storage< Type > storage
Alias declaration for the most common use case.
Definition fwd.hpp:78
entt::component_traits
Common way to access various properties of components.
Definition component.hpp:43
entt::exclude_t
Alias for exclusion lists.
Definition fwd.hpp:140
entt::get_t
Alias for lists of observed elements.
Definition fwd.hpp:157
entt::owned_t
Alias for lists of owned elements.
Definition fwd.hpp:174
entt::sigh_helper< Registry, Type >::on_destroy
auto on_destroy(Args &&...args)
Forwards the call to on_destroy on the underlying storage.
Definition helper.hpp:238
entt::sigh_helper< Registry, Type >::on_construct
auto on_construct(Args &&...args)
Forwards the call to on_construct on the underlying storage.
Definition helper.hpp:212
entt::sigh_helper< Registry, Type >::registry_type
Registry registry_type
Registry type.
Definition helper.hpp:193
entt::sigh_helper< Registry, Type >::on_update
auto on_update(Args &&...args)
Forwards the call to on_update on the underlying storage.
Definition helper.hpp:225
entt::sigh_helper< Registry, Type >::sigh_helper
sigh_helper(registry_type &ref, const id_type id=type_hash< Type >::value())
Constructs a helper for a given registry.
Definition helper.hpp:200
entt::sigh_helper< Registry >::sigh_helper
sigh_helper(registry_type &ref)
Constructs a helper for a given registry.
Definition helper.hpp:159
entt::sigh_helper< Registry >::registry_type
Registry registry_type
Registry type.
Definition helper.hpp:153
entt::sigh_helper< Registry >::registry
registry_type & registry() noexcept
Returns a reference to the underlying registry.
Definition helper.hpp:177
entt::sigh_helper< Registry >::with
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
entt::sigh_helper
Primary template isn't defined on purpose.
Definition helper.hpp:144
entt::type_hash::value
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.
Definition type_info.hpp:101