cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
actionchain.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2015-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "actionchain_p.h"
6 #include "context_p.h"
7 #include "request_p.h"
8 
9 using namespace Cutelyst;
10 
12  : Action(new ActionChainPrivate, parent)
13 {
14  Q_D(ActionChain);
15  d->chain = chain;
16 
17  const Action *final = d->chain.last();
18 
19  QVariantHash args;
20  args.insert(QStringLiteral("namespace"), final->ns());
21  setupAction(args, nullptr);
22 
23  setName(u'_' + final->name());
24  setReverse(final->reverse());
25  setAttributes(final->attributes());
26  setController(final->controller());
27 
28  for (Action *action : chain) {
29  // FINAL should not have captures?
30  if (/*action != final && */ action->numberOfCaptures() > 0) {
31  d->captures += action->numberOfCaptures();
32  }
33  }
34 }
35 
37 {
38  Q_D(const ActionChain);
39  return d->chain;
40 }
41 
42 qint8 ActionChain::numberOfCaptures() const noexcept
43 {
44  Q_D(const ActionChain);
45  return d->captures;
46 }
47 
49 {
50  Q_D(const ActionChain);
51 
52  Request *request = c->request();
53  const QStringList captures = request->captures();
54  const QStringList currentArgs = request->args();
55  const ActionList chain = d->chain;
56 
57  int &actionRefCount = c->d_ptr->actionRefCount;
58  int &captured = c->d_ptr->chainedCaptured;
59  int &chainedIx = c->d_ptr->chainedIx;
60 
61  for (; chainedIx < chain.size(); ++chainedIx) {
62  if (actionRefCount) {
63  c->d_ptr->pendingAsync.prepend(this);
64  request->setArguments(currentArgs);
65  break;
66  }
67 
68  Action *action = chain.at(chainedIx);
69 
70  QStringList args;
71  while (args.size() < action->numberOfCaptures() && captured < captures.size()) {
72  args.append(captures.at(captured++));
73  }
74 
75  // Final action gets args instead of captures
76  request->setArguments(action != chain.last() ? args : currentArgs);
77  ++actionRefCount;
78  const bool ret = action->dispatch(c);
79  --actionRefCount;
80  if (!ret) {
81  return false;
82  }
83  }
84 
85  return true;
86 }
87 
88 #include "moc_actionchain.cpp"
Holds a chain of Cutelyst actions.
Definition: actionchain.h:26
ActionChain(const ActionList &chain, QObject *parent=nullptr)
Definition: actionchain.cpp:11
qint8 numberOfCaptures() const noexcept override
Definition: actionchain.cpp:42
ActionList chain() const noexcept
Definition: actionchain.cpp:36
bool doExecute(Context *c) override
Definition: actionchain.cpp:48
This class represents a Cutelyst Action.
Definition: action.h:35
void setAttributes(const ParamsMultiMap &attributes)
Definition: action.cpp:80
void setupAction(const QVariantHash &args, Application *app)
Definition: action.cpp:46
bool dispatch(Context *c)
Definition: action.h:88
virtual qint8 numberOfCaptures() const
Definition: action.cpp:130
void setController(Controller *controller)
Definition: action.cpp:40
void setReverse(const QString &reverse)
Definition: component.cpp:51
void setName(const QString &name)
Definition: component.cpp:39
The Cutelyst Context.
Definition: context.h:42
Request * request
Definition: context.h:71
A request.
Definition: request.h:42
QStringList captures() const noexcept
Definition: request.cpp:161
void setArguments(const QStringList &arguments)
Definition: request.cpp:155
The Cutelyst namespace holds all public Cutelyst API.
void append(QList::parameter_type value)
QList::const_reference at(qsizetype i) const const
T & last()
qsizetype size() const const