From b2883a6acc7a8d8372a815cc91dd1a8449f25723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 10 Nov 2014 11:17:28 +0100 Subject: Add QDebug support for NSObject and a few selected CoreFoundation types Lets us print out the interface name in addition to the address when doing: qDebug() << myNSObject; Unfortunately, CFTypeRef is just a typedef to void*, so we can't add a generic overload for CFTypeRef. For now, we provide operators for commonly used Core Foundation and Core Graphics types. Additional types may be added using Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE. Change-Id: I27b12ef9e62cb1be348b9771cb31657692903f6c Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcore_mac_objc.mm | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/corelib/kernel') diff --git a/src/corelib/kernel/qcore_mac_objc.mm b/src/corelib/kernel/qcore_mac_objc.mm index 8ac062a154..265126dc58 100644 --- a/src/corelib/kernel/qcore_mac_objc.mm +++ b/src/corelib/kernel/qcore_mac_objc.mm @@ -42,6 +42,8 @@ #include +#include + #ifdef Q_OS_IOS #import #endif @@ -61,6 +63,38 @@ QString QCFString::toQString(const NSString *nsstr) return toQString(reinterpret_cast(nsstr)); } +// ------------------------------------------------------------------------- + +QDebug operator<<(QDebug dbg, const NSObject *nsObject) +{ + return dbg << (nsObject ? nsObject.description.UTF8String : "NSObject(0x0)"); +} + +QDebug operator<<(QDebug dbg, CFStringRef stringRef) +{ + if (!stringRef) + return dbg << "CFStringRef(0x0)"; + + if (const UniChar *chars = CFStringGetCharactersPtr(stringRef)) + dbg << QString::fromRawData(reinterpret_cast(chars), CFStringGetLength(stringRef)); + else + dbg << QString::fromCFString(stringRef); + + return dbg; +} + +// Prevents breaking the ODR in case we introduce support for more types +// later on, and lets the user override our default QDebug operators. +#define QT_DECLARE_WEAK_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \ + __attribute__((weak)) Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) + +QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_DECLARE_WEAK_QDEBUG_OPERATOR_FOR_CF_TYPE); +QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_DECLARE_WEAK_QDEBUG_OPERATOR_FOR_CF_TYPE); +QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_DECLARE_WEAK_QDEBUG_OPERATOR_FOR_CF_TYPE); +QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_DECLARE_WEAK_QDEBUG_OPERATOR_FOR_CF_TYPE); + +// ------------------------------------------------------------------------- + QAppleOperatingSystemVersion qt_apple_os_version() { QAppleOperatingSystemVersion v = {0, 0, 0}; -- cgit v1.2.3