EnTT 3.14.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 Type, typename Override>
31struct unpack_type {
32 using ro = std::conditional_t<
33 type_list_contains_v<Override, const Type> || (std::is_const_v<Type> && !type_list_contains_v<Override, std::remove_const_t<Type>>),
34 type_list<std::remove_const_t<Type>>,
35 type_list<>>;
36
37 using rw = std::conditional_t<
38 type_list_contains_v<Override, std::remove_const_t<Type>> || (!std::is_const_v<Type> && !type_list_contains_v<Override, const Type>),
39 type_list<Type>,
40 type_list<>>;
41};
42
43template<typename... Args, typename... Override>
44struct unpack_type<basic_registry<Args...>, type_list<Override...>> {
45 using ro = type_list<>;
46 using rw = type_list<>;
47};
48
49template<typename... Args, typename... Override>
50struct unpack_type<const basic_registry<Args...>, type_list<Override...>>
51 : unpack_type<basic_registry<Args...>, type_list<Override...>> {};
52
53template<typename... Get, typename... Exclude, typename... Override>
54struct unpack_type<basic_view<get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>> {
55 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...>;
56 using rw = type_list_cat_t<typename unpack_type<constness_as_t<typename Get::element_type, Get>, type_list<Override...>>::rw...>;
57};
58
59template<typename... Get, typename... Exclude, typename... Override>
60struct unpack_type<const basic_view<get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>>
61 : unpack_type<basic_view<get_t<Get...>, exclude_t<Exclude...>>, type_list<Override...>> {};
62
63template<typename, typename>
64struct resource_traits;
65
66template<typename... Args, typename... Req>
67struct resource_traits<type_list<Args...>, type_list<Req...>> {
68 using args = type_list<std::remove_const_t<Args>...>;
69 using ro = type_list_cat_t<typename unpack_type<Args, type_list<Req...>>::ro..., typename unpack_type<Req, type_list<>>::ro...>;
70 using rw = type_list_cat_t<typename unpack_type<Args, type_list<Req...>>::rw..., typename unpack_type<Req, type_list<>>::rw...>;
71};
72
73template<typename... Req, typename Ret, typename... Args>
74resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> free_function_to_resource_traits(Ret (*)(Args...));
75
76template<typename... Req, typename Ret, typename Type, typename... Args>
77resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource_traits(Ret (*)(Type &, Args...));
78
79template<typename... Req, typename Ret, typename Class, typename... Args>
80resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource_traits(Ret (Class::*)(Args...));
81
82template<typename... Req, typename Ret, typename Class, typename... Args>
83resource_traits<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource_traits(Ret (Class::*)(Args...) const);
84
85} // namespace internal
99template<typename Registry>
101 using callback_type = void(const void *, Registry &);
102 using prepare_type = void(Registry &);
103 using dependency_type = std::size_t(const bool, const type_info **, const std::size_t);
104
105 struct vertex_data final {
106 std::size_t ro_count{};
107 std::size_t rw_count{};
108 const char *name{};
109 const void *payload{};
110 callback_type *callback{};
111 dependency_type *dependency{};
112 prepare_type *prepare{};
113 const type_info *info{};
114 };
115
116 template<typename Type>
117 [[nodiscard]] static decltype(auto) extract(Registry &reg) {
118 if constexpr(std::is_same_v<Type, Registry>) {
119 return reg;
120 } else if constexpr(internal::is_view_v<Type>) {
121 return static_cast<Type>(as_view{reg});
122 } else {
123 return reg.ctx().template emplace<std::remove_reference_t<Type>>();
124 }
125 }
126
127 template<typename... Args>
128 [[nodiscard]] static auto to_args(Registry &reg, type_list<Args...>) {
129 return std::tuple<decltype(extract<Args>(reg))...>(extract<Args>(reg)...);
130 }
131
132 template<typename... Type>
133 [[nodiscard]] static std::size_t fill_dependencies(type_list<Type...>, [[maybe_unused]] const type_info **buffer, [[maybe_unused]] const std::size_t count) {
134 if constexpr(sizeof...(Type) == 0u) {
135 return {};
136 } else {
137 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
138 const type_info *info[]{&type_id<Type>()...};
139 const auto length = count < sizeof...(Type) ? count : sizeof...(Type);
140
141 for(std::size_t pos{}; pos < length; ++pos) {
142 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
143 buffer[pos] = info[pos];
144 }
145
146 return length;
147 }
148 }
149
150 template<typename... RO, typename... RW>
151 void track_dependencies(std::size_t index, const bool requires_registry, type_list<RO...>, type_list<RW...>) {
152 builder.bind(static_cast<id_type>(index));
153 builder.set(type_hash<Registry>::value(), requires_registry || (sizeof...(RO) + sizeof...(RW) == 0u));
154 (builder.ro(type_hash<RO>::value()), ...);
155 (builder.rw(type_hash<RW>::value()), ...);
156 }
157
158public:
162 using entity_type = typename registry_type::entity_type;
164 using size_type = std::size_t;
166 using function_type = callback_type;
167
169 struct vertex {
176 vertex(vertex_data data, std::vector<std::size_t> from, std::vector<std::size_t> to)
177 : node{std::move(data)},
178 in{std::move(from)},
179 out{std::move(to)} {}
180
188 [[nodiscard]] size_type ro_dependency(const type_info **buffer, const std::size_t length) const noexcept {
189 return node.dependency(false, buffer, length);
190 }
191
199 [[nodiscard]] size_type rw_dependency(const type_info **buffer, const std::size_t length) const noexcept {
200 return node.dependency(true, buffer, length);
201 }
202
208 return node.ro_count;
209 }
210
216 return node.rw_count;
217 }
218
224 return in.empty();
225 }
226
232 return *node.info;
233 }
234
239 [[nodiscard]] const char *name() const noexcept {
240 return node.name;
241 }
242
248 return node.callback;
249 }
250
255 [[nodiscard]] const void *data() const noexcept {
256 return node.payload;
257 }
258
263 [[nodiscard]] const std::vector<std::size_t> &in_edges() const noexcept {
264 return in;
265 }
266
271 [[nodiscard]] const std::vector<std::size_t> &out_edges() const noexcept {
272 return out;
273 }
274
279 [[deprecated("use ::out_edges")]] [[nodiscard]] const std::vector<std::size_t> &children() const noexcept {
280 return out_edges();
281 }
282
288 void prepare(registry_type &reg) const {
289 node.prepare ? node.prepare(reg) : void();
290 }
291
292 private:
293 vertex_data node;
294 std::vector<std::size_t> in;
295 std::vector<std::size_t> out;
296 };
297
304 template<auto Candidate, typename... Req>
305 void emplace(const char *name = nullptr) {
306 using resource_type = decltype(internal::free_function_to_resource_traits<Req...>(Candidate));
308
309 callback_type *callback = +[](const void *, registry_type &reg) {
310 std::apply(Candidate, to_args(reg, typename resource_type::args{}));
311 };
312
313 vertex_data vdata{
314 resource_type::ro::size,
315 resource_type::rw::size,
316 name,
317 nullptr,
318 callback,
319 +[](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); },
320 +[](registry_type &reg) { void(to_args(reg, typename resource_type::args{})); },
321 &type_id<std::integral_constant<decltype(Candidate), Candidate>>()};
322
323 track_dependencies(vertices.size(), requires_registry, typename resource_type::ro{}, typename resource_type::rw{});
324 vertices.push_back(std::move(vdata));
325 }
326
336 template<auto Candidate, typename... Req, typename Type>
337 void emplace(Type &value_or_instance, const char *name = nullptr) {
338 using resource_type = decltype(internal::constrained_function_to_resource_traits<Req...>(Candidate));
340
341 callback_type *callback = +[](const void *payload, registry_type &reg) {
342 Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
343 std::apply(Candidate, std::tuple_cat(std::forward_as_tuple(*curr), to_args(reg, typename resource_type::args{})));
344 };
345
346 vertex_data vdata{
347 resource_type::ro::size,
348 resource_type::rw::size,
349 name,
351 callback,
352 +[](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); },
353 +[](registry_type &reg) { void(to_args(reg, typename resource_type::args{})); },
354 &type_id<std::integral_constant<decltype(Candidate), Candidate>>()};
355
356 track_dependencies(vertices.size(), requires_registry, typename resource_type::ro{}, typename resource_type::rw{});
357 vertices.push_back(std::move(vdata));
358 }
359
368 template<typename... Req>
369 void emplace(function_type *func, const void *payload = nullptr, const char *name = nullptr) {
370 using resource_type = internal::resource_traits<type_list<>, type_list<Req...>>;
371 track_dependencies(vertices.size(), true, typename resource_type::ro{}, typename resource_type::rw{});
372
373 vertex_data vdata{
374 resource_type::ro::size,
375 resource_type::rw::size,
376 name,
377 payload,
378 func,
379 +[](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); },
380 nullptr,
381 &type_id<void>()};
382
383 vertices.push_back(std::move(vdata));
384 }
385
390 [[nodiscard]] std::vector<vertex> graph() {
391 std::vector<vertex> adjacency_list{};
392 adjacency_list.reserve(vertices.size());
393 auto adjacency_matrix = builder.graph();
394
395 for(auto curr: adjacency_matrix.vertices()) {
396 std::vector<std::size_t> in{};
397 std::vector<std::size_t> out{};
398
399 for(auto &&edge: adjacency_matrix.in_edges(curr)) {
400 in.push_back(edge.first);
401 }
402
403 for(auto &&edge: adjacency_matrix.out_edges(curr)) {
404 out.push_back(edge.second);
405 }
406
407 adjacency_list.emplace_back(vertices[curr], std::move(in), std::move(out));
408 }
409
410 return adjacency_list;
411 }
412
414 void clear() {
415 builder.clear();
416 vertices.clear();
417 }
418
419private:
420 std::vector<vertex_data> vertices;
421 flow builder;
422};
423
424} // namespace entt
425
426#endif
Basic implementation of a directed adjacency matrix.
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.
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.
iterable_adaptor< vertex_iterator > vertices() const noexcept
Returns an iterable object to visit all vertices of a matrix.
Converts a registry to a view.
Definition helper.hpp:22
void clear() noexcept
Clears the flow builder.
Definition flow.hpp:201
basic_flow & rw(const id_type res)
Assigns a writable resource to the current task.
Definition flow.hpp:306
graph_type graph() const
Generates a task graph for the current content.
Definition flow.hpp:332
basic_flow & ro(const id_type res)
Assigns a read-only resource to the current task.
Definition flow.hpp:279
basic_flow & bind(const id_type value)
Binds a task to a flow builder.
Definition flow.hpp:241
basic_flow & set(const id_type res, bool is_rw=false)
Assigns a resource to the current task with a given access mode.
Definition flow.hpp:269
Utility class for creating a static task graph.
std::vector< vertex > graph()
Generates a task graph for the current content.
void clear()
Erases all elements from a container.
void emplace(const char *name=nullptr)
Adds a free function to the task list.
callback_type function_type
Raw task function type.
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.
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.
typename registry_type::entity_type entity_type
Underlying entity identifier.
std::size_t size_type
Unsigned integer type.
EnTT default namespace.
Definition dense_map.hpp:22
basic_view(Type &...storage) -> basic_view< get_t< Type... >, exclude_t<> >
Deduction guide.
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
typename constness_as< To, From >::type constness_as_t
Alias template to facilitate the transcription of the constness.
typename type_list_cat< List... >::type type_list_cat_t
Helper type.
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:14
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
Vertex type of a task graph defined as an adjacency list.
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.
size_type ro_count() const noexcept
Returns the number of read-only resources of a vertex.
const type_info & info() const noexcept
Returns a type info object associated with a vertex.
function_type * callback() const noexcept
Returns the function associated with a vertex.
const std::vector< std::size_t > & in_edges() const noexcept
Returns the list of in-edges of a vertex.
const std::vector< std::size_t > & out_edges() const noexcept
Returns the list of out-edges of a vertex.
const std::vector< std::size_t > & children() const noexcept
Returns the list of nodes reachable from a given vertex.
void prepare(registry_type &reg) const
Prepares a registry and assures that all required resources are properly instantiated before using th...
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.
const char * name() const noexcept
Returns a user defined name associated with a vertex, if any.
const void * data() const noexcept
Returns the payload associated with a vertex, if any.
vertex(vertex_data data, std::vector< std::size_t > from, std::vector< std::size_t > to)
Constructs a vertex of the task graph.
size_type rw_count() const noexcept
Returns the number of writable resources of a vertex.
bool top_level() const noexcept
Checks if a vertex is also a top-level one.
Type hash.
Definition type_info.hpp:92
Implementation specific information about a type.
A class to use to push around lists of types, nothing more.