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

EnTT 3.15.0

Loading...
Searching...
No Matches
organizer.hpp
1#ifndef ENTT_ENTITY_ORGANIZER_HPP
2#define ENTT_ENTITY_ORGANIZER_HPP
3
4#include <cstddef>
5#include <type_traits>
6#include <utility>
7#include <vector>
8#include "../core/type_info.hpp"
9#include "../core/type_traits.hpp"
10#include "../core/utility.hpp"
11#include "../graph/adjacency_matrix.hpp"
12#include "../graph/flow.hpp"
13#include "fwd.hpp"
14#include "helper.hpp"
15
16namespace entt {
17
19namespace internal {
20
21template<typename>
22struct is_view: std::false_type {};
23
24template<typename... Args>
25struct is_view<basic_view<Args...>>: std::true_type {};
26
27template<typename Type>
28inline constexpr bool is_view_v = is_view<Type>::value;
29
30template<typename>
31struct is_group: std::false_type {};
32
33template<typename... Args>
34struct is_group<basic_group<Args...>>: std::true_type {};
35
36template<typename Type>
37inline constexpr bool is_group_v = is_group<Type>::value;
38
39template<typename Type, typename Override>
40struct unpack_type {
41 using ro = std::conditional_t<
42 type_list_contains_v<Override, const Type> || (std::is_const_v<Type> && !type_list_contains_v<Override, std::remove_const_t<Type>>),
43 type_list<std::remove_const_t<Type>>,
44 type_list<>>;
45
46 using rw = std::conditional_t<
47 type_list_contains_v<Override, std::remove_const_t<Type>> || (!std::is_const_v<Type> && !type_list_contains_v<Override, const Type>),
48 type_list<Type>,
49 type_list<>>;
50};
51
52template<typename... Args, typename... Override>
53struct unpack_type<basic_registry<Args...>, type_list<Override...>> {
54 using ro = type_list<>;
55 using rw = type_list<>;
56};
57
58template<typename... Args, typename... Override>
59struct unpack_type<const basic_registry<Args...>, type_list<Override...>>
60 : unpack_type<basic_registry<Args...>, type_list<Override...>> {};
61
62template<typename... Get, typename... Exclude, typename... Override>
63struct unpack_type<basic_view<get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>> {
64 using ro = type_list_cat_t<type_list<typename Exclude::element_type...>, typename unpack_type<constness_as_t<typename Get::element_type, Get>, type_list<Override...>>::ro...>;
65 using rw = type_list_cat_t<typename unpack_type<constness_as_t<typename Get::element_type, Get>, type_list<Override...>>::rw...>;
66};
67
68template<typename... Get, typename... Exclude, typename... Override>
69struct unpack_type<const basic_view<get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>>
70 : unpack_type<basic_view<get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>> {};
71
72template<typename... Owned, typename... Get, typename... Exclude, typename... Override>
73struct unpack_type<basic_group<owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>> {
74 using ro = type_list_cat_t<type_list<typename Exclude::element_type...>, typename unpack_type<constness_as_t<typename Get::element_type, Get>, type_list<Override...>>::ro..., typename unpack_type<constness_as_t<typename Owned::element_type, Owned>, type_list<Override...>>::ro...>;
75 using rw = type_list_cat_t<typename unpack_type<constness_as_t<typename Get::element_type, Get>, type_list<Override...>>::rw..., typename unpack_type<constness_as_t<typename Owned::element_type, Owned>, type_list<Override...>>::rw...>;
76};
77
78template<typename... Owned, typename... Get, typename... Exclude, typename... Override>
79struct unpack_type<const basic_group<owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>>
80 : unpack_type<basic_group<owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>> {};
81
82template<typename, typename>
83struct resource_traits;
84
85template<typename... Args, typename... Req>
86struct resource_traits<type_list<Args...>, type_list<Req...>> {
87 using args = type_list<std::remove_const_t<Args>...>;
88 using ro = type_list_cat_t<typename unpack_type<Args, type_list<Req...>>::ro..., typename unpack_type<Req, type_list<>>::ro...>;
89 using rw = type_list_cat_t<typename unpack_type<Args, type_list<Req...>>::rw..., typename unpack_type<Req, type_list<>>::rw...>;
90};
91
92template<typename... Req, typename Ret, typename... Args>
93resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> free_function_to_resource_traits(Ret (*)(Args...));
94
95template<typename... Req, typename Ret, typename Type, typename... Args>
96resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource_traits(Ret (*)(Type &, Args...));
97
98template<typename... Req, typename Ret, typename Class, typename... Args>
99resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource_traits(Ret (Class::*)(Args...));
100
101template<typename... Req, typename Ret, typename Class, typename... Args>
102resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource_traits(Ret (Class::*)(Args...) const);
103
104} // namespace internal
106
118template<typename Registry>
119 class basic_organizer final {
120 using callback_type = void(const void *, Registry &);
121 using prepare_type = void(Registry &);
122 using dependency_type = std::size_t(const bool, const type_info **, const std::size_t);
123
124 struct vertex_data final {
125 std::size_t ro_count{};
126 std::size_t rw_count{};
127 const char *name{};
128 const void *payload{};
129 callback_type *callback{};
130 dependency_type *dependency{};
131 prepare_type *prepare{};
132 const type_info *info{};
133 };
134
135 template<typename Type>
136 [[nodiscard]] static decltype(auto) extract(Registry &reg) {
137 if constexpr(std::is_same_v<Type, Registry>) {
138 return reg;
139 } else if constexpr(internal::is_view_v<Type>) {
140 return static_cast<Type>(as_view{reg});
141 } else if constexpr(internal::is_group_v<Type>) {
142 return static_cast<Type>(as_group{reg});
143 } else {
144 return reg.ctx().template emplace<std::remove_reference_t<Type>>();
145 }
146 }
147
148 template<typename... Args>
149 [[nodiscard]] static auto to_args(Registry &reg, type_list<Args...>) {
150 return std::tuple<decltype(extract<Args>(reg))...>(extract<Args>(reg)...);
151 }
152
153 template<typename... Type>
154 [[nodiscard]] static std::size_t fill_dependencies(type_list<Type...>, [[maybe_unused]] const type_info **buffer, [[maybe_unused]] const std::size_t count) {
155 if constexpr(sizeof...(Type) == 0u) {
156 return {};
157 } else {
158 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
159 const type_info *info[]{&type_id<Type>()...};
160 const auto length = count < sizeof...(Type) ? count : sizeof...(Type);
161
162 for(std::size_t pos{}; pos < length; ++pos) {
163 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
164 buffer[pos] = info[pos];
165 }
166
167 return length;
168 }
169 }
170
171 template<typename... RO, typename... RW>
172 void track_dependencies(std::size_t index, const bool requires_registry, type_list<RO...>, type_list<RW...>) {
173 builder.bind(static_cast<id_type>(index));
174 builder.set(type_hash<Registry>::value(), requires_registry || (sizeof...(RO) + sizeof...(RW) == 0u));
175 (builder.ro(type_hash<RO>::value()), ...);
176 (builder.rw(type_hash<RW>::value()), ...);
177 }
178
179public:
181 using registry_type = Registry;
183 using entity_type = typename registry_type::entity_type;
185 using size_type = std::size_t;
187 using function_type = callback_type;
188
190 struct vertex {
197 vertex(vertex_data data, std::vector<std::size_t> from, std::vector<std::size_t> to)
198 : node{std::move(data)},
199 in{std::move(from)},
200 out{std::move(to)} {}
201
209 [[nodiscard]] size_type ro_dependency(const type_info **buffer, const std::size_t length) const noexcept {
210 return node.dependency(false, buffer, length);
211 }
212
220 [[nodiscard]] size_type rw_dependency(const type_info **buffer, const std::size_t length) const noexcept {
221 return node.dependency(true, buffer, length);
222 }
223
228 [[nodiscard]] size_type ro_count() const noexcept {
229 return node.ro_count;
230 }
231
236 [[nodiscard]] size_type rw_count() const noexcept {
237 return node.rw_count;
238 }
239
244 [[nodiscard]] bool top_level() const noexcept {
245 return in.empty();
246 }
247
252 [[nodiscard]] const type_info &info() const noexcept {
253 return *node.info;
254 }
255
260 [[nodiscard]] const char *name() const noexcept {
261 return node.name;
262 }
263
268 [[nodiscard]] function_type *callback() const noexcept {
269 return node.callback;
270 }
271
276 [[nodiscard]] const void *data() const noexcept {
277 return node.payload;
278 }
279
284 [[nodiscard]] const std::vector<std::size_t> &in_edges() const noexcept {
285 return in;
286 }
287
292 [[nodiscard]] const std::vector<std::size_t> &out_edges() const noexcept {
293 return out;
294 }
295
301 void prepare(registry_type &reg) const {
302 node.prepare ? node.prepare(reg) : void();
303 }
304
305 private:
306 vertex_data node;
307 std::vector<std::size_t> in;
308 std::vector<std::size_t> out;
309 };
310
317 template<auto Candidate, typename... Req>
318 void emplace(const char *name = nullptr) {
319 using resource_type = decltype(internal::free_function_to_resource_traits<Req...>(Candidate));
320 constexpr auto requires_registry = type_list_contains_v<typename resource_type::args, registry_type>;
321
322 callback_type *callback = +[](const void *, registry_type &reg) {
323 std::apply(Candidate, to_args(reg, typename resource_type::args{}));
324 };
325
326 vertex_data vdata{
327 resource_type::ro::size,
328 resource_type::rw::size,
329 name,
330 nullptr,
331 callback,
332 +[](const bool rw, const type_info **buffer, const std::size_t length) { return rw ? fill_dependencies(typename resource_type::rw{}, buffer, length) : fill_dependencies(typename resource_type::ro{}, buffer, length); },
333 +[](registry_type &reg) { void(to_args(reg, typename resource_type::args{})); },
334 &type_id<std::integral_constant<decltype(Candidate), Candidate>>()};
335
336 track_dependencies(vertices.size(), requires_registry, typename resource_type::ro{}, typename resource_type::rw{});
337 vertices.push_back(std::move(vdata));
338 }
339
349 template<auto Candidate, typename... Req, typename Type>
350 void emplace(Type &value_or_instance, const char *name = nullptr) {
351 using resource_type = decltype(internal::constrained_function_to_resource_traits<Req...>(Candidate));
352 constexpr auto requires_registry = type_list_contains_v<typename resource_type::args, registry_type>;
353
354 callback_type *callback = +[](const void *payload, registry_type &reg) {
355 Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
356 std::apply(Candidate, std::tuple_cat(std::forward_as_tuple(*curr), to_args(reg, typename resource_type::args{})));
357 };
358
359 vertex_data vdata{
360 resource_type::ro::size,
361 resource_type::rw::size,
362 name,
363 &value_or_instance,
364 callback,
365 +[](const bool rw, const type_info **buffer, const std::size_t length) { return rw ? fill_dependencies(typename resource_type::rw{}, buffer, length) : fill_dependencies(typename resource_type::ro{}, buffer, length); },
366 +[](registry_type &reg) { void(to_args(reg, typename resource_type::args{})); },
367 &type_id<std::integral_constant<decltype(Candidate), Candidate>>()};
368
369 track_dependencies(vertices.size(), requires_registry, typename resource_type::ro{}, typename resource_type::rw{});
370 vertices.push_back(std::move(vdata));
371 }
372
381 template<typename... Req>
382 void emplace(function_type *func, const void *payload = nullptr, const char *name = nullptr) {
383 using resource_type = internal::resource_traits<type_list<>, type_list<Req...>>;
384 track_dependencies(vertices.size(), true, typename resource_type::ro{}, typename resource_type::rw{});
385
386 vertex_data vdata{
387 resource_type::ro::size,
388 resource_type::rw::size,
389 name,
390 payload,
391 func,
392 +[](const bool rw, const type_info **buffer, const std::size_t length) { return rw ? fill_dependencies(typename resource_type::rw{}, buffer, length) : fill_dependencies(typename resource_type::ro{}, buffer, length); },
393 nullptr,
394 &type_id<void>()};
395
396 vertices.push_back(std::move(vdata));
397 }
398
403 [[nodiscard]] std::vector<vertex> graph() {
404 std::vector<vertex> adjacency_list{};
405 adjacency_list.reserve(vertices.size());
406 auto adjacency_matrix = builder.graph();
407
408 for(auto curr: adjacency_matrix.vertices()) {
409 std::vector<std::size_t> in{};
410 std::vector<std::size_t> out{};
411
412 for(auto &&edge: adjacency_matrix.in_edges(curr)) {
413 in.push_back(edge.first);
414 }
415
416 for(auto &&edge: adjacency_matrix.out_edges(curr)) {
417 out.push_back(edge.second);
418 }
419
420 adjacency_list.emplace_back(vertices[curr], std::move(in), std::move(out));
421 }
422
423 return adjacency_list;
424 }
425
427 void clear() {
428 builder.clear();
429 vertices.clear();
430 }
431
432private:
433 std::vector<vertex_data> vertices;
434 flow builder;
435};
436
437} // namespace entt
438
439#endif
entt::adjacency_matrix
Basic implementation of a directed adjacency matrix.
Definition adjacency_matrix.hpp:96
entt::adjacency_matrix::out_edges
iterable_adaptor< out_edge_iterator > out_edges(const vertex_type vertex) const noexcept
Returns an iterable object to visit all out-edges of a vertex.
Definition adjacency_matrix.hpp:251
entt::adjacency_matrix::in_edges
iterable_adaptor< in_edge_iterator > in_edges(const vertex_type vertex) const noexcept
Returns an iterable object to visit all in-edges of a vertex.
Definition adjacency_matrix.hpp:263
entt::adjacency_matrix::vertices
iterable_adaptor< vertex_iterator > vertices() const noexcept
Returns an iterable object to visit all vertices of a matrix.
Definition adjacency_matrix.hpp:232
entt::as_group
Converts a registry to a group.
Definition helper.hpp:61
entt::as_view
Converts a registry to a view.
Definition helper.hpp:22
entt::basic_organizer
Utility class for creating a static task graph.
Definition organizer.hpp:119
entt::basic_organizer< registry >::registry_type
registry registry_type
Definition organizer.hpp:181
entt::basic_organizer::graph
std::vector< vertex > graph()
Generates a task graph for the current content.
Definition organizer.hpp:403
entt::basic_organizer::clear
void clear()
Erases all elements from a container.
Definition organizer.hpp:427
entt::basic_organizer::emplace
void emplace(const char *name=nullptr)
Adds a free function to the task list.
Definition organizer.hpp:318
entt::basic_organizer< registry >::function_type
callback_type function_type
Definition organizer.hpp:187
entt::basic_organizer::emplace
void emplace(function_type *func, const void *payload=nullptr, const char *name=nullptr)
Adds an user defined function with optional payload to the task list.
Definition organizer.hpp:382
entt::basic_organizer::emplace
void emplace(Type &value_or_instance, const char *name=nullptr)
Adds a free function with payload or a member function with an instance to the task list.
Definition organizer.hpp:350
entt::basic_organizer< registry >::entity_type
typename registry_type::entity_type entity_type
Definition organizer.hpp:183
entt::basic_organizer< registry >::size_type
std::size_t size_type
Definition organizer.hpp:185
entt::basic_registry<>::entity_type
typename traits_type::value_type entity_type
Definition registry.hpp:304
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::basic_view
basic_view(Type &...storage) -> basic_view< get_t< Type... >, exclude_t<> >
Deduction guide.
entt::constness_as_t
typename constness_as< To, From >::type constness_as_t
Alias template to facilitate the transcription of the constness.
Definition type_traits.hpp:841
entt::type_list_cat_t
typename type_list_cat< List... >::type type_list_cat_t
Helper type.
Definition type_traits.hpp:252
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
entt::flow
basic_flow<> flow
Alias declaration for the most common use case.
Definition fwd.hpp:23
entt::type_list_contains_v
constexpr bool type_list_contains_v
Helper variable template.
Definition type_traits.hpp:313
entt::type_id
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
Definition type_info.hpp:251
entt::basic_organizer::vertex::ro_dependency
size_type ro_dependency(const type_info **buffer, const std::size_t length) const noexcept
Fills a buffer with the type info objects for the writable resources of a vertex.
Definition organizer.hpp:209
entt::basic_organizer::vertex::ro_count
size_type ro_count() const noexcept
Returns the number of read-only resources of a vertex.
Definition organizer.hpp:228
entt::basic_organizer::vertex::info
const type_info & info() const noexcept
Returns a type info object associated with a vertex.
Definition organizer.hpp:252
entt::basic_organizer::vertex::callback
function_type * callback() const noexcept
Returns the function associated with a vertex.
Definition organizer.hpp:268
entt::basic_organizer::vertex::in_edges
const std::vector< std::size_t > & in_edges() const noexcept
Returns the list of in-edges of a vertex.
Definition organizer.hpp:284
entt::basic_organizer::vertex::out_edges
const std::vector< std::size_t > & out_edges() const noexcept
Returns the list of out-edges of a vertex.
Definition organizer.hpp:292
entt::basic_organizer::vertex::prepare
void prepare(registry_type &reg) const
Prepares a registry and assures that all required resources are properly instantiated before using th...
Definition organizer.hpp:301
entt::basic_organizer::vertex::rw_dependency
size_type rw_dependency(const type_info **buffer, const std::size_t length) const noexcept
Fills a buffer with the type info objects for the read-only resources of a vertex.
Definition organizer.hpp:220
entt::basic_organizer::vertex::name
const char * name() const noexcept
Returns a user defined name associated with a vertex, if any.
Definition organizer.hpp:260
entt::basic_organizer::vertex::data
const void * data() const noexcept
Returns the payload associated with a vertex, if any.
Definition organizer.hpp:276
entt::basic_organizer::vertex::vertex
vertex(vertex_data data, std::vector< std::size_t > from, std::vector< std::size_t > to)
Constructs a vertex of the task graph.
Definition organizer.hpp:197
entt::basic_organizer::vertex::rw_count
size_type rw_count() const noexcept
Returns the number of writable resources of a vertex.
Definition organizer.hpp:236
entt::basic_organizer::vertex::top_level
bool top_level() const noexcept
Checks if a vertex is also a top-level one.
Definition organizer.hpp:244
entt::type_hash::value
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.
Definition type_info.hpp:101
entt::type_info
Implementation specific information about a type.
Definition type_info.hpp:133
entt::type_list
A class to use to push around lists of types, nothing more.
Definition type_traits.hpp:111