cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
component.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2014-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "common.h"
6 #include "component_p.h"
7 #include "context.h"
8 
9 using namespace Cutelyst;
10 
12  : QObject(parent)
13  , d_ptr(new ComponentPrivate)
14 {
15 }
16 
17 Component::Component(ComponentPrivate *d, QObject *parent)
18  : QObject(parent)
19  , d_ptr(d)
20 {
21 }
22 
24 {
25  delete d_ptr;
26 }
27 
28 Component::Modifiers Component::modifiers() const
29 {
30  return Component::None;
31 }
32 
33 QString Component::name() const noexcept
34 {
35  Q_D(const Component);
36  return d->name;
37 }
38 
39 void Component::setName(const QString &name)
40 {
41  Q_D(Component);
42  d->name = name;
43 }
44 
45 QString Component::reverse() const noexcept
46 {
47  Q_D(const Component);
48  return d->reverse;
49 }
50 
51 void Component::setReverse(const QString &reverse)
52 {
53  Q_D(Component);
54  d->reverse = reverse;
55 }
56 
57 bool Component::init(Cutelyst::Application *application, const QVariantHash &args)
58 {
59  Q_UNUSED(application)
60  Q_UNUSED(args)
61  return true;
62 }
63 
65 {
66  Q_D(Component);
67 
68  if (d->proccessRoles) {
69  const auto beforeRoles = d->beforeRoles;
70  for (Component *code : beforeRoles) {
71  if (!code->beforeExecute(c)) {
72  return false;
73  }
74  }
75 
76  QStack<Component *> stack = d->aroundRoles;
77  // first item on the stack is always the execution code
78  stack.push_front(this);
79  if (!aroundExecute(c, stack)) {
80  return false;
81  }
82 
83  const auto afterRoles = d->afterRoles;
84  for (Component *code : afterRoles) {
85  if (!code->afterExecute(c)) {
86  return false;
87  }
88  }
89 
90  // Do not call doExecute twice
91  return true;
92  }
93 
94  return doExecute(c);
95 }
96 
98 {
99  Q_UNUSED(c)
100  return true;
101 }
102 
104 {
105  Q_UNUSED(c)
106 
107  int stackSize = stack.size();
108  if (stackSize == 1) {
109  Component *code = stack.pop();
110  return code->doExecute(c);
111  } else if (stackSize > 1) {
112  Component *code = stack.pop();
113  return code->aroundExecute(c, stack);
114  }
115 
116  // Should NEVER happen
117  qCCritical(CUTELYST_COMPONENT) << "Reached end of the stack!" << c->req()->uri();
118  return false;
119 }
120 
122 {
123  Q_UNUSED(c)
124  return true;
125 }
126 
128 {
129  Q_UNUSED(c)
130  return true;
131 }
132 
134 {
135  Q_D(Component);
136 
137  for (Component *code : roles) {
138  if (code->modifiers() & BeforeExecute) {
139  d->beforeRoles.push(code);
140  }
141 
142  if (code->modifiers() & AroundExecute) {
143  d->aroundRoles.push(code);
144  }
145 
146  if (code->modifiers() & AfterExecute) {
147  d->afterRoles.push(code);
148  }
149  }
150  d->roles = roles;
151  d->proccessRoles = true;
152 }
153 
154 bool Component::dispatcherReady(const Dispatcher *dispatch, Controller *controller)
155 {
156  Q_D(Component);
157 
158  const auto roles = d->roles;
159  for (Component *code : roles) {
160  code->dispatcherReady(dispatch, controller);
161  }
162  return true;
163 }
164 
165 #include "moc_component.cpp"
The Cutelyst application.
Definition: application.h:66
The Cutelyst Component base class.
Definition: component.h:30
virtual Modifiers modifiers() const
Definition: component.cpp:28
virtual bool doExecute(Context *c)
Definition: component.cpp:127
void setReverse(const QString &reverse)
Definition: component.cpp:51
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:57
virtual bool beforeExecute(Context *c)
Definition: component.cpp:97
virtual bool dispatcherReady(const Dispatcher *dispatch, Controller *controller)
Definition: component.cpp:154
Component(QObject *parent=nullptr)
Definition: component.cpp:11
virtual bool aroundExecute(Context *c, QStack< Component * > stack)
Definition: component.cpp:103
void applyRoles(const QStack< Component * > &roles)
Definition: component.cpp:133
QString reverse() const noexcept
Definition: component.cpp:45
bool execute(Context *c)
Definition: component.cpp:64
QString name() const noexcept
Definition: component.cpp:33
void setName(const QString &name)
Definition: component.cpp:39
virtual ~Component() override
Definition: component.cpp:23
virtual bool afterExecute(Context *c)
Definition: component.cpp:121
The Cutelyst Context.
Definition: context.h:42
Request * req
Definition: context.h:66
Cutelyst Controller base class.
Definition: controller.h:56
The Cutelyst Dispatcher.
Definition: dispatcher.h:29
The Cutelyst namespace holds all public Cutelyst API.
void push_front(QList::parameter_type value)
qsizetype size() const const