EnTT: src/entt/process/scheduler.hpp Source File

EnTT 3.16.0

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
35template<typename Delta, typename Allocator>
36 class basic_scheduler {
37 using base_type = basic_process<Delta, 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>;
41
42public:
44 using type = base_type;
46 using allocator_type = Allocator;
48 using size_type = std::size_t;
50 using delta_type = Delta;
51
53 basic_scheduler()
54 : basic_scheduler{allocator_type{}} {}
55
60 explicit basic_scheduler(const allocator_type &allocator)
61 : handlers{allocator, allocator} {}
62
64 basic_scheduler(const basic_scheduler &) = delete;
65
70 basic_scheduler(basic_scheduler &&other) noexcept
71 : handlers{std::move(other.handlers)} {}
72
78 basic_scheduler(basic_scheduler &&other, const allocator_type &allocator)
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");
81 }
82
84 ~basic_scheduler() = default;
85
90 basic_scheduler &operator=(const basic_scheduler &) = delete;
91
97 basic_scheduler &operator=(basic_scheduler &&other) noexcept {
98 ENTT_ASSERT(alloc_traits::is_always_equal::value || get_allocator() == other.get_allocator(), "Copying a scheduler is not allowed");
99 swap(other);
100 return *this;
101 }
102
107 void swap(basic_scheduler &other) noexcept {
108 using std::swap;
109 swap(handlers, other.handlers);
110 }
111
116 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
117 return handlers.second();
118 }
119
124 [[nodiscard]] size_type size() const noexcept {
125 return handlers.first().size();
126 }
127
132 [[nodiscard]] bool empty() const noexcept {
133 return handlers.first().empty();
134 }
135
142 void clear() {
143 handlers.first().clear();
144 }
145
153 template<typename Type, typename... Args>
154 type &attach(Args &&...args) {
155 const auto &allocator = handlers.second();
156 return *handlers.first().emplace_back(std::allocate_shared<Type>(allocator, allocator, std::forward<Args>(args)...));
157 }
158
165 template<typename Func>
166 type &attach(Func 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)));
170 }
171
183 void update(const delta_type delta, void *data = nullptr) {
184 for(auto next = handlers.first().size(); next; --next) {
185 const auto pos = next - 1u;
186 handlers.first()[pos]->tick(delta, data);
187 // updating might spawn/reallocate, cannot hold refs until here
188 auto &elem = handlers.first()[pos];
189
190 if(elem->finished()) {
191 elem = elem->peek();
192 }
193
194 if(!elem || elem->rejected()) {
195 elem = std::move(handlers.first().back());
196 handlers.first().pop_back();
197 }
198 }
199 }
200
211 void abort(const bool immediate = false) {
212 for(auto &&curr: handlers.first()) {
213 curr->abort();
214
215 if(immediate) {
216 curr->tick({});
217 }
218 }
219 }
220
221private:
222 compressed_pair<container_type, allocator_type> handlers;
223};
224
225} // namespace entt
226
227#endif
entt::basic_process
Base class for processes.
Definition process.hpp:72
entt::basic_scheduler::empty
bool empty() const noexcept
Returns true if at least a process is currently scheduled.
Definition scheduler.hpp:132
entt::basic_scheduler::basic_scheduler
basic_scheduler(const basic_scheduler &)=delete
Default copy constructor, deleted on purpose.
entt::basic_scheduler< std::uint32_t >::allocator_type
Allocator allocator_type
Definition scheduler.hpp:46
entt::basic_scheduler::basic_scheduler
basic_scheduler()
Default constructor.
Definition scheduler.hpp:53
entt::basic_scheduler::size
size_type size() const noexcept
Number of processes currently scheduled.
Definition scheduler.hpp:124
entt::basic_scheduler::operator=
basic_scheduler & operator=(const basic_scheduler &)=delete
Default copy assignment operator, deleted on purpose.
entt::basic_scheduler::attach
type & attach(Args &&...args)
Schedules a process for the next tick.
Definition scheduler.hpp:154
entt::basic_scheduler::basic_scheduler
basic_scheduler(basic_scheduler &&other) noexcept
Move constructor.
Definition scheduler.hpp:70
entt::basic_scheduler< std::uint32_t >::size_type
std::size_t size_type
Definition scheduler.hpp:48
entt::basic_scheduler::basic_scheduler
basic_scheduler(const allocator_type &allocator)
Constructs a scheduler with a given allocator.
Definition scheduler.hpp:60
entt::basic_scheduler::~basic_scheduler
~basic_scheduler()=default
Default destructor.
entt::basic_scheduler::operator=
basic_scheduler & operator=(basic_scheduler &&other) noexcept
Move assignment operator.
Definition scheduler.hpp:97
entt::basic_scheduler< std::uint32_t >::type
base_type type
Definition scheduler.hpp:44
entt::basic_scheduler::update
void update(const delta_type delta, void *data=nullptr)
Updates all scheduled processes.
Definition scheduler.hpp:183
entt::basic_scheduler::basic_scheduler
basic_scheduler(basic_scheduler &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Definition scheduler.hpp:78
entt::basic_scheduler::abort
void abort(const bool immediate=false)
Aborts all scheduled processes.
Definition scheduler.hpp:211
entt::basic_scheduler::attach
type & attach(Func func)
Schedules a process for the next tick.
Definition scheduler.hpp:166
entt::basic_scheduler::get_allocator
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition scheduler.hpp:116
entt::basic_scheduler::swap
void swap(basic_scheduler &other) noexcept
Exchanges the contents with those of a given scheduler.
Definition scheduler.hpp:107
entt::basic_scheduler::clear
void clear()
Discards all scheduled processes.
Definition scheduler.hpp:142
entt::basic_scheduler< std::uint32_t >::delta_type
std::uint32_t delta_type
Definition scheduler.hpp:50
entt::compressed_pair
A compressed pair.
Definition compressed_pair.hpp:87
entt
EnTT default namespace.
Definition dense_map.hpp:22