Cutelyst  3.4.0
actionchain.cpp
1 /*
2  * Copyright (C) 2015-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "actionchain_p.h"
19 #include "request_p.h"
20 
21 #include "context_p.h"
22 
23 using namespace Cutelyst;
24 
25 ActionChain::ActionChain(const ActionList &chain, QObject *parent) : Action(new ActionChainPrivate, parent)
26 {
27  Q_D(ActionChain);
28  d->chain = chain;
29 
30  const Action *final = d->chain.last();
31 
32  QVariantHash args;
33  args.insert(QStringLiteral("namespace"), final->ns());
34  setupAction(args, nullptr);
35 
36  setName(QLatin1Char('_') + final->name());
37  setReverse(final->reverse());
38  setAttributes(final->attributes());
39  setController(final->controller());
40 
41  for (Action *action : chain) {
42  // FINAL should not have captures?
43  if (/*action != final && */action->numberOfCaptures() > 0) {
44  d->captures += action->numberOfCaptures();
45  }
46  }
47 }
48 
50 {
51  Q_D(const ActionChain);
52  return d->chain;
53 }
54 
55 qint8 ActionChain::numberOfCaptures() const noexcept
56 {
57  Q_D(const ActionChain);
58  return d->captures;
59 }
60 
62 {
63  Q_D(const ActionChain);
64 
65  Request *request = c->request();
66  const QStringList captures = request->captures();
67  const QStringList currentArgs = request->args();
68  const ActionList chain = d->chain;
69 
70  int &asyncDetached = c->d_ptr->asyncDetached;
71  int &captured = c->d_ptr->chainedCaptured;
72  int &chainedIx = c->d_ptr->chainedIx;
73 
74  for (; chainedIx < chain.size(); ++chainedIx) {
75  if (asyncDetached) {
76  c->d_ptr->pendingAsync.prepend(this);
77  request->setArguments(currentArgs);
78  break;
79  }
80 
81  Action *action = chain.at(chainedIx);
82 
83  QStringList args;
84  while (args.size() < action->numberOfCaptures() && captured < captures.size()) {
85  args.append(captures.at(captured++));
86  }
87 
88  // Final action gets args instead of captures
89  request->setArguments(action != chain.last() ? args : currentArgs);
90  if (!action->dispatch(c)) {
91  return false;
92  }
93  }
94 
95  return true;
96 }
97 
98 #include "moc_actionchain.cpp"
Holds a chain of Cutelyst Actions.
Definition: actionchain.h:37
ActionChain(const ActionList &chain, QObject *parent=nullptr)
Definition: actionchain.cpp:25
virtual qint8 numberOfCaptures() const noexcept override
Definition: actionchain.cpp:55
ActionList chain() const noexcept
Definition: actionchain.cpp:49
virtual bool doExecute(Context *c) override
Definition: actionchain.cpp:61
This class represents a Cutelyst Action.
Definition: action.h:48
void setAttributes(const ParamsMultiMap &attributes)
Definition: action.cpp:91
void setupAction(const QVariantHash &args, Application *app)
Definition: action.cpp:57
bool dispatch(Context *c)
Definition: action.h:94
virtual qint8 numberOfCaptures() const noexcept
Definition: action.cpp:141
void setController(Controller *controller)
Definition: action.cpp:51
void setReverse(const QString &reverse)
Definition: component.cpp:62
void setName(const QString &name)
Definition: component.cpp:50
The Cutelyst Context.
Definition: context.h:52
QStringList captures() const noexcept
Definition: request.cpp:174
void setArguments(const QStringList &arguments)
Definition: request.cpp:168
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
void append(const T &value)
const T & at(int i) const const
int size() const const
const T & at(int i) const const
T & last()
int size() const const