1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

FunctionNotificationCenterObserver: Move to ObjCHelpers header

This type will be used to monitor UMP endpoint changes in an upcoming
commit.
This commit is contained in:
reuk 2025-03-05 20:29:10 +00:00
parent 1f4cc7bbb1
commit 51e5820cd3
No known key found for this signature in database
2 changed files with 57 additions and 63 deletions

View file

@ -585,6 +585,63 @@ private:
Class klass = nullptr;
};
//==============================================================================
/*
Forwards NSNotificationCenter callbacks to a std::function<void()>.
*/
class FunctionNotificationCenterObserver
{
public:
FunctionNotificationCenterObserver (NSNotificationName notificationName,
id objectToObserve,
std::function<void()> callback)
: onNotification (std::move (callback)),
observer (observerObject.get(), getSelector(), notificationName, objectToObserve)
{}
private:
static SEL getSelector()
{
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
return @selector (notificationFired:);
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
}
std::function<void()> onNotification;
NSUniquePtr<NSObject> observerObject
{
[this]
{
static auto observerClass = std::invoke ([]
{
ObjCClass<NSObject> k { "JUCEObserverClass_" };
k.addIvar<FunctionNotificationCenterObserver*> ("owner");
k.addMethod (getSelector(), [] (id self, SEL, NSNotification*)
{
getIvar<FunctionNotificationCenterObserver*> (self, "owner")->onNotification();
});
k.registerClass();
return k;
});
auto* result = observerClass.createInstance();
object_setInstanceVariable (result, "owner", this);
return result;
}()
};
ScopedNotificationCenterObserver observer;
// Instances can't be copied or moved, because 'this' is stored as a member of the ObserverClass
// object.
JUCE_DECLARE_NON_COPYABLE (FunctionNotificationCenterObserver)
JUCE_DECLARE_NON_MOVEABLE (FunctionNotificationCenterObserver)
};
#if JUCE_IOS
// Defines a function that will check the requested version both at

View file

@ -35,69 +35,6 @@
namespace juce
{
//==============================================================================
/*
Forwards NSNotificationCenter callbacks to a std::function<void()>.
*/
class FunctionNotificationCenterObserver
{
public:
FunctionNotificationCenterObserver (NSNotificationName notificationName,
id objectToObserve,
std::function<void()> callback)
: onNotification (std::move (callback)),
observer (observerObject.get(), getSelector(), notificationName, objectToObserve)
{}
private:
struct ObserverClass
{
ObserverClass()
{
klass.addIvar<FunctionNotificationCenterObserver*> ("owner");
klass.addMethod (getSelector(), [] (id self, SEL, NSNotification*)
{
getIvar<FunctionNotificationCenterObserver*> (self, "owner")->onNotification();
});
klass.registerClass();
}
NSObject* createInstance() const { return klass.createInstance(); }
private:
ObjCClass<NSObject> klass { "JUCEObserverClass_" };
};
static SEL getSelector()
{
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
return @selector (notificationFired:);
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
}
std::function<void()> onNotification;
NSUniquePtr<NSObject> observerObject
{
[this]
{
static ObserverClass observerClass;
auto* result = observerClass.createInstance();
object_setInstanceVariable (result, "owner", this);
return result;
}()
};
ScopedNotificationCenterObserver observer;
// Instances can't be copied or moved, because 'this' is stored as a member of the ObserverClass
// object.
JUCE_DECLARE_NON_COPYABLE (FunctionNotificationCenterObserver)
JUCE_DECLARE_NON_MOVEABLE (FunctionNotificationCenterObserver)
};
//==============================================================================
/*
Manages the lifetime of a CVDisplayLinkRef for a single display, and automatically starts and