cutelyst  5.0.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  if (!std::ranges::all_of(beforeRoles,
71  [&](Component *code) { return code->beforeExecute(c); })) {
72  return false;
73  }
74 
75  QStack<Component *> stack = d->aroundRoles;
76  // first item on the stack is always the execution code
77  stack.push_front(this);
78  if (!aroundExecute(c, stack)) {
79  return false;
80  }
81 
82  const auto afterRoles = d->afterRoles;
83  if (!std::ranges::all_of(afterRoles,
84  [&](Component *code) { return code->afterExecute(c); })) {
85  return false;
86  }
87 
88  // Do not call doExecute twice
89  return true;
90  }
91 
92  return doExecute(c);
93 }
94 
96 {
97  Q_UNUSED(c)
98  return true;
99 }
100 
102 {
103  Q_UNUSED(c)
104 
105  int stackSize = stack.size();
106  if (stackSize == 1) {
107  Component *code = stack.pop();
108  return code->doExecute(c);
109  } else if (stackSize > 1) {
110  Component *code = stack.pop();
111  return code->aroundExecute(c, stack);
112  }
113 
114  // Should NEVER happen
115  qCCritical(CUTELYST_COMPONENT) << "Reached end of the stack!" << c->req()->uri();
116  return false;
117 }
118 
120 {
121  Q_UNUSED(c)
122  return true;
123 }
124 
126 {
127  Q_UNUSED(c)
128  return true;
129 }
130 
132 {
133  Q_D(Component);
134 
135  for (Component *code : roles) {
136  if (code->modifiers() & BeforeExecute) {
137  d->beforeRoles.push(code);
138  }
139 
140  if (code->modifiers() & AroundExecute) {
141  d->aroundRoles.push(code);
142  }
143 
144  if (code->modifiers() & AfterExecute) {
145  d->afterRoles.push(code);
146  }
147  }
148  d->roles = roles;
149  d->proccessRoles = true;
150 }
151 
152 bool Component::dispatcherReady(const Dispatcher *dispatch, Controller *controller)
153 {
154  Q_D(Component);
155 
156  const auto roles = d->roles;
157  for (Component *code : roles) {
158  code->dispatcherReady(dispatch, controller);
159  }
160  return true;
161 }
162 
163 #include "moc_component.cpp"
bool execute(Context *c)
Definition: component.cpp:64
void setName(const QString &name)
Definition: component.cpp:39
virtual bool afterExecute(Context *c)
Definition: component.cpp:119
void setReverse(const QString &reverse)
Definition: component.cpp:51
virtual bool doExecute(Context *c)
Definition: component.cpp:125
Request req
Definition: context.h:67
QString reverse() const noexcept
Definition: component.cpp:45
The Cutelyst Component base class.
Definition: component.h:30
qsizetype size() const const
The Cutelyst Context.
Definition: context.h:42
Cutelyst Controller base class.
Definition: controller.h:55
QString name() const noexcept
Definition: component.cpp:33
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:57
The Cutelyst namespace holds all public Cutelyst API.
virtual Modifiers modifiers() const
Definition: component.cpp:28
void applyRoles(const QStack< Component *> &roles)
Definition: component.cpp:131
void push_front(parameter_type value)
virtual ~Component() override
Definition: component.cpp:23
Component(QObject *parent=nullptr)
Definition: component.cpp:11
virtual bool dispatcherReady(const Dispatcher *dispatch, Controller *controller)
Definition: component.cpp:152
virtual bool aroundExecute(Context *c, QStack< Component *> stack)
Definition: component.cpp:101
The Cutelyst application.
Definition: application.h:72
The Cutelyst Dispatcher.
Definition: dispatcher.h:28
virtual bool beforeExecute(Context *c)
Definition: component.cpp:95