18 #include "controller_p.h" 20 #include "application.h" 21 #include "dispatcher.h" 25 #include "context_p.h" 27 #include <QMetaClassInfo> 28 #include <QRegularExpression> 33 , d_ptr(new ControllerPrivate(this))
37 Controller::~Controller()
40 qDeleteAll(d->actionList);
53 Action *ret = d->actions.value(name);
57 return d->dispatcher->getAction(name, d->pathPrefix);
68 return !qstrcmp(
metaObject()->className(), className);
83 ControllerPrivate::ControllerPrivate(
Controller *parent) :
92 dispatcher = _dispatcher;
100 q->setObjectName(className);
102 bool namespaceFound =
false;
109 namespaceFound =
true;
114 if (!namespaceFound) {
116 bool lastWasUpper =
true;
118 for (
int i = 0; i < className.
length(); ++i) {
119 const QChar c = className.
at(i);
122 lastWasUpper =
false;
134 pathPrefix = controlerNS;
137 registerActionMethods(meta, q, app);
140 void ControllerPrivate::setupFinished()
144 const ActionList beginList = dispatcher->getActions(QStringLiteral(
"Begin"), pathPrefix);
149 beginAutoList.append(dispatcher->getActions(QStringLiteral(
"Auto"), pathPrefix));
151 const ActionList endList = dispatcher->getActions(QStringLiteral(
"End"), pathPrefix);
153 end = endList.
last();
156 const auto actions = actionList;
157 for (
Action *action : actions) {
158 action->dispatcherReady(dispatcher, q);
161 q->preFork(qobject_cast<Application *>(q->parent()));
170 int &asyncDetached = c->d_ptr->asyncDetached;
173 const auto beginAutoList = d->beginAutoList;
174 for (
Action *action : beginAutoList) {
176 c->d_ptr->pendingAsync.append(action);
177 }
else if (!action->dispatch(c)) {
186 c->d_ptr->pendingAsync.append(c->action());
187 }
else if (!c->action()->
dispatch(c)) {
195 c->d_ptr->pendingAsync.append(d->end);
196 }
else if (!d->end->dispatch(c)) {
204 Action *ControllerPrivate::actionClass(
const QVariantHash &args)
207 const QString actionClass = attributes.value(QStringLiteral(
"ActionClass"));
209 QObject *
object = instantiateClass(actionClass,
"Cutelyst::Action");
215 qCWarning(CUTELYST_CONTROLLER) <<
"ActionClass" 217 <<
"is not an ActionClass";
226 Action *action = actionClass(args);
232 for (
int i = 0; i < roles.
size(); ++i) {
234 code->
init(app, args);
240 action->
setName(args.value(QStringLiteral(
"name")).toString());
241 action->
setReverse(args.value(QStringLiteral(
"reverse")).toString());
265 if (name == classInfo.
name()) {
278 Action *action = createAction({
288 actions.insertMulti(action->
reverse(), action);
289 actionList.append(action);
297 std::vector<std::pair<QString, QString> > attributes;
304 int size = str.
size();
311 if (str.
at(pos) ==
':') {
312 int keyStart = ++pos;
315 if (str.
at(pos) ==
'(') {
317 int valueStart = ++pos;
319 if (str.
at(pos) ==
')') {
322 if (++pos < size && str.
at(pos) ==
':') {
327 }
else if (pos >= size) {
340 }
else if (str.
at(pos) ==
':') {
369 auto i = attributes.crbegin();
370 const auto end = attributes.crend();
375 key = QStringLiteral(
"Path");
378 key = QStringLiteral(
"Path");
381 value = parsePathAttr(value);
391 value = parseChainedAttr(value);
399 if (!ret.
contains(QStringLiteral(
"Args")) && !ret.
contains(QStringLiteral(
"CaptureArgs")) &&
400 (ret.
contains(QStringLiteral(
"AutoArgs")) || ret.
contains(QStringLiteral(
"AutoCaptureArgs")))) {
401 if (ret.
contains(QStringLiteral(
"AutoArgs")) && ret.
contains(QStringLiteral(
"AutoCaptureArgs"))) {
402 qFatal(
"Action '%s' has both AutoArgs and AutoCaptureArgs, which is not allowed", name.
constData());
405 if (ret.
contains(QStringLiteral(
"AutoArgs"))) {
406 ret.
remove(QStringLiteral(
"AutoArgs"));
407 parameterName = QStringLiteral(
"Args");
409 ret.
remove(QStringLiteral(
"AutoCaptureArgs"));
410 parameterName = QStringLiteral(
"CaptureArgs");
415 int parameterCount = 0;
418 if (typeId == QMetaType::QString) {
429 if (!ret.
contains(QStringLiteral(
"Private")) && method.
access() == QMetaMethod::Private) {
440 auto doesIt = attributes.
constFind(QStringLiteral(
"Does"));
441 while (doesIt != attributes.constEnd() && doesIt.
key() ==
QLatin1String(
"Does")) {
442 QObject *
object = instantiateClass(doesIt.
value(), QByteArrayLiteral(
"Cutelyst::Component"));
444 roles.
push(qobject_cast<Component *>(
object));
464 QString ret = QStringLiteral(
"/");
469 if (attr == QStringLiteral(
".")) {
472 if (!pathPrefix.isEmpty()) {
498 if (!
id && !instanceName.
startsWith(QStringLiteral(
"Cutelyst::"))) {
507 if (!superIsClassName(metaObj->
superClass(), super)) {
508 qCWarning(CUTELYST_CONTROLLER)
511 <<
"is not a derived class of" 517 qCWarning(CUTELYST_CONTROLLER)
518 <<
"Could create a new instance of" 520 <<
"make sure it's default constructor is " 521 "marked with the Q_INVOKABLE macro";
527 Component *component = application->createComponentPlugin(name);
532 component = application->createComponentPlugin(instanceName);
539 qFatal(
"Could not create component '%s', you can register it with qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
540 qPrintable(instanceName), qPrintable(instanceName));
552 return superIsClassName(super->
superClass(), className);
557 #include "moc_controller.cpp" void setName(const QString &name)
QString & append(QChar ch)
bool dispatch(Context *c)
bool contains(const Key &key) const
void append(const T &value)
void setReverse(const QString &reverse)
virtual bool preFork(Application *app)
const_iterator constFind(const Key &key) const
Action * actionFor(const QString &name) const
QString & remove(int position, int n)
The Cutelyst Component base class.
This class represents a Cutelyst Action.
QString number(int n, int base)
Cutelyst Controller base class
iterator insertMulti(const Key &key, const T &value)
const char * constData() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
virtual bool init(Application *application, const QVariantHash &args)
bool _DISPATCH(Context *c)
The Cutelyst namespace holds all public Cutelyst API.
void applyRoles(const QStack< Component *> &roles)
QByteArray mid(int pos, int len) const
QByteArray & append(char ch)
void setParent(QObject *parent)
QVariant fromValue(const T &value)
void setController(Controller *controller)
virtual bool postFork(Application *app)
const Key key(const T &value, const Key &defaultKey) const
const T & at(int i) const
QByteArray toLatin1() const
void setupAction(const QVariantHash &args, Application *app)
const QChar at(int position) const
QString fromLatin1(const char *str, int size)
bool operator==(const char *className)
The Cutelyst Application.
void setMethod(const QMetaMethod &method)
iterator insert(const Key &key, const T &value)
ActionList actions() const
const T value(const Key &key, const T &defaultValue) const
int remove(const Key &key)
Controller(QObject *parent=nullptr)