Add QDebug operator for Objective-C id type
Not every id is an NSObject, and even those that are are sometimes passed around as id<SomeProtocol>, in which case we still want them to have pretty debug descriptions. Change-Id: I50a02cd8fb9d6a2cbde3c4aef81116033d10b72c Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
3de6cd9bd1
commit
e47c22480f
@ -530,6 +530,7 @@ inline QDebug operator<<(QDebug debug, QKeyCombination combination)
|
||||
|
||||
QT_END_NAMESPACE
|
||||
Q_FORWARD_DECLARE_CF_TYPE(CFString);
|
||||
typedef struct objc_object *id;
|
||||
Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
|
||||
QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_CF_TYPE)
|
||||
QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_MUTABLE_CF_TYPE)
|
||||
@ -554,6 +555,7 @@ QT_BEGIN_NAMESPACE
|
||||
}
|
||||
|
||||
// Defined in qcore_mac_objc.mm
|
||||
Q_CORE_EXPORT QDebug operator<<(QDebug, id obj);
|
||||
Q_CORE_EXPORT QDebug operator<<(QDebug, const NSObject *);
|
||||
Q_CORE_EXPORT QDebug operator<<(QDebug, CFStringRef);
|
||||
|
||||
|
@ -196,6 +196,27 @@ os_log_t AppleUnifiedLogger::cachedLog(const QString &subsystem, const QString &
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
QDebug operator<<(QDebug dbg, id obj)
|
||||
{
|
||||
if (!obj) {
|
||||
// Match NSLog
|
||||
dbg << "(null)";
|
||||
return dbg;
|
||||
}
|
||||
|
||||
for (Class cls = object_getClass(obj); cls; cls = class_getSuperclass(cls)) {
|
||||
if (cls == NSObject.class) {
|
||||
dbg << static_cast<NSObject*>(obj);
|
||||
return dbg;
|
||||
}
|
||||
}
|
||||
|
||||
// Match NSObject.debugDescription
|
||||
const QDebugStateSaver saver(dbg);
|
||||
dbg.nospace() << '<' << object_getClassName(obj) << ": " << static_cast<void*>(obj) << '>';
|
||||
return dbg;
|
||||
}
|
||||
|
||||
QDebug operator<<(QDebug dbg, const NSObject *nsObject)
|
||||
{
|
||||
return dbg << (nsObject ?
|
||||
|
Loading…
x
Reference in New Issue
Block a user