1 #ifndef ENTT_PROCESS_PROCESS_HPP 2 #define ENTT_PROCESS_PROCESS_HPP 17 enum class State: unsigned int {
28 using tag = std::integral_constant<State, state>;
84 template<
typename Derived,
typename Delta>
86 template<
typename Target = Derived>
87 auto tick(
int, tag<State::UNINITIALIZED>)
88 -> decltype(std::declval<Target>().init()) {
89 static_cast<Target *
>(
this)->init();
92 template<
typename Target = Derived>
93 auto tick(
int, tag<State::RUNNING>, Delta delta)
94 -> decltype(std::declval<Target>().update(delta)) {
95 static_cast<Target *
>(
this)->update(delta);
98 template<
typename Target = Derived>
99 auto tick(
int, tag<State::SUCCEEDED>)
100 -> decltype(std::declval<Target>().succeeded()) {
101 static_cast<Target *
>(
this)->succeeded();
104 template<
typename Target = Derived>
105 auto tick(
int, tag<State::FAILED>)
106 -> decltype(std::declval<Target>().failed()) {
107 static_cast<Target *
>(
this)->failed();
110 template<
typename Target = Derived>
111 auto tick(
int, tag<State::ABORTED>)
112 -> decltype(std::declval<Target>().aborted()) {
113 static_cast<Target *
>(
this)->aborted();
116 template<State S,
typename... Args>
117 void tick(
char, tag<S>, Args&&...) {}
128 current = State::SUCCEEDED;
140 current = State::FAILED;
151 if(current == State::RUNNING) {
152 current = State::PAUSED;
163 if(current == State::PAUSED) {
164 current = State::RUNNING;
174 static_assert(std::is_base_of<Process, Derived>::value,
"!");
185 void abort(
bool immediately =
false) noexcept {
187 current = State::ABORTED;
200 return current == State::RUNNING || current == State::PAUSED;
208 return current == State::FINISHED;
216 return current == State::PAUSED;
233 case State::UNINITIALIZED:
234 tick(0, tag<State::UNINITIALIZED>{});
235 current = State::RUNNING;
238 tick(0, tag<State::RUNNING>{}, delta);
246 case State::SUCCEEDED:
247 tick(0, tag<State::SUCCEEDED>{});
248 current = State::FINISHED;
251 tick(0, tag<State::FAILED>{});
252 current = State::FINISHED;
256 tick(0, tag<State::ABORTED>{});
257 current = State::FINISHED;
267 State current{State::UNINITIALIZED};
310 template<
typename Func,
typename Delta>
317 template<
typename... Args>
319 : Func{std::forward<Args>(args)...}
327 Func::operator()(delta, [
this](){ this->succeed(); }, [
this](){ this->fail(); });
335 #endif // ENTT_PROCESS_PROCESS_HPP bool rejected() const noexcept
Returns true if a process terminated with errors.
void succeed() noexcept
Terminates a process with success if it's still alive.
void unpause() noexcept
Restarts a process if it's paused.
void fail() noexcept
Terminates a process with errors if it's still alive.
Base class for processes.
ProcessAdaptor(Args &&... args)
Constructs a process adaptor from a lambda or a functor.
void tick(Delta delta)
Updates a process and its internal state if required.
bool alive() const noexcept
Returns true if a process is either running or paused.
Adaptor for lambdas and functors to turn them into processes.
Delta delta_type
Type used to provide elapsed time.
void update(Delta delta)
Updates a process and its internal state if required.
void abort(bool immediately=false) noexcept
Aborts a process if it's still alive.
~Process() noexcept
Default destructor.
bool dead() const noexcept
Returns true if a process is already terminated.
void pause() noexcept
Stops a process if it's in a running state.
bool paused() const noexcept
Returns true if a process is currently paused.