first state machine code, exclusive already works

need to make final, parallel, timeouts
This commit is contained in:
2024-07-17 14:16:25 +03:00
parent b35561f74e
commit 3db26a762c
10 changed files with 753 additions and 2 deletions

View File

@@ -0,0 +1,84 @@
/*! \file pistatemachine_transition.h
* \ingroup StateMachine
* \~\brief
* \~english State machine transition
* \~russian Переход машины состояний
*/
/*
PIP - Platform Independent Primitives
State machine transition
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef pistatemachine_transition_H
#define pistatemachine_transition_H
#include "pistatemachine_base.h"
//! \ingroup StateMachine
//! \~\brief
//! \~english
//! \~russian
class PIP_EXPORT PITransitionBase {
friend class PIStateMachine;
friend class PIStateBase;
public:
PITransitionBase(PIStateBase * source, PIStateBase * target, int event_id);
~PITransitionBase();
PIStateMachine * machine() const { return root; }
PIStateBase * source() const { return source_state; }
PIStateBase * target() const { return target_state; }
template<typename R, typename... Args>
PITransitionBase * addGuard(std::function<R(Args...)> f) {
static_assert(std::is_same<R, bool>::value, "guard function should return bool!");
piDeleteSafety(guard);
guard = PIStateMachineHelpers::makeFunction(f);
return this;
}
template<typename L>
PITransitionBase * addGuard(L f) {
return addGuard(PIStateMachineHelpers::toStdFunction(f));
}
template<typename... Args>
bool testGuard(Args... args) {
if (!guard) return true;
if (guard->formatHash() != PIStateMachineHelpers::Function<Args...>().formatHash()) {
piCout << "invalid arguments format!";
return false;
}
return reinterpret_cast<PIStateMachineHelpers::Function<Args...> *>(guard)->func(args...);
}
PITransitionBase * addAction(std::function<void()> a);
void makeAction();
void trigger();
protected:
int eventID = 0;
PIStateBase *source_state = nullptr, *target_state = nullptr;
PIStateMachine * root = nullptr;
PIStateMachineHelpers::FunctionBase * guard = nullptr;
std::function<void()> action;
};
#endif