Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/metal-cpp/Foundation/NSNotification.hpp
21066 views
1
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
2
//
3
// Foundation/NSNotification.hpp
4
//
5
// Copyright 2020-2024 Apple Inc.
6
//
7
// Licensed under the Apache License, Version 2.0 (the "License");
8
// you may not use this file except in compliance with the License.
9
// You may obtain a copy of the License at
10
//
11
// http://www.apache.org/licenses/LICENSE-2.0
12
//
13
// Unless required by applicable law or agreed to in writing, software
14
// distributed under the License is distributed on an "AS IS" BASIS,
15
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
// See the License for the specific language governing permissions and
17
// limitations under the License.
18
//
19
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
20
21
#pragma once
22
23
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
24
25
#include "NSDefines.hpp"
26
#include "NSDictionary.hpp"
27
#include "NSObject.hpp"
28
#include "NSString.hpp"
29
#include "NSTypes.hpp"
30
#include <functional>
31
32
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
33
34
namespace NS
35
{
36
using NotificationName = class String*;
37
38
class Notification : public NS::Referencing<Notification>
39
{
40
public:
41
NS::String* name() const;
42
NS::Object* object() const;
43
NS::Dictionary* userInfo() const;
44
};
45
46
using ObserverBlock = void(^)(Notification*);
47
using ObserverFunction = std::function<void(Notification*)>;
48
49
class NotificationCenter : public NS::Referencing<NotificationCenter>
50
{
51
public:
52
static class NotificationCenter* defaultCenter();
53
Object* addObserver(NotificationName name, Object* pObj, void* pQueue, ObserverBlock block);
54
Object* addObserver(NotificationName name, Object* pObj, void* pQueue, ObserverFunction &handler);
55
void removeObserver(Object* pObserver);
56
57
};
58
}
59
60
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
61
62
_NS_INLINE NS::String* NS::Notification::name() const
63
{
64
return Object::sendMessage<NS::String*>(this, _NS_PRIVATE_SEL(name));
65
}
66
67
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
68
69
_NS_INLINE NS::Object* NS::Notification::object() const
70
{
71
return Object::sendMessage<NS::Object*>(this, _NS_PRIVATE_SEL(object));
72
}
73
74
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
75
76
_NS_INLINE NS::Dictionary* NS::Notification::userInfo() const
77
{
78
return Object::sendMessage<NS::Dictionary*>(this, _NS_PRIVATE_SEL(userInfo));
79
}
80
81
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
82
83
_NS_INLINE NS::NotificationCenter* NS::NotificationCenter::defaultCenter()
84
{
85
return NS::Object::sendMessage<NS::NotificationCenter*>(_NS_PRIVATE_CLS(NSNotificationCenter), _NS_PRIVATE_SEL(defaultCenter));
86
}
87
88
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
89
90
_NS_INLINE NS::Object* NS::NotificationCenter::addObserver(NS::NotificationName name, Object* pObj, void* pQueue, NS::ObserverBlock block)
91
{
92
return NS::Object::sendMessage<Object*>(this, _NS_PRIVATE_SEL(addObserverName_object_queue_block_), name, pObj, pQueue, block);
93
}
94
95
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
96
97
_NS_INLINE NS::Object* NS::NotificationCenter::addObserver(NS::NotificationName name, Object* pObj, void* pQueue, NS::ObserverFunction &handler)
98
{
99
__block ObserverFunction blockFunction = handler;
100
101
return addObserver(name, pObj, pQueue, ^(NS::Notification* pNotif) {blockFunction(pNotif);});
102
}
103
104
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
105
106
_NS_INLINE void NS::NotificationCenter::removeObserver(Object* pObserver)
107
{
108
return NS::Object::sendMessage<void>(this, _NS_PRIVATE_SEL(removeObserver_), pObserver);
109
}
110
111
112