-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcommand.cpp
76 lines (66 loc) · 1.58 KB
/
command.cpp
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
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "command.h"
#include "servicetool.h"
Command::Command(CommandService *service)
: m_service(service)
{
setAutoDelete(true);
}
void Command::exit()
{
m_isExit = true;
}
Command::State Command::state() const
{
return m_state;
}
CommandService *Command::service() const
{
return m_service;
}
void Command::installMessageExtractor(Extractor *extractor)
{
Q_ASSERT(extractor);
switch (extractor->direction()) {
case Extractor::CommandToTransporter: {
if (m_outEx) {
qWarning("This command already has the out direction Extractor.");
return;
}
m_outEx = extractor;
}
break;
case Extractor::TransporterToCommand: {
if (m_inEx) {
qWarning("This command already has the in direction Extractor.");
return;
}
m_inEx = extractor;
}
break;
}
}
void Command::run()
{
Q_ASSERT(m_inEx);
while (true) {
m_state = Preparing;
CommandMessage *msg = m_inEx->takeFromTransporter();
if (m_isExit) {
delete msg;
break;
}
m_state = Running;
QList<CommandMessage *> destMsgs = doExecute(msg);
// 允许复用同一个 msg 的情况
if (!destMsgs.contains(msg))
delete msg;
if (m_outEx) {
for (auto destMsg : destMsgs)
m_outEx->pushToTransporter(destMsg);
}
}
m_state = Preparing;
}