QObject: port setObjectName() to QAnyStringView

... while keeping the QString overload for users that pass actual
QStrings (e.g. QStringLiteral).

However, QString and QAnyStringView cannot be overloaded (they're
ambiguous for most arguments), so we need to make one of them a
Q_WEAK_OVERLOAD.

Normally, we'd make the new function weak and keep the old function
as-is, but, here, that would be beside the point, because all callers
would continue to resolve to the QString overload, and nothing would
call the QAnyStringView one.

So we really want the old function to be the Q_WEAK_OVERLOAD, so that
the QString overload is only selected when actual QStrings are
passed. That means we need to leave the old function in a compat build
(compiled in, but not visible in the public header). Since
Q_WEAK_OVERLOADs cannot be (easily) exported (they're templates), make
it call a private function (which can be, and is, exported).

Reviewers may questions whether one can overload

   setObjectName() and
   template <typename = void> setObjectName()

The answer is that we can, because templates mangle differently from
normal functions. We can even call the template function (with
explicit template arguments), as seen in removed_api.cpp.

This adapts the interface of the function to how most users use it:
They pass QLatin1String or just const char[]. Only very few passed
QStringLiteral, which, ignoring that fact that it produces a ton of
code for the temporary QString's destructor that's never executed,
would have been, and continues to be, the optimal way of passing data,
modulo plugin bugs (QTBUG-51602, QTBUG-49061).

[ChangeLog][QtCore][QObject] Added setObjectName() overload taking
QAnyStringView.

Fixes: QTBUG-101393
Change-Id: I1243545614754b4978d4c8641460980b6b293c1a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2020-05-20 22:33:11 +02:00 committed by Marc Mutz
parent 6313a1adf5
commit 9da4c6bfb7
3 changed files with 37 additions and 2 deletions

View File

@ -205,6 +205,15 @@ QCalendar::QCalendar(QStringView name)
QCalendar::QCalendar(QLatin1String name)
: QCalendar(QAnyStringView{name}) {}
#include "qobject.h"
void QObject::setObjectName(const QString &name)
{
setObjectName<void>(name);
}
#include "qversionnumber.h"
QT_WARNING_PUSH
@ -229,5 +238,6 @@ QT_WARNING_POP
// #include <qotherheader.h>
// // implement removed functions from qotherheader.h
// order sections alphabetically to reduce chances of merge conflicts
#endif // QT_CORE_REMOVED_SINCE(6, 4)

View File

@ -1265,10 +1265,11 @@ QString QObject::objectName() const
return d->extraData ? d->extraData->objectName : QString();
}
/*
/*!
\fn void QObject::setObjectName(const QString &name)
Sets the object's name to \a name.
*/
void QObject::setObjectName(const QString &name)
void QObject::doSetObjectName(const QString &name)
{
Q_D(QObject);
@ -1282,6 +1283,24 @@ void QObject::setObjectName(const QString &name)
}
}
/*!
\overload
\since 6.4
*/
void QObject::setObjectName(QAnyStringView name)
{
Q_D(QObject);
d->ensureExtraData();
d->extraData->objectName.removeBindingUnlessInWrapper();
if (d->extraData->objectName.value() != name) {
d->extraData->objectName.setValueBypassingBindings(name.toString());
d->extraData->objectName.notify(); // also emits a signal
}
}
QBindable<QString> QObject::bindableObjectName()
{
Q_D(QObject);

View File

@ -145,7 +145,12 @@ public:
#endif // QT_NO_TRANSLATION
QString objectName() const;
#if QT_CORE_REMOVED_SINCE(6, 4)
void setObjectName(const QString &name);
#endif
Q_WEAK_OVERLOAD
void setObjectName(const QString &name) { doSetObjectName(name); }
void setObjectName(QAnyStringView name);
QBindable<QString> bindableObjectName();
inline bool isWidgetType() const { return d_ptr->isWidget; }
@ -449,6 +454,7 @@ protected:
friend class QThreadData;
private:
void doSetObjectName(const QString &name);
Q_DISABLE_COPY(QObject)
Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *))