Q_DECLARE_INTERFACE: rework to use more inline functions

That way, we can add the NOLINTNEXTLINE comment to suppress clang-tidy,
which otherwise flags all usage of const_cast in C++ code.

Change-Id: Ie72b0dd0fbe84d2caae0fffd16a247b96d223772
Reviewed-by: Rui Oliveira
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Thiago Macieira 2021-09-06 09:14:36 -07:00 committed by Volker Hilsheimer
parent 4f2a515f12
commit a8083ff47a

View File

@ -482,14 +482,17 @@ inline T qobject_cast(const QObject *object)
template <class T> constexpr const char * qobject_interface_iid() = delete;
inline const QBindingStorage *qGetBindingStorage(const QObject *o)
template <class T> inline T *
qobject_iid_cast(QObject *object, const char *IId = qobject_interface_iid<T *>())
{
return o->bindingStorage();
return reinterpret_cast<T *>((object ? object->qt_metacast(IId) : nullptr));
}
inline QBindingStorage *qGetBindingStorage(QObject *o)
template <class T> inline std::enable_if_t<std::is_const<T>::value, T *>
qobject_iid_cast(const QObject *object)
{
return o->bindingStorage();
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
QObject *o = const_cast<QObject *>(object);
return qobject_iid_cast<std::remove_cv_t<T>>(o);
}
#if defined(Q_CLANG_QDOC)
@ -499,11 +502,20 @@ inline QBindingStorage *qGetBindingStorage(QObject *o)
template <> constexpr const char *qobject_interface_iid<IFace *>() \
{ return IId; } \
template <> inline IFace *qobject_cast<IFace *>(QObject *object) \
{ return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : nullptr)); } \
{ return qobject_iid_cast<IFace>(object); } \
template <> inline const IFace *qobject_cast<const IFace *>(const QObject *object) \
{ return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : nullptr)); }
{ return qobject_iid_cast<const IFace>(object); }
#endif // Q_MOC_RUN
inline const QBindingStorage *qGetBindingStorage(const QObject *o)
{
return o->bindingStorage();
}
inline QBindingStorage *qGetBindingStorage(QObject *o)
{
return o->bindingStorage();
}
#ifndef QT_NO_DEBUG_STREAM
Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *);
#endif