File: main.cpp

package info (click to toggle)
qt6-scxml 6.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,772 kB
  • sloc: cpp: 28,750; javascript: 330; sh: 95; python: 78; makefile: 26
file content (104 lines) | stat: -rw-r--r-- 2,180 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QEvent>
#include <QtStateMachine/QAbstractTransition>
#include <QtStateMachine/QState>
#include <QtStateMachine/QStateMachine>

static constexpr QEvent::Type PingEventType = QEvent::Type(QEvent::User + 2);
static constexpr QEvent::Type PongEventType = QEvent::Type(QEvent::User + 3);

//! [0]
class PingEvent : public QEvent
{
public:
    PingEvent() : QEvent(PingEventType) { }
};

class PongEvent : public QEvent
{
public:
    PongEvent() : QEvent(PongEventType) { }
};
//! [0]

//! [1]
class Pinger : public QState
{
public:
    explicit Pinger(QState *parent) : QState(parent) { }

protected:
    void onEntry(QEvent *) override
    {
        machine()->postEvent(new PingEvent);
        qInfo() << "ping?";
    }
};
//! [1]

//! [3]
class PongTransition : public QAbstractTransition
{
public:
    PongTransition() {}

protected:
    bool eventTest(QEvent *e) override { return (e->type() == PingEventType); }

    void onTransition(QEvent *) override
    {
        machine()->postDelayedEvent(new PingEvent, 500);
        qInfo() << "ping?";
    }
};
//! [3]

//! [2]
class PingTransition : public QAbstractTransition
{
public:
    PingTransition() {}

protected:
    bool eventTest(QEvent *e) override { return e->type() == PingEventType; }

    void onTransition(QEvent *) override
    {
        machine()->postDelayedEvent(new PongEvent, 500);
        qInfo() << "pong!";
    }
};
//! [2]

//! [4]
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    QStateMachine machine;
    auto group = new QState(QState::ParallelStates);
    group->setObjectName("group");
//! [4]

//! [5]
    auto pinger = new Pinger(group);
    pinger->setObjectName("pinger");
    pinger->addTransition(new PongTransition);

    auto ponger = new QState(group);
    ponger->setObjectName("ponger");
    ponger->addTransition(new PingTransition);
//! [5]

//! [6]
    machine.addState(group);
    machine.setInitialState(group);
    machine.start();

    return app.exec();
}
//! [6]