summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qobject.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-09-06 09:14:36 -0700
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2021-09-14 19:25:54 +0000
commita8083ff47a646bc9bfdb17122be7088a6fbaf6a2 (patch)
tree43316317ada0e6c92b5e6200ffc06e8eb1d59214 /src/corelib/kernel/qobject.h
parent4f2a515f12e18e932de563ee2a5d7a10ae09487e (diff)
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>
Diffstat (limited to 'src/corelib/kernel/qobject.h')
-rw-r--r--src/corelib/kernel/qobject.h26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h
index f64a1a3b08..c94fb17755 100644
--- a/src/corelib/kernel/qobject.h
+++ b/src/corelib/kernel/qobject.h
@@ -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