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