QPermission: fight template bloat in data()

Extract the type-independent code into an out-of-line data_impl()
private method, leaving data() containing only the
QMetaType::fromType<T>() call as well as T copy- and
default-constructor calls.

As a drive-by, use categorized logging (consistency with the rest of
the permissions code) and printf-style qWarning() (expands to less
code).

Change-Id: Ie23b83cb3fc537c9cff15f853ceee2888bf63124
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit 66235481043d85e4f51a216cf2ad3bb895ea7842)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-01-12 09:42:34 +01:00 committed by Qt Cherry-pick Bot
parent b2f0931359
commit 0c7643bcd5
2 changed files with 21 additions and 6 deletions

View File

@ -257,6 +257,22 @@ Q_LOGGING_CATEGORY(lcPermissions, "qt.permissions", QtWarningMsg);
Returns the type of the permission.
*/
/*
\internal
*/
const void *QPermission::data(QMetaType requestedType) const
{
const auto actualType = type();
if (requestedType != actualType) {
qCWarning(lcPermissions, "Cannot convert from %s to %s",
actualType.name(), requestedType.name());
return nullptr;
}
return m_data.data();
}
#define QT_DEFINE_PERMISSION_SPECIAL_FUNCTIONS(ClassName) \
ClassName::ClassName() : d(new ClassName##Private) {} \
ClassName::ClassName(const ClassName &other) noexcept = default; \

View File

@ -61,13 +61,10 @@ public:
template <typename T, std::enable_if_t<is_permission<T>::value, bool> = true>
T data() const
{
auto requestedType = QMetaType::fromType<T>();
if (type() != requestedType) {
qWarning() << "Can not convert from" << type().name()
<< "to" << requestedType.name();
if (auto p = data(QMetaType::fromType<T>()))
return *static_cast<const T *>(p);
else
return T{};
}
return m_data.value<T>();
}
#endif
@ -76,6 +73,8 @@ public:
#endif
private:
Q_CORE_EXPORT const void *data(QMetaType id) const;
Qt::PermissionStatus m_status = Qt::PermissionStatus::Undetermined;
QVariant m_data;