1#ifndef ENTT_ENTITY_ORGANIZER_HPP
2#define ENTT_ENTITY_ORGANIZER_HPP
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"
22struct is_view: std::false_type {};
24template<
typename... Args>
25struct is_view<
basic_view<Args...>>: std::true_type {};
27template<
typename Type>
28inline constexpr bool is_view_v = is_view<Type>::value;
30template<
typename Type,
typename Overr
ide>
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>>,
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>),
43template<
typename... Args,
typename... Override>
44struct unpack_type<basic_registry<Args...>, type_list<Override...>> {
45 using ro = type_list<>;
46 using rw = type_list<>;
49template<
typename... Args,
typename... Override>
50struct unpack_type<const basic_registry<Args...>, type_list<Override...>>
51 : unpack_type<basic_registry<Args...>, type_list<Override...>> {};
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...>;
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...>> {};
63template<
typename,
typename>
64struct resource_traits;
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...>;
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...));
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...));
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...));
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);
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);
105 struct vertex_data final {
106 std::size_t ro_count{};
107 std::size_t rw_count{};
109 const void *payload{};
110 callback_type *callback{};
111 dependency_type *dependency{};
112 prepare_type *prepare{};
116 template<
typename Type>
117 [[nodiscard]]
static decltype(
auto) extract(Registry ®) {
118 if constexpr(std::is_same_v<Type, Registry>) {
120 }
else if constexpr(internal::is_view_v<Type>) {
121 return static_cast<Type
>(
as_view{reg});
127 template<
typename... Args>
129 return std::tuple<decltype(extract<Args>(reg))...>(extract<Args>(reg)...);
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) {
139 const auto length = count <
sizeof...(Type) ? count :
sizeof...(Type);
141 for(std::size_t pos{}; pos < length; ++pos) {
143 buffer[pos] = info[pos];
150 template<
typename... RO,
typename... RW>
176 vertex(vertex_data
data, std::vector<std::size_t> from, std::vector<std::size_t> to)
177 : node{std::move(
data)},
179 out{std::move(to)} {}
189 return node.dependency(
false, buffer, length);
200 return node.dependency(
true, buffer, length);
208 return node.ro_count;
216 return node.rw_count;
239 [[nodiscard]]
const char *
name() const noexcept {
248 return node.callback;
255 [[nodiscard]]
const void *
data() const noexcept {
263 [[nodiscard]]
const std::vector<std::size_t> &
in_edges() const noexcept {
271 [[nodiscard]]
const std::vector<std::size_t> &
out_edges() const noexcept {
279 [[deprecated(
"use ::out_edges")]] [[nodiscard]]
const std::vector<std::size_t> &
children() const noexcept {
289 node.prepare ? node.prepare(reg) : void();
294 std::vector<std::size_t> in;
295 std::vector<std::size_t> out;
304 template<
auto Candidate,
typename... Req>
306 using resource_type =
decltype(internal::free_function_to_resource_traits<Req...>(Candidate));
309 callback_type *callback = +[](
const void *,
registry_type ®) {
310 std::apply(Candidate, to_args(reg,
typename resource_type::args{}));
314 resource_type::ro::size,
315 resource_type::rw::size,
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 ®) { void(to_args(reg,
typename resource_type::args{})); },
321 &
type_id<std::integral_constant<
decltype(Candidate), Candidate>>()};
323 track_dependencies(vertices.size(), requires_registry,
typename resource_type::ro{},
typename resource_type::rw{});
324 vertices.push_back(std::move(vdata));
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));
341 callback_type *callback = +[](
const void *payload,
registry_type ®) {
343 std::apply(Candidate, std::tuple_cat(std::forward_as_tuple(*curr), to_args(reg,
typename resource_type::args{})));
347 resource_type::ro::size,
348 resource_type::rw::size,
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 ®) { void(to_args(reg,
typename resource_type::args{})); },
354 &
type_id<std::integral_constant<
decltype(Candidate), Candidate>>()};
356 track_dependencies(vertices.size(), requires_registry,
typename resource_type::ro{},
typename resource_type::rw{});
357 vertices.push_back(std::move(vdata));
368 template<
typename... Req>
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{});
374 resource_type::ro::size,
375 resource_type::rw::size,
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); },
383 vertices.push_back(std::move(vdata));
390 [[nodiscard]] std::vector<vertex>
graph() {
391 std::vector<vertex> adjacency_list{};
392 adjacency_list.reserve(vertices.size());
396 std::vector<std::size_t> in{};
397 std::vector<std::size_t> out{};
400 in.push_back(edge.first);
404 out.push_back(edge.second);
407 adjacency_list.emplace_back(vertices[curr], std::move(in), std::move(out));
410 return adjacency_list;
420 std::vector<vertex_data> vertices;
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< vertex_iterator > vertices() const noexcept
Returns an iterable object to visit all vertices of a matrix.
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.
Converts a registry to a view.
Utility class for creating task graphs.
void clear() noexcept
Clears the flow builder.
basic_flow & ro(const id_type res)
Assigns a read-only resource to the current task.
basic_flow & set(const id_type res, bool is_rw=false)
Assigns a resource to the current task with a given access mode.
basic_flow & bind(const id_type value)
Binds a task to a flow builder.
graph_type graph() const
Generates a task graph for the current content.
basic_flow & rw(const id_type res)
Assigns a writable resource to the current task.
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.
basic_view(Type &...storage) -> basic_view< get_t< Type... >, exclude_t<> >
Deduction guide.
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.
constexpr bool type_list_contains_v
Helper variable template.
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 ®) 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.
Implementation specific information about a type.
A class to use to push around lists of types, nothing more.