Cutelyst  3.5.0
component.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2014-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "component_p.h"
6 #include "common.h"
7 #include "context.h"
8 
9 using namespace Cutelyst;
10 
12  , d_ptr(new ComponentPrivate)
13 {
14 }
15 
16 Component::Component(ComponentPrivate* d, QObject *parent) : QObject(parent)
17  , d_ptr(d)
18 {
19 }
20 
21 Component::~Component()
22 {
23  delete d_ptr;
24 }
25 
26 Component::Modifiers Component::modifiers() const
27 {
28  return Component::None;
29 }
30 
32 {
33  Q_D(const Component);
34  return d->name;
35 }
36 
37 void Component::setName(const QString &name)
38 {
39  Q_D(Component);
40  d->name = name;
41 }
42 
44 {
45  Q_D(const Component);
46  return d->reverse;
47 }
48 
49 void Component::setReverse(const QString &reverse)
50 {
51  Q_D(Component);
52  d->reverse = reverse;
53 }
54 
55 bool Component::init(Cutelyst::Application *application, const QVariantHash &args)
56 {
57  Q_UNUSED(application)
58  Q_UNUSED(args)
59  return true;
60 }
61 
63 {
64  Q_D(Component);
65 
66  if (d->proccessRoles) {
67  const auto beforeRoles = d->beforeRoles;
68  for (Component *code : beforeRoles) {
69  if (!code->beforeExecute(c)) {
70  return false;
71  }
72  }
73 
74  QStack<Component *> stack = d->aroundRoles;
75  // first item on the stack is always the execution code
76  stack.push_front(this);
77  if (!aroundExecute(c, stack)) {
78  return false;
79  }
80 
81  const auto afterRoles = d->afterRoles;
82  for (Component *code : afterRoles) {
83  if (!code->afterExecute(c)) {
84  return false;
85  }
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:62
void setName(const QString &name)
Definition: component.cpp:37
virtual bool afterExecute(Context *c)
Definition: component.cpp:119
void setReverse(const QString &reverse)
Definition: component.cpp:49
virtual bool doExecute(Context *c)
Definition: component.cpp:125
The Cutelyst Component base class.
Definition: component.h:25
The Cutelyst Context.
Definition: context.h:38
Cutelyst Controller base class
Definition: controller.h:89
QString name() const
Definition: component.cpp:31
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:55
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
virtual Modifiers modifiers() const
Definition: component.cpp:26
void applyRoles(const QStack< Component *> &roles)
Definition: component.cpp:131
QString reverse() const
Definition: component.cpp:43
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
void push_front(T &&value)
The Cutelyst Application.
Definition: application.h:42
int size() const const
The Cutelyst Dispatcher.
Definition: dispatcher.h:27
virtual bool beforeExecute(Context *c)
Definition: component.cpp:95