EnTT 3.13.1
Loading...
Searching...
No Matches
scheduler.hpp
1#ifndef ENTT_PROCESS_SCHEDULER_HPP
2#define ENTT_PROCESS_SCHEDULER_HPP
3
4#include <cstddef>
5#include <memory>
6#include <type_traits>
7#include <utility>
8#include <vector>
9#include "../config/config.h"
10#include "../core/compressed_pair.hpp"
11#include "fwd.hpp"
12#include "process.hpp"
13
14namespace entt {
15
17namespace internal {
18
19template<typename Delta>
20struct basic_process_handler {
21 virtual ~basic_process_handler() = default;
22
23 virtual bool update(const Delta, void *) = 0;
24 virtual void abort(const bool) = 0;
25
26 // std::shared_ptr because of its type erased allocator which is useful here
27 std::shared_ptr<basic_process_handler> next;
28};
29
30template<typename Delta, typename Type>
31struct process_handler final: basic_process_handler<Delta> {
32 template<typename... Args>
33 process_handler(Args &&...args)
34 : process{std::forward<Args>(args)...} {}
35
36 bool update(const Delta delta, void *data) override {
37 if(process.tick(delta, data); process.rejected()) {
38 this->next.reset();
39 }
40
41 return (process.rejected() || process.finished());
42 }
43
44 void abort(const bool immediate) override {
45 process.abort(immediate);
46 }
47
48 Type process;
49};
50
51} // namespace internal
81template<typename Delta, typename Allocator>
83 template<typename Type>
84 using handler_type = internal::process_handler<Delta, Type>;
85
86 // std::shared_ptr because of its type erased allocator which is useful here
87 using process_type = std::shared_ptr<internal::basic_process_handler<Delta>>;
88
89 using alloc_traits = std::allocator_traits<Allocator>;
90 using container_allocator = typename alloc_traits::template rebind_alloc<process_type>;
91 using container_type = std::vector<process_type, container_allocator>;
92
93public:
95 using allocator_type = Allocator;
97 using size_type = std::size_t;
100
104
110 : handlers{allocator, allocator} {}
111
117 : handlers{std::move(other.handlers)} {}
118
125 : handlers{container_type{std::move(other.handlers.first()), allocator}, allocator} {
126 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(), "Copying a scheduler is not allowed");
127 }
128
135 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(), "Copying a scheduler is not allowed");
136 handlers = std::move(other.handlers);
137 return *this;
138 }
139
145 using std::swap;
146 swap(handlers, other.handlers);
147 }
148
154 return handlers.second();
155 }
156
162 return handlers.first().size();
163 }
164
170 return handlers.first().empty();
171 }
172
179 void clear() {
180 handlers.first().clear();
181 }
182
208 template<typename Proc, typename... Args>
210 static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>, "Invalid process type");
211 auto &ref = handlers.first().emplace_back(std::allocate_shared<handler_type<Proc>>(handlers.second(), std::forward<Args>(args)...));
212 // forces the process to exit the uninitialized state
213 ref->update({}, nullptr);
214 return *this;
215 }
216
268 template<typename Func>
269 basic_scheduler &attach(Func &&func) {
271 return attach<Proc>(std::forward<Func>(func));
272 }
273
281 template<typename Proc, typename... Args>
283 static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>, "Invalid process type");
284 ENTT_ASSERT(!handlers.first().empty(), "Process not available");
285 auto *curr = handlers.first().back().get();
286 for(; curr->next; curr = curr->next.get()) {}
287 curr->next = std::allocate_shared<handler_type<Proc>>(handlers.second(), std::forward<Args>(args)...);
288 return *this;
289 }
290
297 template<typename Func>
298 basic_scheduler &then(Func &&func) {
300 return then<Proc>(std::forward<Func>(func));
301 }
302
314 void update(const delta_type delta, void *data = nullptr) {
315 for(auto next = handlers.first().size(); next; --next) {
316 if(const auto pos = next - 1u; handlers.first()[pos]->update(delta, data)) {
317 // updating might spawn/reallocate, cannot hold refs until here
318 if(auto &curr = handlers.first()[pos]; curr->next) {
319 curr = std::move(curr->next);
320 // forces the process to exit the uninitialized state
321 curr->update({}, nullptr);
322 } else {
323 curr = std::move(handlers.first().back());
324 handlers.first().pop_back();
325 }
326 }
327 }
328 }
329
340 void abort(const bool immediate = false) {
341 for(auto &&curr: handlers.first()) {
342 curr->abort(immediate);
343 }
344 }
345
346private:
348};
349
350} // namespace entt
351
352#endif
Cooperative scheduler for processes.
Definition scheduler.hpp:82
bool empty() const noexcept
Returns true if at least a process is currently scheduled.
Allocator allocator_type
Allocator type.
Definition scheduler.hpp:95
basic_scheduler()
Default constructor.
size_type size() const noexcept
Number of processes currently scheduled.
basic_scheduler(basic_scheduler &&other) noexcept
Move constructor.
basic_scheduler & then(Func &&func)
Sets a process as a continuation of the last scheduled process.
std::size_t size_type
Unsigned integer type.
Definition scheduler.hpp:97
basic_scheduler(const allocator_type &allocator)
Constructs a scheduler with a given allocator.
basic_scheduler & operator=(basic_scheduler &&other) noexcept
Move assignment operator.
basic_scheduler & then(Args &&...args)
Sets a process as a continuation of the last scheduled process.
void update(const delta_type delta, void *data=nullptr)
Updates all scheduled processes.
void abort(const bool immediate=false)
Aborts all scheduled processes.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
basic_scheduler & attach(Func &&func)
Schedules a process for the next tick.
basic_scheduler(basic_scheduler &&other, const allocator_type &allocator) noexcept
Allocator-extended move constructor.
void clear()
Discards all scheduled processes.
Delta delta_type
Unsigned integer type.
Definition scheduler.hpp:99
void swap(basic_scheduler &other)
Exchanges the contents with those of a given scheduler.
basic_scheduler & attach(Args &&...args)
Schedules a process for the next tick.
A compressed pair.
constexpr second_type & second() noexcept
Returns the second element that a pair stores.
constexpr first_type & first() noexcept
Returns the first element that a pair stores.
EnTT default namespace.
Definition dense_map.hpp:21
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:258
@ ref
Aliasing mode, the object points to a non-const element.
Adaptor for lambdas and functors to turn them into processes.
Definition process.hpp:306