1 #ifndef ENTT_PROCESS_PROCESS_HPP 2 #define ENTT_PROCESS_PROCESS_HPP 8 #include "../config/config.h" 72 template<
typename Derived,
typename Delta>
74 enum class State: unsigned int {
85 using tag = std::integral_constant<State, state>;
87 template<
typename Target = Derived>
88 auto tick(
int, tag<State::UNINITIALIZED>,
void *data)
89 -> decltype(std::declval<Target>().init(data)) {
90 static_cast<Target *
>(
this)->init(data);
93 template<
typename Target = Derived>
94 auto tick(
int, tag<State::RUNNING>, Delta delta,
void *data)
95 -> decltype(std::declval<Target>().update(delta, data)) {
96 static_cast<Target *
>(
this)->update(delta, data);
99 template<
typename Target = Derived>
100 auto tick(
int, tag<State::SUCCEEDED>)
101 -> decltype(std::declval<Target>().succeeded()) {
102 static_cast<Target *
>(
this)->succeeded();
105 template<
typename Target = Derived>
106 auto tick(
int, tag<State::FAILED>)
107 -> decltype(std::declval<Target>().failed()) {
108 static_cast<Target *
>(
this)->failed();
111 template<
typename Target = Derived>
112 auto tick(
int, tag<State::ABORTED>)
113 -> decltype(std::declval<Target>().aborted()) {
114 static_cast<Target *
>(
this)->aborted();
117 template<State S,
typename... Args>
118 void tick(
char, tag<S>, Args &&...)
const ENTT_NOEXCEPT {}
129 current = State::SUCCEEDED;
141 current = State::FAILED;
152 if(current == State::RUNNING) {
153 current = State::PAUSED;
164 if(current == State::PAUSED) {
165 current = State::RUNNING;
175 static_assert(std::is_base_of<Process, Derived>::value,
"!");
186 void abort(
const bool immediately =
false) ENTT_NOEXCEPT {
188 current = State::ABORTED;
201 return current == State::RUNNING || current == State::PAUSED;
208 bool dead() const ENTT_NOEXCEPT {
209 return current == State::FINISHED;
217 return current == State::PAUSED;
233 void tick(
const Delta delta,
void *data =
nullptr) {
235 case State::UNINITIALIZED:
236 tick(0, tag<State::UNINITIALIZED>{}, data);
237 current = State::RUNNING;
240 tick(0, tag<State::RUNNING>{}, delta, data);
248 case State::SUCCEEDED:
249 tick(0, tag<State::SUCCEEDED>{});
250 current = State::FINISHED;
253 tick(0, tag<State::FAILED>{});
254 current = State::FINISHED;
258 tick(0, tag<State::ABORTED>{});
259 current = State::FINISHED;
269 State current{State::UNINITIALIZED};
313 template<
typename Func,
typename Delta>
320 template<
typename... Args>
322 : Func{std::forward<Args>(args)...}
330 void update(
const Delta delta,
void *data) {
331 Func::operator()(delta, data, [
this]() { this->
succeed(); }, [
this]() { this->
fail(); });
339 #endif // ENTT_PROCESS_PROCESS_HPP Base class for processes.
void unpause() ENTT_NOEXCEPT
Restarts a process if it's paused.
bool dead() const ENTT_NOEXCEPT
Returns true if a process is already terminated.
bool rejected() const ENTT_NOEXCEPT
Returns true if a process terminated with errors.
void abort(const bool immediately=false) ENTT_NOEXCEPT
Aborts a process if it's still alive.
virtual ~Process() ENTT_NOEXCEPT
Default destructor.
ProcessAdaptor(Args &&... args)
Constructs a process adaptor from a lambda or a functor.
Adaptor for lambdas and functors to turn them into processes.
void fail() ENTT_NOEXCEPT
Terminates a process with errors if it's still alive.
bool alive() const ENTT_NOEXCEPT
Returns true if a process is either running or paused.
void succeed() ENTT_NOEXCEPT
Terminates a process with success if it's still alive.
Delta delta_type
Type used to provide elapsed time.
void update(const Delta delta, void *data)
Updates a process and its internal state if required.
void pause() ENTT_NOEXCEPT
Stops a process if it's in a running state.
void tick(const Delta delta, void *data=nullptr)
Updates a process and its internal state if required.
bool paused() const ENTT_NOEXCEPT
Returns true if a process is currently paused.