forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.h
148 lines (125 loc) · 4.11 KB
/
component.h
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
/*
* 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
*/
#ifndef CUTELYST_COMPONENT_H
#define CUTELYST_COMPONENT_H
#include <QtCore/qobject.h>
#include <Cutelyst/cutelyst_global.h>
namespace Cutelyst {
class Application;
class Context;
class Controller;
class Dispatcher;
class ComponentPrivate;
/*! \class Component component.h Cutelyst/Component
* @brief The %Cutelyst %Component base class
*
* This is the base class of a Cutelyst component
*/
class CUTELYST_LIBRARY Component : public QObject
{
Q_OBJECT
Q_DECLARE_PRIVATE(Component)
Q_FLAGS(Modifiers)
public:
/** This value defines which kind of modifiers should be executed */
enum Modifier {
None = 0 << 1,
OnlyExecute = 1 << 1,
BeforeExecute = 2 << 1,
AroundExecute = 3 << 1,
AfterExecute = 4 << 1,
};
Q_ENUM(Modifier)
Q_DECLARE_FLAGS(Modifiers, Modifier)
/**
* This is the base class for many Cutelyst objects,
* prividing access to name and reverse for actions,
* and modifiers to customize execution.
*/
explicit Component(QObject *parent = nullptr);
virtual ~Component();
/**
* Reimplement to return custom Modifiers, default is None
*/
virtual Modifiers modifiers() const;
/**
* Returns the sub name of this Component.
*/
QString name() const;
/**
* Defines the sub name of this Component.
*/
void setName(const QString &name);
/**
* Returns the private name of this component.
*/
QString reverse() const;
/**
* Defines the private name of this Component.
*/
void setReverse(const QString &reverse);
/**
* A Does class is always attached to an action,
* if this method returns false the application
* will fail to start. Often useful if the user
* misconfigured the settings
*/
virtual bool init(Application *application, const QVariantHash &args);
/**
* Executes this component agains the Context
*/
bool execute(Context *c);
protected:
/*!
* A derived class using pimpl should call this constructor, to reduce the number of memory allocations
*/
explicit Component(ComponentPrivate *d, QObject *parent = nullptr);
/**
* Reimplement this if you want to do processing before doExecute
*/
virtual bool beforeExecute(Context *c);
/**
* Reimplement this if you want to do processing around doExecute,
* you must call doExecute yourself then
*/
virtual bool aroundExecute(Context *c, QStack<Component *> stack);
/**
* Reimplement this if you want to do processing after doExecute
*/
virtual bool afterExecute(Context *c);
/**
* Reimplement this for the main processing
*/
virtual bool doExecute(Context *c);
/**
* Call this to install before, around and after roles
*/
void applyRoles(const QStack<Component *> &roles);
/**
* Called by dispatcher once it's done preparing actions
*
* Subclasses might want to implement this to cache special
* actions, such as special methods for REST actions
*/
virtual bool dispatcherReady(const Dispatcher *dispatch, Controller *controller);
protected:
friend class Controller;
ComponentPrivate *d_ptr; //!< we cannot inherit from QObjectPrivate and therefore need our own d_ptr
};
}
#endif // CUTELYST_COMPONENT_H