diff options
author | Jake Petroules <jake.petroules@qt.io> | 2016-06-21 11:44:54 -0700 |
---|---|---|
committer | Jake Petroules <jake.petroules@qt.io> | 2017-04-01 15:19:02 +0000 |
commit | 384f6ca96a0a969c910f9ea1f556aa4235354244 (patch) | |
tree | 9f8699be6d5f84ce596d943047a8447b389810ad /src | |
parent | d436e0e6f5da5daf7f017ac4fa8ab0d9564f0859 (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/kernel/qcore_mac_p.h | 44 |
1 files changed, 25 insertions, 19 deletions
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 <typename T, typename U, U (*RetainFunction)(U), void (*ReleaseFunction)(U)> +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 <typename T> -class Q_CORE_EXPORT QCFType +class QCFType : public QAppleRefCounted<T, CFTypeRef, CFRetain, CFRelease> { 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 <typename X> X as() const { return reinterpret_cast<X>(type); } + using QAppleRefCounted<T, CFTypeRef, CFRetain, CFRelease>::QAppleRefCounted; + template <typename X> X as() const { return reinterpret_cast<X>(this->type); } static QCFType constructFromGet(const T &t) { CFRetain(t); return QCFType<T>(t); } -protected: - T type; }; class Q_CORE_EXPORT QCFString : public QCFType<CFStringRef> |