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