state machine, parallel seems to work, final state and info about active atomic states

This commit is contained in:
2024-07-17 21:11:01 +03:00
parent 3db26a762c
commit abdba6d68b
6 changed files with 168 additions and 61 deletions

View File

@@ -44,9 +44,13 @@ public:
virtual void onEnter() {}
virtual void onExit() {}
virtual void onFinish() {}
PIStateMachine * machine() const { return root; }
PIStateBase * parent() const { return parent_state; }
PIStateBase * activeChild() const { return active_state; }
PIVector<PIStateBase *> activeChildren() const;
PIVector<PIStateBase *> activeAtomics() const;
void addState(PIStateBase * s);
void addStates(PIVector<PIStateBase *> s);
@@ -60,6 +64,7 @@ public:
bool isStateMachine() const { return is_root; }
bool isActive() const { return is_active; }
bool isParallel() const { return is_parallel; }
bool isFinal() const { return is_final; }
bool isAtomic() const { return children.isEmpty(); }
bool isCompound() const { return children.isNotEmpty(); }
const PIVector<PIStateBase *> & getChildren() const { return children; }
@@ -71,14 +76,17 @@ protected:
bool start();
void setActive(bool yes);
void setActiveRecursive(bool yes);
void setChildActiveRecursive(bool yes);
void setChildActive(PIStateBase * s);
void setChildrenActive(bool yes);
void setChildrenActiveRecursive(bool yes);
void activeChild(PIStateBase * c);
void childActived(PIStateBase * s);
void propagateRoot(PIStateMachine * r);
void gatherActiveStates(PIVector<PIStateBase *> & output);
void gatherActiveAtomicStates(PIVector<PIStateBase *> & output) const;
void gatherPathToMachine(PIVector<PIStateBase *> & output);
PIVector<PIStateBase *> pathToMachine();
bool is_active = false, is_root = false, is_parallel = false;
bool is_active = false, is_root = false, is_parallel = false, is_final = false;
PIVector<PIStateBase *> children;
PIVector<PITransitionBase *> transitions;
PIStateBase *active_state = nullptr, *initial_state = nullptr, *parent_state = nullptr;
@@ -107,20 +115,26 @@ private:
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;
PIStateFinal(std::function<void()> on_finish = nullptr, const PIString & n = {}): PIStateBase(n) {
is_final = true;
enter = on_finish;
}
void onEnter() override {
if (enter) enter();
}
void onExit() override {
if (exit) exit();
}
private:
std::function<void()> enter, exit;
std::function<void()> enter;
};
inline PICout operator<<(PICout c, PIStateBase * s) {
if (!s)
c << "state(nullptr)";
else
c << ("state(" + s->getName() + ")");
return c;
}
#endif