-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignals.h
More file actions
148 lines (121 loc) · 3.2 KB
/
signals.h
File metadata and controls
148 lines (121 loc) · 3.2 KB
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
#ifndef SIGNAL_H
#define SIGNAL_H
#include <functional>
#include <utility>
#include "intrusive_list.h"
namespace signals {
template <typename T>
struct signal;
template <typename... Args>
struct signal<void(Args...)> {
struct connection;
struct signal_tag {};
using connection_list = intrusive_list<connection, signal_tag>;
using slot_t = std::function<void(Args...)>;
using base_node = details::intrusive_list_node<signal_tag>;
struct connection : public base_node {
friend class signal;
connection() = default;
explicit connection(signal* sig, slot_t slot) : sig(sig), slot(std::move(slot)) {
sig->list.push_back(*this);
}
connection(connection const&) = delete;
connection(connection&& other) : slot(std::move(other.slot)), sig(other.sig) {
this->update();
if (sig) {
sig->list.insert(++(typename connection_list::iterator(&other)), *this);
}
other.disconnect();
}
void swap(connection& other) {
using std::swap;
static_cast<base_node&>(*this).swap(static_cast<base_node&>(other));
swap(slot, other.slot);
swap(sig, other.sig);
}
connection& operator=(connection const&) = delete;
connection& operator=(connection&& other) {
if (&other != this) {
connection(std::move(other)).swap(*this);
}
return *this;
}
void disconnect() {
if (!sig) {
return;
}
for (iterator_holder* cur = sig->tail; cur != nullptr; cur = cur->prev) {
if (this == &(*(cur->current))) {
++(cur->current);
}
}
this->remove();
sig = nullptr;
}
~connection() {
disconnect();
}
private:
slot_t slot;
signal* sig{nullptr};
}; // connection
connection connect(slot_t slt) {
return connection(this, slt);
}
template <typename... Args_>
void operator()(Args_... args) {
for (iterator_holder it(this); it.current != list.end();) {
(it.current++)->slot(args...);
if (!it.sig) {
return;
}
}
}
void swap(signal& other) {
using std::swap;
list.swap(other.list);
swap(tail, other.tail);
}
signal() = default;
signal(signal const&) = delete;
signal(signal&& other) : list(std::move(other.list)), tail(other.tail) {
other.tail = nullptr;
}
signal& operator=(signal const&) = delete;
signal& operator=(signal&& other) {
if (&other != *this) {
signal(std::move(other)).swap(*this);
}
return *this;
}
~signal() {
iterator_holder* it;
while (tail != nullptr) {
it = tail->prev;
tail->sig = nullptr;
tail = it;
}
for (typename connection_list::iterator it = list.begin(); it != list.end(); ++it) {
it->sig = nullptr;
}
}
private:
struct iterator_holder {
explicit iterator_holder(signal* sig) : sig(sig), current(sig->list.begin()), prev(sig->tail) {
sig->tail = this;
}
~iterator_holder() {
if (sig) {
sig->tail = prev;
sig = nullptr;
}
}
iterator_holder* prev;
typename connection_list::const_iterator current;
signal const* sig;
};
connection_list list;
mutable iterator_holder* tail{nullptr};
};
} // namespace signals
#endif