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::value_type...>,
typename unpack_type<constness_as_t<typename Get::value_type, Get>, type_list<Override...>>::ro...>;
56 using rw = type_list_cat_t<typename unpack_type<constness_as_t<typename Get::value_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});
123 return reg.ctx().template emplace<std::remove_reference_t<Type>>();
127 template<
typename... Args>
129 return std::tuple<decltype(extract<Args>(reg))...>(extract<Args>(reg)...);
132 template<
typename... Type>
133 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) {
137 const type_info *info[
sizeof...(Type)]{&type_id<Type>()...};
138 const auto length = count <
sizeof...(Type) ? count :
sizeof...(Type);
140 for(std::size_t pos{}; pos < length; ++pos) {
141 buffer[pos] = info[pos];
148 template<
typename... RO,
typename... RW>
174 vertex(
const bool vtype, vertex_data
data, std::vector<std::size_t> edges)
175 : is_top_level{vtype},
176 node{std::move(
data)},
177 reachable{std::move(edges)} {}
187 return node.dependency(
false, buffer, length);
198 return node.dependency(
true, buffer, length);
206 return node.ro_count;
214 return node.rw_count;
237 const char *
name() const noexcept {
246 return node.callback;
253 const void *
data() const noexcept {
261 const std::vector<std::size_t> &
children() const noexcept {
271 node.prepare ? node.prepare(reg) : void();
277 std::vector<std::size_t> reachable;
286 template<
auto Candidate,
typename... Req>
288 using resource_type =
decltype(internal::free_function_to_resource_traits<Req...>(Candidate));
289 constexpr auto requires_registry = type_list_contains_v<typename resource_type::args, registry_type>;
291 callback_type *callback = +[](
const void *,
registry_type ®) {
292 std::apply(Candidate, to_args(reg,
typename resource_type::args{}));
296 resource_type::ro::size,
297 resource_type::rw::size,
301 +[](
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); },
302 +[](
registry_type ®) { void(to_args(reg,
typename resource_type::args{})); },
303 &
type_id<std::integral_constant<
decltype(Candidate), Candidate>>()};
305 track_dependencies(vertices.size(), requires_registry,
typename resource_type::ro{},
typename resource_type::rw{});
306 vertices.push_back(std::move(vdata));
318 template<
auto Candidate,
typename... Req,
typename Type>
319 void emplace(Type &value_or_instance,
const char *name =
nullptr) {
320 using resource_type =
decltype(internal::constrained_function_to_resource_traits<Req...>(Candidate));
321 constexpr auto requires_registry = type_list_contains_v<typename resource_type::args, registry_type>;
323 callback_type *callback = +[](
const void *payload,
registry_type ®) {
325 std::apply(Candidate, std::tuple_cat(std::forward_as_tuple(*curr), to_args(reg,
typename resource_type::args{})));
329 resource_type::ro::size,
330 resource_type::rw::size,
334 +[](
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); },
335 +[](
registry_type ®) { void(to_args(reg,
typename resource_type::args{})); },
336 &
type_id<std::integral_constant<
decltype(Candidate), Candidate>>()};
338 track_dependencies(vertices.size(), requires_registry,
typename resource_type::ro{},
typename resource_type::rw{});
339 vertices.push_back(std::move(vdata));
350 template<
typename... Req>
352 using resource_type = internal::resource_traits<type_list<>,
type_list<Req...>>;
353 track_dependencies(vertices.size(),
true,
typename resource_type::ro{},
typename resource_type::rw{});
356 resource_type::ro::size,
357 resource_type::rw::size,
361 +[](
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); },
365 vertices.push_back(std::move(vdata));
373 std::vector<vertex> adjacency_list{};
374 adjacency_list.reserve(vertices.size());
379 std::vector<std::size_t> reachable{};
382 reachable.push_back(edge.second);
385 adjacency_list.emplace_back(iterable.cbegin() == iterable.cend(), vertices[curr], std::move(reachable));
388 return adjacency_list;
398 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< 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.
void clear() noexcept
Clears the flow builder.
basic_flow & rw(const id_type res)
Assigns a writable resource to the current task.
graph_type graph() const
Generates a task graph for the current content.
basic_flow & ro(const id_type res)
Assigns a read-only resource to the current task.
basic_flow & bind(const id_type value)
Binds a task to a flow builder.
basic_flow & set(const id_type res, bool is_rw=false)
Assigns a resource to the current task with a given access mode.
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.
std::uint32_t id_type
Alias declaration for type identifiers.
typename type_list_cat< List... >::type type_list_cat_t
Helper type.
typename constness_as< To, From >::type constness_as_t
Alias template to facilitate the transcription of the constness.
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 > & 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.
vertex(const bool vtype, vertex_data data, std::vector< std::size_t > edges)
Constructs a vertex of the task graph.
const void * data() const noexcept
Returns the payload associated with a vertex, if any.
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.