/*! \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 . */ #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 PITransitionBase * addGuard(std::function f) { static_assert(std::is_same::value, "guard function should return bool!"); piDeleteSafety(guard); guard = PIStateMachineHelpers::makeFunction(f); return this; } template PITransitionBase * addGuard(L f) { return addGuard(toStdFunction(f)); } template bool testGuard(Args... args) { if (!guard) return true; if (guard->formatHash() != PIStateMachineHelpers::Function().formatHash()) { piCout << "invalid arguments format!"; return false; } return reinterpret_cast *>(guard)->func(args...); } PITransitionBase * addAction(std::function 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 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