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