forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.cpp
176 lines (147 loc) · 3.93 KB
/
component.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (C) 2014-2017 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "component_p.h"
#include "common.h"
#include "context.h"
using namespace Cutelyst;
Component::Component(QObject *parent) : QObject(parent)
, d_ptr(new ComponentPrivate)
{
}
Component::Component(ComponentPrivate* d, QObject *parent) : QObject(parent)
, d_ptr(d)
{
}
Component::~Component()
{
delete d_ptr;
}
Component::Modifiers Component::modifiers() const
{
return Component::None;
}
QString Component::name() const
{
Q_D(const Component);
return d->name;
}
void Component::setName(const QString &name)
{
Q_D(Component);
d->name = name;
}
QString Component::reverse() const
{
Q_D(const Component);
return d->reverse;
}
void Component::setReverse(const QString &reverse)
{
Q_D(Component);
d->reverse = reverse;
}
bool Component::init(Cutelyst::Application *application, const QVariantHash &args)
{
Q_UNUSED(application)
Q_UNUSED(args)
return true;
}
bool Component::execute(Context *c)
{
Q_D(Component);
if (d->proccessRoles) {
const auto beforeRoles = d->beforeRoles;
for (Component *code : beforeRoles) {
if (!code->beforeExecute(c)) {
return false;
}
}
QStack<Component *> stack = d->aroundRoles;
// first item on the stack is always the execution code
stack.push_front(this);
if (!aroundExecute(c, stack)) {
return false;
}
const auto afterRoles = d->afterRoles;
for (Component *code : afterRoles) {
if (!code->afterExecute(c)) {
return false;
}
}
// Do not call doExecute twice
return true;
}
return doExecute(c);
}
bool Component::beforeExecute(Context *c)
{
Q_UNUSED(c)
return true;
}
bool Component::aroundExecute(Context *c, QStack<Cutelyst::Component *> stack)
{
Q_UNUSED(c)
int stackSize = stack.size();
if (stackSize == 1) {
Component *code = stack.pop();
return code->doExecute(c);
} else if (stackSize > 1) {
Component *code = stack.pop();
return code->aroundExecute(c, stack);
}
// Should NEVER happen
qCCritical(CUTELYST_COMPONENT) << "Reached end of the stack!" << c->req()->uri();
return false;
}
bool Component::afterExecute(Context *c)
{
Q_UNUSED(c)
return true;
}
bool Component::doExecute(Context *c)
{
Q_UNUSED(c)
return true;
}
void Component::applyRoles(const QStack<Cutelyst::Component *> &roles)
{
Q_D(Component);
for (Component *code : roles) {
if (code->modifiers() & BeforeExecute) {
d->beforeRoles.push(code);
}
if (code->modifiers() & AroundExecute) {
d->aroundRoles.push(code);
}
if (code->modifiers() & AfterExecute) {
d->afterRoles.push(code);
}
}
d->roles = roles;
d->proccessRoles = true;
}
bool Component::dispatcherReady(const Dispatcher *dispatch, Controller *controller)
{
Q_D(Component);
const auto roles = d->roles;
for (Component *code : roles) {
code->dispatcherReady(dispatch, controller);
}
return true;
}
#include "moc_component.cpp"