From 384f6ca96a0a969c910f9ea1f556aa4235354244 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Tue, 21 Jun 2016 11:44:54 -0700 Subject: Add QAppleRefCounted template as the base of QCFType This helps to reduce code duplication as it will be used as the basis of other reference-counted RAII template classes in the future. Change-Id: I6e7e32373ec738f68d960afc759213610232b9bf Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcore_mac_p.h | 44 +++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/corelib/kernel/qcore_mac_p.h b/src/corelib/kernel/qcore_mac_p.h index d0edef33a2..881212a224 100644 --- a/src/corelib/kernel/qcore_mac_p.h +++ b/src/corelib/kernel/qcore_mac_p.h @@ -76,6 +76,28 @@ #endif QT_BEGIN_NAMESPACE +template +class QAppleRefCounted +{ +public: + QAppleRefCounted(const T &t = T()) : type(t) {} + QAppleRefCounted(const QAppleRefCounted &helper) : type(helper.type) { if (type) RetainFunction(type); } + ~QAppleRefCounted() { if (type) ReleaseFunction(type); } + operator T() { return type; } + QAppleRefCounted &operator=(const QAppleRefCounted &helper) + { + if (helper.type) + RetainFunction(helper.type); + T type2 = type; + type = helper.type; + if (type2) + ReleaseFunction(type2); + return *this; + } + T *operator&() { return &type; } +protected: + T type; +}; /* Helper class that automates refernce counting for CFtypes. @@ -90,32 +112,16 @@ QT_BEGIN_NAMESPACE HIThemeGet*Shape functions, which in reality are "Copy" functions. */ template -class Q_CORE_EXPORT QCFType +class QCFType : public QAppleRefCounted { public: - inline QCFType(const T &t = 0) : type(t) {} - inline QCFType(const QCFType &helper) : type(helper.type) { if (type) CFRetain(type); } - inline ~QCFType() { if (type) CFRelease(type); } - inline operator T() { return type; } - inline QCFType operator =(const QCFType &helper) - { - if (helper.type) - CFRetain(helper.type); - CFTypeRef type2 = type; - type = helper.type; - if (type2) - CFRelease(type2); - return *this; - } - inline T *operator&() { return &type; } - template X as() const { return reinterpret_cast(type); } + using QAppleRefCounted::QAppleRefCounted; + template X as() const { return reinterpret_cast(this->type); } static QCFType constructFromGet(const T &t) { CFRetain(t); return QCFType(t); } -protected: - T type; }; class Q_CORE_EXPORT QCFString : public QCFType -- cgit v1.2.3