summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2020-05-13 16:40:19 +0200
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2020-06-05 00:03:51 +0200
commitdf2e029a06f065c82dacc28ec885b4307260b6fb (patch)
tree7c1c8f334c9036c0b5438c944fce1b205b962293 /src/corelib
parentc7d80486dee44c096ba993cbda8f69612441a264 (diff)
macOS: Clean up headers
The headers are now C++ clean and can be used outside of Objective-C code. All includes of Objective-C frameworks have been moved to the implementation files. Header guards have been added in the few places they were missing. All includes are now done via #include, instead of sometimes using the #import variant. Change-Id: Ibb0a9c0bcfefbda4347737212e40e300a3184982 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/kernel/qcore_mac.mm14
-rw-r--r--src/corelib/kernel/qcore_mac_p.h53
2 files changed, 48 insertions, 19 deletions
diff --git a/src/corelib/kernel/qcore_mac.mm b/src/corelib/kernel/qcore_mac.mm
index 9ab91e465f..c1ba9a7b9e 100644
--- a/src/corelib/kernel/qcore_mac.mm
+++ b/src/corelib/kernel/qcore_mac.mm
@@ -639,6 +639,20 @@ Q_CONSTRUCTOR_FUNCTION(qt_apple_check_os_version);
// -------------------------------------------------------------------------
+void QMacNotificationObserver::remove()
+{
+ if (observer)
+ [[NSNotificationCenter defaultCenter] removeObserver:observer];
+ observer = nullptr;
+}
+
+// -------------------------------------------------------------------------
+
+QMacKeyValueObserver::QMacKeyValueObserver(const QMacKeyValueObserver &other)
+ : QMacKeyValueObserver(other.object, other.keyPath, *other.callback.get())
+{
+}
+
void QMacKeyValueObserver::addObserver(NSKeyValueObservingOptions options)
{
[object addObserver:observer forKeyPath:keyPath options:options context:callback.get()];
diff --git a/src/corelib/kernel/qcore_mac_p.h b/src/corelib/kernel/qcore_mac_p.h
index 535d3579b2..a21ce5e65a 100644
--- a/src/corelib/kernel/qcore_mac_p.h
+++ b/src/corelib/kernel/qcore_mac_p.h
@@ -81,6 +81,24 @@
#define QT_MAC_WEAK_IMPORT(symbol) extern "C" decltype(symbol) symbol __attribute__((weak_import));
+#if defined(__OBJC__)
+#define QT_DECLARE_NAMESPACED_OBJC_INTERFACE(classname, definition) \
+ @interface QT_MANGLE_NAMESPACE(classname) : \
+ definition \
+ @end \
+ QT_NAMESPACE_ALIAS_OBJC_CLASS(classname);
+#else
+#define QT_DECLARE_NAMESPACED_OBJC_INTERFACE(classname, definition) \
+ Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(classname)); \
+ using classname = QT_MANGLE_NAMESPACE(classname);
+#endif
+
+#define QT_FORWARD_DECLARE_OBJC_ENUM(name, type) \
+ typedef type name;
+
+Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
+Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
+
QT_BEGIN_NAMESPACE
template <typename T, typename U, U (*RetainFunction)(U), void (*ReleaseFunction)(U)>
class QAppleRefCounted
@@ -307,24 +325,25 @@ QT_MAC_WEAK_IMPORT(_os_activity_current);
// -------------------------------------------------------------------------
-#if defined( __OBJC__)
-class QMacNotificationObserver
+class Q_CORE_EXPORT QMacNotificationObserver
{
public:
QMacNotificationObserver() {}
+#if defined( __OBJC__)
template<typename Functor>
- QMacNotificationObserver(id object, NSNotificationName name, Functor callback) {
+ QMacNotificationObserver(NSObject *object, NSNotificationName name, Functor callback) {
observer = [[NSNotificationCenter defaultCenter] addObserverForName:name
object:object queue:nil usingBlock:^(NSNotification *) {
callback();
}
];
}
+#endif
QMacNotificationObserver(const QMacNotificationObserver& other) = delete;
QMacNotificationObserver(QMacNotificationObserver&& other) : observer(other.observer) {
- other.observer = nil;
+ other.observer = nullptr;
}
QMacNotificationObserver &operator=(const QMacNotificationObserver& other) = delete;
@@ -332,26 +351,20 @@ public:
if (this != &other) {
remove();
observer = other.observer;
- other.observer = nil;
+ other.observer = nullptr;
}
return *this;
}
- void remove() {
- if (observer)
- [[NSNotificationCenter defaultCenter] removeObserver:observer];
- observer = nil;
- }
+ void remove();
~QMacNotificationObserver() { remove(); }
private:
- id observer = nil;
+ NSObject *observer = nullptr;
};
QT_END_NAMESPACE
-@interface QT_MANGLE_NAMESPACE(KeyValueObserver) : NSObject
-@end
-QT_NAMESPACE_ALIAS_OBJC_CLASS(KeyValueObserver);
+QT_DECLARE_NAMESPACED_OBJC_INTERFACE(KeyValueObserver, NSObject)
QT_BEGIN_NAMESPACE
class Q_CORE_EXPORT QMacKeyValueObserver
@@ -361,16 +374,17 @@ public:
QMacKeyValueObserver() {}
+#if defined( __OBJC__)
// Note: QMacKeyValueObserver must not outlive the object observed!
- QMacKeyValueObserver(id object, NSString *keyPath, Callback callback,
+ QMacKeyValueObserver(NSObject *object, NSString *keyPath, Callback callback,
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew)
: object(object), keyPath(keyPath), callback(new Callback(callback))
{
addObserver(options);
}
+#endif
- QMacKeyValueObserver(const QMacKeyValueObserver &other)
- : QMacKeyValueObserver(other.object, other.keyPath, *other.callback.get()) {}
+ QMacKeyValueObserver(const QMacKeyValueObserver &other);
QMacKeyValueObserver(QMacKeyValueObserver &&other) { swap(other, *this); }
@@ -397,15 +411,16 @@ private:
std::swap(first.callback, second.callback);
}
+#if defined( __OBJC__)
void addObserver(NSKeyValueObservingOptions options);
+#endif
- id object = nil;
+ NSObject *object = nullptr;
NSString *keyPath = nullptr;
std::unique_ptr<Callback> callback;
static KeyValueObserver *observer;
};
-#endif
// -------------------------------------------------------------------------