-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathks_notification_center.h
112 lines (90 loc) · 4.77 KB
/
ks_notification_center.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Copyright 2024 The Kingsoft's ks-async Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#pragma once
#include "ks_async_base.h"
#include "ks_apartment.h"
#include "ks_async_context.h"
#include "ktl/ks_any.h"
#include <string>
#include <memory>
class ks_notification;
class ks_notification_center;
class ks_notification {
public:
template <class DATA_TYPE>
explicit ks_notification(const void* sender, const char* notification_name, DATA_TYPE&& notification_data, const ks_async_context& notification_context = {})
: m_props(std::make_shared<_NOTIFICATION_PROPS>()) {
m_props->sender = sender;
m_props->notification_name = notification_name != nullptr ? notification_name : "";
m_props->notification_data_any = ks_any::of(std::forward<DATA_TYPE>(notification_data));
m_props->notification_context = notification_context;
}
template <class DATA_TYPE>
explicit ks_notification(const void* sender, const char* notification_name, const ks_async_context& notification_context, DATA_TYPE&& notification_data)
: ks_notification(sender, notification_name, std::forward<DATA_TYPE>(notification_data), notification_context) { //only for compat
}
ks_notification(const ks_notification&) = default;
ks_notification& operator=(const ks_notification&) = default;
ks_notification(ks_notification&&) noexcept = default;
ks_notification& operator=(ks_notification&&) noexcept = default;
public:
const void* get_sender() const { ASSERT(m_props != nullptr); return m_props->sender; }
const char* get_notification_name() const { ASSERT(m_props != nullptr); return m_props->notification_name.c_str(); }
template <class DATA_TYPE>
const DATA_TYPE& get_notification_data() const { ASSERT(m_props != nullptr); return m_props->notification_data_any.get<DATA_TYPE>(); }
const ks_async_context& get_notification_context() const { ASSERT(m_props != nullptr); return m_props->notification_context; }
private:
struct _NOTIFICATION_PROPS {
const void* sender;
std::string notification_name;
ks_any notification_data_any;
ks_async_context notification_context = ks_async_context::__empty_inst();
};
std::shared_ptr<_NOTIFICATION_PROPS> m_props;
};
class ks_notification_center {
private:
enum class __raw_ctor { v };
public:
ks_notification_center() = delete;
_DISABLE_COPY_CONSTRUCTOR(ks_notification_center);
explicit ks_notification_center(__raw_ctor, const char* center_name);
~ks_notification_center();
public:
KS_ASYNC_API static ks_notification_center* default_center();
KS_ASYNC_API static std::shared_ptr<ks_notification_center> _create_center(const char* center_name);
public:
KS_ASYNC_API void add_observer(
const void* observer, const char* notification_name,
ks_apartment* apartment, std::function<void(const ks_notification&)>&& fn, const ks_async_context& context = {});
KS_ASYNC_INLINE_API void add_observer(
const void* observer, const char* notification_name,
ks_apartment* apartment, const ks_async_context& context, std::function<void(const ks_notification&)>&& fn) { //only for compat
return this->add_observer(observer, notification_name, apartment, std::move(fn), context);
}
KS_ASYNC_API void remove_observer(const void* observer, const char* notification_name);
KS_ASYNC_API void remove_observer(const void* observer);
public:
template <class DATA_TYPE>
KS_ASYNC_INLINE_API void post_notification(const void* sender, const char* notification_name, DATA_TYPE&& notification_data, const ks_async_context& notification_context = {}) {
return this->do_post_notification(ks_notification(sender, notification_name, std::forward<DATA_TYPE>(notification_data), notification_context));
}
template <class DATA_TYPE>
KS_ASYNC_INLINE_API void post_notification(const void* sender, const char* notification_name, const ks_async_context& notification_context, DATA_TYPE&& notification_data) { //only for compat
return this->post_notification(sender, notification_name, std::forward<DATA_TYPE>(notification_data), notification_context);
}
private:
KS_ASYNC_API void do_post_notification(const ks_notification& notification);
private:
class __ks_notification_center_data;
__ks_notification_center_data* m_d;
};