1#ifndef ENTT_PROCESS_SCHEDULER_HPP
2#define ENTT_PROCESS_SCHEDULER_HPP
9#include "../config/config.h"
10#include "../core/compressed_pair.hpp"
35template<
typename Delta,
typename Allocator>
38 using alloc_traits = std::allocator_traits<Allocator>;
39 using container_allocator =
typename alloc_traits::template rebind_alloc<std::shared_ptr<base_type>>;
40 using container_type = std::vector<std::shared_ptr<base_type>, container_allocator>;
61 : handlers{allocator, allocator} {}
71 : handlers{std::move(other.handlers)} {}
79 : handlers{container_type{std::move(other.handlers.first()), allocator}, allocator} {
80 ENTT_ASSERT(alloc_traits::is_always_equal::value ||
get_allocator() == other.get_allocator(),
"Copying a scheduler is not allowed");
98 ENTT_ASSERT(alloc_traits::is_always_equal::value ||
get_allocator() == other.get_allocator(),
"Copying a scheduler is not allowed");
109 swap(handlers, other.handlers);
117 return handlers.second();
125 return handlers.first().size();
132 [[nodiscard]]
bool empty() const noexcept {
133 return handlers.first().empty();
143 handlers.first().clear();
153 template<
typename Type,
typename... Args>
155 const auto &allocator = handlers.second();
156 return *handlers.first().emplace_back(std::allocate_shared<Type>(allocator, allocator, std::forward<Args>(args)...));
165 template<
typename Func>
167 const auto &allocator = handlers.second();
168 using process_type = internal::process_adaptor<delta_type, Func, allocator_type>;
169 return *handlers.first().emplace_back(std::allocate_shared<process_type>(allocator, allocator, std::move(func)));
184 for(
auto next = handlers.first().size(); next; --next) {
185 const auto pos = next - 1u;
186 handlers.first()[pos]->tick(delta, data);
188 auto &elem = handlers.first()[pos];
190 if(elem->finished()) {
194 if(!elem || elem->rejected()) {
195 elem = std::move(handlers.first().back());
196 handlers.first().pop_back();
211 void abort(
const bool immediate =
false) {
212 for(
auto &&curr: handlers.first()) {
Base class for processes.
bool empty() const noexcept
Returns true if at least a process is currently scheduled.
basic_scheduler(const basic_scheduler &)=delete
Default copy constructor, deleted on purpose.
basic_scheduler()
Default constructor.
size_type size() const noexcept
Number of processes currently scheduled.
basic_scheduler & operator=(const basic_scheduler &)=delete
Default copy assignment operator, deleted on purpose.
type & attach(Args &&...args)
Schedules a process for the next tick.
basic_scheduler(basic_scheduler &&other) noexcept
Move constructor.
basic_scheduler(const allocator_type &allocator)
Constructs a scheduler with a given allocator.
~basic_scheduler()=default
Default destructor.
basic_scheduler & operator=(basic_scheduler &&other) noexcept
Move assignment operator.
void update(const delta_type delta, void *data=nullptr)
Updates all scheduled processes.
basic_scheduler(basic_scheduler &&other, const allocator_type &allocator)
Allocator-extended move constructor.
void abort(const bool immediate=false)
Aborts all scheduled processes.
type & attach(Func func)
Schedules a process for the next tick.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
void swap(basic_scheduler &other) noexcept
Exchanges the contents with those of a given scheduler.
void clear()
Discards all scheduled processes.