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))
246 Controller::~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);
292 ControllerPrivate::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);
353 void 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;
421 Action *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<Cutelyst::Action *>(
object);
432 qCWarning(CUTELYST_CONTROLLER) <<
"ActionClass" << actionClass <<
"is not an ActionClass"
440 Action *ControllerPrivate::createAction(
const QVariantHash &args,
445 Action *action = actionClass(args);
451 for (
int i = 0; i < roles.
size(); ++i) {
453 code->
init(app, args);
459 action->
setName(args.value(QStringLiteral(
"name")).toString());
460 action->
setReverse(args.value(QStringLiteral(
"reverse")).toString());
466 void ControllerPrivate::registerActionMethods(
const QMetaObject *meta,
482 method.
parameterType(0) == qMetaTypeId<Cutelyst::Context *>())) {
488 if (name == classInfo.
name()) {
492 ParamsMultiMap attrs = parseAttributes(method, attributeArray, name);
511 actionList.append(action);
521 std::vector<std::pair<QString, QString>> attributes;
528 int size = str.
size();
535 if (str.
at(pos) ==
':') {
536 int keyStart = ++pos;
539 if (str.
at(pos) ==
'(') {
541 int valueStart = ++pos;
543 if (str.
at(pos) ==
')') {
546 if (++pos < size && str.
at(pos) ==
':') {
552 }
else if (pos >= size) {
566 }
else if (str.
at(pos) ==
':') {
578 if (!value.isEmpty()) {
579 if ((value.startsWith(u
'\'') && value.endsWith(u
'\'')) ||
580 (value.startsWith(u
'"') && value.endsWith(u
'"'))) {
582 value.remove(value.size() - 1, 1);
587 attributes.emplace_back(std::make_pair(key, value));
597 for (
const auto &pair : attributes) {
600 if (key.
compare(u
"Global") == 0) {
601 key = QStringLiteral(
"Path");
603 }
else if (key.
compare(u
"Local") == 0) {
604 key = QStringLiteral(
"Path");
606 }
else if (key.
compare(u
"Path") == 0) {
607 value = parsePathAttr(value);
608 }
else if (key.
compare(u
"Args") == 0) {
611 value = args.
remove(digitRE);
613 }
else if (key.
compare(u
"CaptureArgs") == 0) {
615 value = captureArgs.
remove(digitRE);
616 }
else if (key.
compare(u
"Chained") == 0) {
617 value = parseChainedAttr(value);
624 if (!ret.
contains(QStringLiteral(
"Args")) && !ret.
contains(QStringLiteral(
"CaptureArgs")) &&
625 (ret.
contains(QStringLiteral(
"AutoArgs")) ||
626 ret.
contains(QStringLiteral(
"AutoCaptureArgs")))) {
627 if (ret.
contains(QStringLiteral(
"AutoArgs")) &&
628 ret.
contains(QStringLiteral(
"AutoCaptureArgs"))) {
629 qFatal(
"Action '%s' has both AutoArgs and AutoCaptureArgs, which is not allowed",
633 if (ret.
contains(QStringLiteral(
"AutoArgs"))) {
634 ret.
remove(QStringLiteral(
"AutoArgs"));
635 parameterName = QStringLiteral(
"Args");
637 ret.
remove(QStringLiteral(
"AutoCaptureArgs"));
638 parameterName = QStringLiteral(
"CaptureArgs");
644 int parameterCount = 0;
668 auto doesIt = attributes.
constFind(QStringLiteral(
"Does"));
669 while (doesIt != attributes.constEnd() && doesIt.
key().compare(u
"Does") == 0) {
671 instantiateClass(doesIt.
value(), QByteArrayLiteral(
"Cutelyst::Component"));
673 roles.
push(qobject_cast<Component *>(
object));
683 if (value.startsWith(u
'/')) {
685 }
else if (!value.isEmpty()) {
686 ret = pathPrefix + u
'/' + value;
693 QString ret = QStringLiteral(
"/");
701 if (!pathPrefix.isEmpty()) {
702 ret.
append(pathPrefix + u
'/' + attr);
719 instanceName.
remove(nonWordsRE);
728 if (!
id.isValid() && !instanceName.
startsWith(u
"Cutelyst::")) {
737 if (!superIsClassName(metaObj->
superClass(), super)) {
738 qCWarning(CUTELYST_CONTROLLER)
739 <<
"Class name" << instanceName <<
"is not a derived class of" << super;
744 qCWarning(CUTELYST_CONTROLLER)
745 <<
"Could create a new instance of" << instanceName
746 <<
"make sure it's default constructor is "
747 "marked with the Q_INVOKABLE macro";
753 Component *component = application->createComponentPlugin(name);
758 component = application->createComponentPlugin(instanceName);
765 qCCritical(CUTELYST_CONTROLLER,
766 "Could not create component '%s', you can register it with "
767 "qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
768 qPrintable(instanceName),
769 qPrintable(instanceName));
782 return superIsClassName(super->
superClass(), className);
787 #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(T &&value)