first state machine code, exclusive already works
need to make final, parallel, timeouts
This commit is contained in:
126
libs/main/state_machine/pistatemachine_state.h
Normal file
126
libs/main/state_machine/pistatemachine_state.h
Normal file
@@ -0,0 +1,126 @@
|
||||
/*! \file pistatemachine_state.h
|
||||
* \ingroup StateMachine
|
||||
* \~\brief
|
||||
* \~english State machine node
|
||||
* \~russian Узел машины состояний
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
State machine node
|
||||
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_state_H
|
||||
#define pistatemachine_state_H
|
||||
|
||||
#include "pistatemachine_base.h"
|
||||
#include "pistring.h"
|
||||
|
||||
|
||||
//! \ingroup StateMachine
|
||||
//! \~\brief
|
||||
//! \~english
|
||||
//! \~russian
|
||||
class PIP_EXPORT PIStateBase {
|
||||
friend class PIStateMachine;
|
||||
friend class PITransitionBase;
|
||||
|
||||
public:
|
||||
PIStateBase(const PIString & n = {}): name_(n) { ; }
|
||||
virtual ~PIStateBase();
|
||||
|
||||
virtual void onEnter() {}
|
||||
virtual void onExit() {}
|
||||
|
||||
PIStateMachine * machine() const { return root; }
|
||||
PIStateBase * parent() const { return parent_state; }
|
||||
|
||||
void addState(PIStateBase * s);
|
||||
void addStates(PIVector<PIStateBase *> s);
|
||||
void setInitialState(PIStateBase * s);
|
||||
|
||||
PITransitionBase * addTransition(PIStateBase * target, int event_id);
|
||||
|
||||
void setParallel(bool yes) { is_parallel = yes; }
|
||||
|
||||
const PIString & getName() const { return name_; }
|
||||
bool isStateMachine() const { return is_root; }
|
||||
bool isActive() const { return is_active; }
|
||||
bool isParallel() const { return is_parallel; }
|
||||
bool isAtomic() const { return children.isEmpty(); }
|
||||
bool isCompound() const { return children.isNotEmpty(); }
|
||||
const PIVector<PIStateBase *> & getChildren() const { return children; }
|
||||
const PIVector<PITransitionBase *> & getTransitions() const { return transitions; }
|
||||
|
||||
void print(PIString prefix = {});
|
||||
|
||||
protected:
|
||||
bool start();
|
||||
void setActive(bool yes);
|
||||
void setActiveRecursive(bool yes);
|
||||
void setChildActiveRecursive(bool yes);
|
||||
void setChildActive(PIStateBase * s);
|
||||
void propagateRoot(PIStateMachine * r);
|
||||
void gatherActiveStates(PIVector<PIStateBase *> & output);
|
||||
void gatherPathToMachine(PIVector<PIStateBase *> & output);
|
||||
PIVector<PIStateBase *> pathToMachine();
|
||||
|
||||
bool is_active = false, is_root = false, is_parallel = false;
|
||||
PIVector<PIStateBase *> children;
|
||||
PIVector<PITransitionBase *> transitions;
|
||||
PIStateBase *active_state = nullptr, *initial_state = nullptr, *parent_state = nullptr;
|
||||
PIStateMachine * root = nullptr;
|
||||
PIString name_;
|
||||
};
|
||||
|
||||
|
||||
class PIP_EXPORT PIStateLambda: public PIStateBase {
|
||||
public:
|
||||
PIStateLambda(std::function<void()> on_enter, std::function<void()> on_exit = nullptr, const PIString & n = {}): PIStateBase(n) {
|
||||
enter = on_enter;
|
||||
exit = on_exit;
|
||||
}
|
||||
void onEnter() override {
|
||||
if (enter) enter();
|
||||
}
|
||||
void onExit() override {
|
||||
if (exit) exit();
|
||||
}
|
||||
|
||||
private:
|
||||
std::function<void()> enter, exit;
|
||||
};
|
||||
|
||||
|
||||
class PIP_EXPORT PIStateFinal: public PIStateBase {
|
||||
public:
|
||||
PIStateFinal(std::function<void()> on_enter, std::function<void()> on_exit = nullptr, const PIString & n = {}): PIStateBase(n) {
|
||||
enter = on_enter;
|
||||
exit = on_exit;
|
||||
}
|
||||
void onEnter() override {
|
||||
if (enter) enter();
|
||||
}
|
||||
void onExit() override {
|
||||
if (exit) exit();
|
||||
}
|
||||
|
||||
private:
|
||||
std::function<void()> enter, exit;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user