diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2010-09-11 08:03:08 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2010-09-11 08:04:41 (GMT) |
commit | b320acf4889b4d36f6040dd454dbb27caab3a057 (patch) | |
tree | b8f6bd36c8490a2af210372c64b9a178a14a2703 /src/corelib/kernel/qmetaobject.cpp | |
parent | 31cc0b1820c0c8fdfdbc3d5b804f2dba2051c96f (diff) | |
download | Qt-b320acf4889b4d36f6040dd454dbb27caab3a057.zip Qt-b320acf4889b4d36f6040dd454dbb27caab3a057.tar.gz Qt-b320acf4889b4d36f6040dd454dbb27caab3a057.tar.bz2 |
Properly implement qobject_cast for const pointers.
Instead of using ugly const_cast in public headers and Q_UNUSED
(which expands to nothing in release builds), use a properly-const
method.
Reviewed-By: Bradley T. Hughes
(cherry picked from commit 0b0aa7603b27447a5abe55c55d0514bc56b35f9e)
Diffstat (limited to 'src/corelib/kernel/qmetaobject.cpp')
-rw-r--r-- | src/corelib/kernel/qmetaobject.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index a70772f..ceb2a9c 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -266,7 +266,25 @@ QObject *QMetaObject::cast(QObject *obj) const const QMetaObject *m = obj->metaObject(); do { if (m == this) - return const_cast<QObject*>(obj); + return obj; + } while ((m = m->d.superdata)); + } + return 0; +} + +/*! + \internal + + Returns \a obj if object \a obj inherits from this + meta-object; otherwise returns 0. +*/ +const QObject *QMetaObject::cast(const QObject *obj) const +{ + if (obj) { + const QMetaObject *m = obj->metaObject(); + do { + if (m == this) + return obj; } while ((m = m->d.superdata)); } return 0; |