Path: blob/master/thirdparty/metal-cpp/Foundation/NSNotification.hpp
21066 views
//-------------------------------------------------------------------------------------------------------------------------------------------------------------1//2// Foundation/NSNotification.hpp3//4// Copyright 2020-2024 Apple Inc.5//6// Licensed under the Apache License, Version 2.0 (the "License");7// you may not use this file except in compliance with the License.8// You may obtain a copy of the License at9//10// http://www.apache.org/licenses/LICENSE-2.011//12// Unless required by applicable law or agreed to in writing, software13// distributed under the License is distributed on an "AS IS" BASIS,14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15// See the License for the specific language governing permissions and16// limitations under the License.17//18//-------------------------------------------------------------------------------------------------------------------------------------------------------------1920#pragma once2122//-------------------------------------------------------------------------------------------------------------------------------------------------------------2324#include "NSDefines.hpp"25#include "NSDictionary.hpp"26#include "NSObject.hpp"27#include "NSString.hpp"28#include "NSTypes.hpp"29#include <functional>3031//-------------------------------------------------------------------------------------------------------------------------------------------------------------3233namespace NS34{35using NotificationName = class String*;3637class Notification : public NS::Referencing<Notification>38{39public:40NS::String* name() const;41NS::Object* object() const;42NS::Dictionary* userInfo() const;43};4445using ObserverBlock = void(^)(Notification*);46using ObserverFunction = std::function<void(Notification*)>;4748class NotificationCenter : public NS::Referencing<NotificationCenter>49{50public:51static class NotificationCenter* defaultCenter();52Object* addObserver(NotificationName name, Object* pObj, void* pQueue, ObserverBlock block);53Object* addObserver(NotificationName name, Object* pObj, void* pQueue, ObserverFunction &handler);54void removeObserver(Object* pObserver);5556};57}5859//-------------------------------------------------------------------------------------------------------------------------------------------------------------6061_NS_INLINE NS::String* NS::Notification::name() const62{63return Object::sendMessage<NS::String*>(this, _NS_PRIVATE_SEL(name));64}6566//-------------------------------------------------------------------------------------------------------------------------------------------------------------6768_NS_INLINE NS::Object* NS::Notification::object() const69{70return Object::sendMessage<NS::Object*>(this, _NS_PRIVATE_SEL(object));71}7273//-------------------------------------------------------------------------------------------------------------------------------------------------------------7475_NS_INLINE NS::Dictionary* NS::Notification::userInfo() const76{77return Object::sendMessage<NS::Dictionary*>(this, _NS_PRIVATE_SEL(userInfo));78}7980//-------------------------------------------------------------------------------------------------------------------------------------------------------------8182_NS_INLINE NS::NotificationCenter* NS::NotificationCenter::defaultCenter()83{84return NS::Object::sendMessage<NS::NotificationCenter*>(_NS_PRIVATE_CLS(NSNotificationCenter), _NS_PRIVATE_SEL(defaultCenter));85}8687//-------------------------------------------------------------------------------------------------------------------------------------------------------------8889_NS_INLINE NS::Object* NS::NotificationCenter::addObserver(NS::NotificationName name, Object* pObj, void* pQueue, NS::ObserverBlock block)90{91return NS::Object::sendMessage<Object*>(this, _NS_PRIVATE_SEL(addObserverName_object_queue_block_), name, pObj, pQueue, block);92}9394//-------------------------------------------------------------------------------------------------------------------------------------------------------------9596_NS_INLINE NS::Object* NS::NotificationCenter::addObserver(NS::NotificationName name, Object* pObj, void* pQueue, NS::ObserverFunction &handler)97{98__block ObserverFunction blockFunction = handler;99100return addObserver(name, pObj, pQueue, ^(NS::Notification* pNotif) {blockFunction(pNotif);});101}102103//-------------------------------------------------------------------------------------------------------------------------------------------------------------104105_NS_INLINE void NS::NotificationCenter::removeObserver(Object* pObserver)106{107return NS::Object::sendMessage<void>(this, _NS_PRIVATE_SEL(removeObserver_), pObserver);108}109110111112