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>
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
55
60 explicit basic_scheduler(const allocator_type &allocator)
61 : handlers{allocator, allocator} {}
62
65
71 : handlers{std::move(other.handlers)} {}
72
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
91
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:
223};
224
225} // namespace entt
226
227#endif
Base class for processes.
Definition process.hpp:72
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.
Definition scheduler.hpp:53
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.
Definition scheduler.hpp:70
basic_scheduler(const allocator_type &allocator)
Constructs a scheduler with a given allocator.
Definition scheduler.hpp:60
~basic_scheduler()=default
Default destructor.
basic_scheduler & operator=(basic_scheduler &&other) noexcept
Move assignment operator.
Definition scheduler.hpp:97
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.
Definition scheduler.hpp:78
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.
A compressed pair.
EnTT default namespace.
Definition dense_map.hpp:22