forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactionchain.cpp
88 lines (73 loc) · 2.44 KB
/
actionchain.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
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (C) 2015-2017 Daniel Nicoletti <[email protected]>
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "actionchain_p.h"
#include "request_p.h"
#include "context.h"
using namespace Cutelyst;
ActionChain::ActionChain(const ActionList &chain, QObject *parent) : Action(new ActionChainPrivate, parent)
{
Q_D(ActionChain);
d->chain = chain;
Action *final = d->chain.takeLast();
d->final = final;
QVariantHash args;
args.insert(QStringLiteral("namespace"), final->ns());
setupAction(args, nullptr);
setName(QLatin1Char('_') + final->name());
setReverse(final->reverse());
setAttributes(final->attributes());
setController(final->controller());
for (Action *action : chain) {
if (action->numberOfCaptures() > 0) {
d->captures += action->numberOfCaptures();
}
}
}
ActionList ActionChain::chain() const
{
Q_D(const ActionChain);
return d->chain;
}
qint8 ActionChain::numberOfCaptures() const
{
Q_D(const ActionChain);
return d->captures;
}
bool ActionChain::doExecute(Context *c)
{
Q_D(ActionChain);
Request *request = c->request();
const QStringList captures = request->captures();
const QStringList currentArgs = request->args();
const ActionList chain = d->chain;
Action *final = d->final;
int captured = 0;
for (Action *action : chain) {
QStringList args;
while (args.size() < action->numberOfCaptures() && captured < captures.size()) {
args.append(captures.at(captured++));
}
request->setArguments(args);
if (!action->dispatch(c)) {
return false;
}
}
request->setArguments(currentArgs);
return final->dispatch(c);
}
#include "moc_actionchain.cpp"