6#include "application.h"
9#include "controller_p.h"
10#include "dispatcher.h"
12#include <QMetaClassInfo>
13#include <QRegularExpression>
242 , d_ptr(new ControllerPrivate(this))
246Controller::~Controller()
249 qDeleteAll(d->actionList);
256 return d->pathPrefix;
262 auto it = d->actions.constFind(name);
263 if (it != d->actions.constEnd()) {
266 return d->dispatcher->getAction(name.
toString(), d->pathPrefix);
272 return d->actionList;
277 return !qstrcmp(
metaObject()->className(), className);
292ControllerPrivate::ControllerPrivate(
Controller *parent)
303 dispatcher = _dispatcher;
311 q->setObjectName(className);
313 bool namespaceFound =
false;
317 while (pathPrefix.startsWith(u
'/')) {
318 pathPrefix.remove(0, 1);
320 namespaceFound =
true;
325 if (!namespaceFound) {
327 bool lastWasUpper =
true;
329 for (
int i = 0; i < className.
length(); ++i) {
330 const QChar c = className.
at(i);
333 lastWasUpper =
false;
334 }
else if (c == u
'_') {
347 pathPrefix = controlerNS;
350 registerActionMethods(meta, q, app);
353void ControllerPrivate::setupFinished()
357 const ActionList beginList = dispatcher->getActions(QStringLiteral(
"Begin"), pathPrefix);
362 beginAutoList.append(dispatcher->getActions(QStringLiteral(
"Auto"), pathPrefix));
364 const ActionList endList = dispatcher->getActions(QStringLiteral(
"End"), pathPrefix);
366 end = endList.
last();
369 const auto actions = actionList;
370 for (
Action *action : actions) {
371 action->dispatcherReady(dispatcher, q);
374 q->preFork(qobject_cast<Application *>(q->parent()));
383 int &actionRefCount = c->d_ptr->actionRefCount;
386 const auto beginAutoList = d->beginAutoList;
387 for (
Action *action : beginAutoList) {
388 if (actionRefCount) {
389 c->d_ptr->pendingAsync.enqueue(action);
390 }
else if (!action->dispatch(c)) {
398 if (actionRefCount) {
399 c->d_ptr->pendingAsync.enqueue(c->
action());
407 if (actionRefCount) {
408 c->d_ptr->pendingAsync.enqueue(d->end);
409 }
else if (!d->end->dispatch(c)) {
414 if (actionRefCount) {
415 c->d_ptr->engineRequest->status |= EngineRequest::Async;
421Action *ControllerPrivate::actionClass(
const QVariantHash &args)
423 const auto attributes = args.value(QStringLiteral(
"attributes")).value<
ParamsMultiMap>();
424 const QString actionClass = attributes.value(QStringLiteral(
"ActionClass"));
426 QObject *
object = instantiateClass(actionClass,
"Cutelyst::Action");
428 Action *action = qobject_cast<Action *>(
object);
432 qCWarning(CUTELYST_CONTROLLER) <<
"ActionClass" << actionClass <<
"is not an ActionClass";
439Action *ControllerPrivate::createAction(
const QVariantHash &args,
444 Action *action = actionClass(args);
450 for (
int i = 0; i < roles.
size(); ++i) {
452 code->
init(app, args);
458 action->
setName(args.value(QStringLiteral(
"name")).toString());
459 action->
setReverse(args.value(QStringLiteral(
"reverse")).toString());
465void ControllerPrivate::registerActionMethods(
const QMetaObject *meta,
481 method.
parameterType(0) == qMetaTypeId<Cutelyst::Context *>())) {
487 if (name == classInfo.
name()) {
491 ParamsMultiMap attrs = parseAttributes(method, attributeArray, name);
510 actionList.append(action);
520 std::vector<std::pair<QString, QString>> attributes;
527 int size = str.
size();
534 if (str.
at(pos) ==
':') {
535 int keyStart = ++pos;
538 if (str.
at(pos) ==
'(') {
540 int valueStart = ++pos;
542 if (str.
at(pos) ==
')') {
545 if (++pos < size && str.
at(pos) ==
':') {
551 }
else if (pos >= size) {
565 }
else if (str.
at(pos) ==
':') {
577 if (!value.isEmpty()) {
578 if ((value.startsWith(u
'\'') && value.endsWith(u
'\'')) ||
579 (value.startsWith(u
'"') && value.endsWith(u
'"'))) {
581 value.remove(value.size() - 1, 1);
586 attributes.emplace_back(std::make_pair(key, value));
596 for (
const auto &pair : attributes) {
599 if (key.
compare(u
"Global") == 0) {
600 key = QStringLiteral(
"Path");
602 }
else if (key.
compare(u
"Local") == 0) {
603 key = QStringLiteral(
"Path");
605 }
else if (key.
compare(u
"Path") == 0) {
606 value = parsePathAttr(value);
607 }
else if (key.
compare(u
"Args") == 0) {
610 value = args.
remove(digitRE);
612 }
else if (key.
compare(u
"CaptureArgs") == 0) {
614 value = captureArgs.
remove(digitRE);
615 }
else if (key.
compare(u
"Chained") == 0) {
616 value = parseChainedAttr(value);
623 if (!ret.
contains(QStringLiteral(
"Args")) && !ret.
contains(QStringLiteral(
"CaptureArgs")) &&
624 (ret.
contains(QStringLiteral(
"AutoArgs")) ||
625 ret.
contains(QStringLiteral(
"AutoCaptureArgs")))) {
626 if (ret.
contains(QStringLiteral(
"AutoArgs")) &&
627 ret.
contains(QStringLiteral(
"AutoCaptureArgs"))) {
628 qFatal(
"Action '%s' has both AutoArgs and AutoCaptureArgs, which is not allowed",
632 if (ret.
contains(QStringLiteral(
"AutoArgs"))) {
633 ret.
remove(QStringLiteral(
"AutoArgs"));
634 parameterName = QStringLiteral(
"Args");
636 ret.
remove(QStringLiteral(
"AutoCaptureArgs"));
637 parameterName = QStringLiteral(
"CaptureArgs");
643 int parameterCount = 0;
667 auto doesIt = attributes.
constFind(QStringLiteral(
"Does"));
668 while (doesIt != attributes.constEnd() && doesIt.
key().compare(u
"Does") == 0) {
670 instantiateClass(doesIt.
value(), QByteArrayLiteral(
"Cutelyst::Component"));
672 roles.
push(qobject_cast<Component *>(
object));
682 if (value.startsWith(u
'/')) {
684 }
else if (!value.isEmpty()) {
685 ret = pathPrefix + u
'/' + value;
692 QString ret = QStringLiteral(
"/");
700 if (!pathPrefix.isEmpty()) {
701 ret.
append(pathPrefix + u
'/' + attr);
726 if (!
id.isValid() && !instanceName.
startsWith(u
"Cutelyst::")) {
735 if (!superIsClassName(metaObj->
superClass(), super)) {
736 qCWarning(CUTELYST_CONTROLLER)
737 <<
"Class name" << instanceName <<
"is not a derived class of" << super;
742 qCWarning(CUTELYST_CONTROLLER)
743 <<
"Could create a new instance of" << instanceName
744 <<
"make sure it's default constructor is "
745 "marked with the Q_INVOKABLE macro";
751 Component *component = application->createComponentPlugin(name);
756 component = application->createComponentPlugin(instanceName);
763 qCCritical(CUTELYST_CONTROLLER,
764 "Could not create component '%s', you can register it with "
765 "qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
766 qPrintable(instanceName),
767 qPrintable(instanceName));
780 return superIsClassName(super->
superClass(), className);
785#include "moc_controller.cpp"
This class represents a Cutelyst Action.
void setupAction(const QVariantHash &args, Application *app)
bool dispatch(Context *c)
void setMethod(const QMetaMethod &method)
void setController(Controller *controller)
The Cutelyst application.
The Cutelyst Component base class.
void setReverse(const QString &reverse)
virtual bool init(Application *application, const QVariantHash &args)
void applyRoles(const QStack< Component * > &roles)
QString reverse() const noexcept
void setName(const QString &name)
Cutelyst Controller base class.
virtual bool preFork(Application *app)
virtual bool postFork(Application *app)
bool operator==(const char *className)
QString ns() const noexcept
ActionList actions() const noexcept
bool _DISPATCH(Context *c)
Controller(QObject *parent=nullptr)
Action * actionFor(QStringView name) const
The Cutelyst namespace holds all public Cutelyst API.
QByteArray & append(QByteArrayView data)
char at(qsizetype i) const const
const char * constData() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
qsizetype size() const const
bool isDigit(char32_t ucs4)
bool isLower(char32_t ucs4)
char32_t toLower(char32_t ucs4)
void append(QList::parameter_type value)
QList::const_reference at(qsizetype i) const const
bool isEmpty() const const
qsizetype size() const const
T value(qsizetype i) const const
QMultiMap::const_iterator constFind(const Key &key) const const
bool contains(const Key &key) const const
QMultiMap::iterator insert(QMultiMap::const_iterator pos, const Key &key, const T &value)
Key key(const T &value, const Key &defaultKey) const const
QMultiMap::size_type remove(const Key &key)
QMultiMap::iterator replace(const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const
void setParent(QObject *parent)
QString & append(QChar ch)
const QChar at(qsizetype position) const const
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QString first(qsizetype n) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
qsizetype length() const const
QString number(double n, char format, int precision)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toLatin1() const const
QString toString() const const
QVariant fromValue(const T &value)