-
Notifications
You must be signed in to change notification settings - Fork 61
/
nano_observer.hpp
202 lines (159 loc) · 5.49 KB
/
nano_observer.hpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pragma once
#include <algorithm>
#include <vector>
#include "nano_function.hpp"
#include "nano_mutex.hpp"
namespace Nano
{
template <typename MT_Policy = ST_Policy>
class Observer : private MT_Policy
{
// Only Nano::Signal is allowed private access
template <typename, typename> friend class Signal;
struct Connection
{
Delegate_Key delegate;
typename MT_Policy::Weak_Ptr observer;
Connection() noexcept = default;
Connection(Delegate_Key const& key) : delegate(key), observer() {}
Connection(Delegate_Key const& key, Observer* obs) : delegate(key), observer(obs->weak_ptr()) {}
};
struct Z_Order
{
inline bool operator()(Delegate_Key const& lhs, Delegate_Key const& rhs) const
{
std::size_t x = lhs[0] ^ rhs[0];
std::size_t y = lhs[1] ^ rhs[1];
auto k = (x < y) && x < (x ^ y);
return lhs[k] < rhs[k];
}
inline bool operator()(Connection const& lhs, Connection const& rhs) const
{
return operator()(lhs.delegate, rhs.delegate);
}
};
std::vector<Connection> connections;
//--------------------------------------------------------------------------
void nolock_insert(Delegate_Key const& key, Observer* obs)
{
auto begin = std::begin(connections);
auto end = std::end(connections);
connections.emplace(std::upper_bound(begin, end, key, Z_Order()), key, obs);
}
void insert(Delegate_Key const& key, Observer* obs)
{
[[maybe_unused]]
auto lock = MT_Policy::lock_guard();
nolock_insert(key, obs);
}
void remove(Delegate_Key const& key) noexcept
{
[[maybe_unused]]
auto lock = MT_Policy::lock_guard();
auto begin = std::begin(connections);
auto end = std::end(connections);
auto slots = std::equal_range(begin, end, key, Z_Order());
connections.erase(slots.first, slots.second);
}
//--------------------------------------------------------------------------
template <typename Function, typename... Uref>
void for_each(Uref&&... args)
{
[[maybe_unused]]
auto lock = MT_Policy::lock_guard();
for (auto const& slot : MT_Policy::copy_or_ref(connections, lock))
{
if (auto observer = MT_Policy::observed(slot.observer))
{
Function::bind(slot.delegate)(args...);
}
}
}
template <typename Function, typename Accumulate, typename... Uref>
void for_each_accumulate(Accumulate&& accumulate, Uref&&... args)
{
[[maybe_unused]]
auto lock = MT_Policy::lock_guard();
for (auto const& slot : MT_Policy::copy_or_ref(connections, lock))
{
if (auto observer = MT_Policy::observed(slot.observer))
{
accumulate(Function::bind(slot.delegate)(args...));
}
}
}
//--------------------------------------------------------------------------
void nolock_disconnect_all() noexcept
{
for (auto const& slot : connections)
{
if (auto observed = MT_Policy::visiting(slot.observer))
{
auto ptr = static_cast<Observer*>(MT_Policy::unmask(observed));
ptr->remove(slot.delegate);
}
}
connections.clear();
}
void move_connections_from(Observer* other) noexcept
{
[[maybe_unused]]
auto lock = MT_Policy::scoped_lock(other);
// Make sure this is disconnected and ready to receive
nolock_disconnect_all();
// Disconnect other from everyone else and connect them to this
for (auto const& slot : other->connections)
{
if (auto observed = other->visiting(slot.observer))
{
auto ptr = static_cast<Observer*>(MT_Policy::unmask(observed));
ptr->remove(slot.delegate);
ptr->insert(slot.delegate, this);
nolock_insert(slot.delegate, ptr);
}
// Connect free functions and function objects
else
{
nolock_insert(slot.delegate, this);
}
}
other->connections.clear();
}
//--------------------------------------------------------------------------
public:
void disconnect_all() noexcept
{
[[maybe_unused]]
auto lock = MT_Policy::lock_guard();
nolock_disconnect_all();
}
bool is_empty() const noexcept
{
[[maybe_unused]]
auto lock = MT_Policy::lock_guard();
return connections.empty();
}
protected:
// Guideline #4: A base class destructor should be
// either public and virtual, or protected and non-virtual.
~Observer()
{
MT_Policy::before_disconnect_all();
disconnect_all();
}
Observer() noexcept = default;
// Observer may be movable depending on policy, but should never be copied
Observer(Observer const&) noexcept = delete;
Observer& operator= (Observer const&) noexcept = delete;
// When moving an observer, make sure everyone it's connected to knows about it
Observer(Observer&& other) noexcept
{
move_connections_from(std::addressof(other));
}
Observer& operator=(Observer&& other) noexcept
{
move_connections_from(std::addressof(other));
return *this;
}
};
} // namespace Nano ------------------------------------------------------------