Files
pip/libs/main/state_machine/pistatemachine_transition.h
peri4 cd7e053fc5 version 4.2.0
move toStdFunction() to pibase.h
refactor PIParseHelper, now it much more abstract and useful
fix PIIODevice::createFromFullPath() when whitespaces at start or end are presence
PIStreamPacker add events for start and end packet receive
PIClientServer::ClientBase add virtual methods for start and end packet receive. also one can enable diagnostics with enableDiagnostics() method
PICout now call flush() on each end of output
add PIString::entries(const PIString & str)
2024-10-15 12:02:18 +03:00

102 lines
2.9 KiB
C++

/*! \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"
#include "pitimer.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);
virtual ~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(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:
virtual void enabled() {}
virtual void disabled() {}
int eventID = 0;
PIStateBase *source_state = nullptr, *target_state = nullptr;
PIStateMachine * root = nullptr;
PIStateMachineHelpers::FunctionBase * guard = nullptr;
std::function<void()> action;
};
class PIP_EXPORT PITransitionTimeout: public PITransitionBase {
public:
PITransitionTimeout(PIStateBase * source, PIStateBase * target, PISystemTime timeout);
~PITransitionTimeout();
private:
void enabled() override;
void disabled() override;
PITimer timer;
};
#endif