/src/wt/src/Wt/Signals/signals.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2016 Emweb bv, Herent, Belgium. |
3 | | * |
4 | | * See the LICENSE file for terms of use. |
5 | | */ |
6 | | |
7 | | #include "signals.hpp" |
8 | | |
9 | | namespace Wt { namespace Signals { namespace Impl { |
10 | | |
11 | | SignalLinkBase::SignalLinkBase(CBUnlink unlink_callback) |
12 | 0 | : connected_(false), |
13 | 0 | connection_ring_(nullptr), |
14 | 0 | unlink_callback_(unlink_callback) |
15 | 0 | { } |
16 | | |
17 | | SignalLinkBase::~SignalLinkBase() |
18 | 0 | { |
19 | 0 | if (connection_ring_) |
20 | 0 | connection_ring_->unlinkAll(); |
21 | 0 | } |
22 | | |
23 | | Connection SignalLinkBase::connect(const Wt::Core::observable *object) |
24 | 0 | { |
25 | 0 | assert (!connected_); |
26 | |
|
27 | 0 | connected_ = true; |
28 | 0 | obj_.reset(object); |
29 | |
|
30 | 0 | return Connection(this); |
31 | 0 | } |
32 | | |
33 | | void SignalLinkBase::disconnect() |
34 | 0 | { |
35 | 0 | if (connection_ring_) |
36 | 0 | connection_ring_->unlinkAll(); |
37 | 0 | connection_ring_ = nullptr; |
38 | 0 | connected_ = false; |
39 | 0 | unlink_callback_(this); |
40 | 0 | } |
41 | | |
42 | | bool SignalLinkBase::isConnected() const |
43 | 0 | { |
44 | 0 | return connected_ && !obj_.observedDeleted(); |
45 | 0 | } |
46 | | |
47 | | Connection::Connection() noexcept |
48 | 0 | : next_(nullptr), |
49 | 0 | prev_(nullptr), |
50 | 0 | signalLink_(nullptr) |
51 | 0 | { } |
52 | | |
53 | | Connection::Connection(const Connection& conn) noexcept |
54 | 0 | : next_(nullptr), |
55 | 0 | prev_(nullptr), |
56 | 0 | signalLink_(nullptr) |
57 | 0 | { |
58 | 0 | *this = conn; |
59 | 0 | } |
60 | | |
61 | | Connection::Connection(Connection&& conn) noexcept |
62 | 0 | : next_(nullptr), |
63 | 0 | prev_(nullptr), |
64 | 0 | signalLink_(nullptr) |
65 | 0 | { |
66 | 0 | *this = std::move(conn); |
67 | 0 | } |
68 | | |
69 | | Connection::~Connection() noexcept |
70 | 0 | { |
71 | 0 | clear(); |
72 | 0 | } |
73 | | |
74 | | Connection& Connection::operator= (const Connection& other) noexcept |
75 | 0 | { |
76 | 0 | if (this == &other) |
77 | 0 | return *this; |
78 | | |
79 | 0 | clear(); |
80 | |
|
81 | 0 | if (other.isConnected()) { |
82 | 0 | signalLink_ = other.signalLink_; |
83 | 0 | next_ = &other; |
84 | 0 | prev_ = other.prev_; |
85 | 0 | next_->prev_ = this; |
86 | 0 | prev_->next_ = this; |
87 | 0 | } |
88 | |
|
89 | 0 | return *this; |
90 | 0 | } |
91 | | |
92 | | Connection& Connection::operator= (Connection&& other) noexcept |
93 | 0 | { |
94 | 0 | if (this == &other) |
95 | 0 | return *this; |
96 | | |
97 | 0 | const Connection &cother = other; |
98 | 0 | operator=(cother); |
99 | 0 | other.clear(); |
100 | |
|
101 | 0 | return *this; |
102 | 0 | } |
103 | | |
104 | | void Connection::disconnect() noexcept |
105 | 0 | { |
106 | 0 | if (signalLink_) |
107 | 0 | signalLink_->disconnect(); |
108 | 0 | } |
109 | | |
110 | | bool Connection::isConnected() const noexcept |
111 | 0 | { |
112 | 0 | if (signalLink_) |
113 | 0 | return signalLink_->isConnected(); |
114 | 0 | else |
115 | 0 | return false; |
116 | 0 | } |
117 | | |
118 | | Connection::Connection(SignalLinkBase *signalLink) noexcept |
119 | 0 | : next_(this), |
120 | 0 | prev_(this), |
121 | 0 | signalLink_(signalLink) |
122 | 0 | { |
123 | 0 | if (signalLink_->connection_ring_) { |
124 | 0 | next_ = signalLink_->connection_ring_; |
125 | 0 | prev_ = signalLink_->connection_ring_->prev_; |
126 | 0 | next_->prev_ = this; |
127 | 0 | prev_->next_ = this; |
128 | 0 | } else { |
129 | 0 | signalLink_->connection_ring_ = this; |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | void Connection::clear() noexcept |
134 | 0 | { |
135 | 0 | if (next_) { |
136 | 0 | bool lastConnection = next_ == this; |
137 | 0 | assert(lastConnection || prev_ != this); |
138 | |
|
139 | 0 | if (signalLink_) { |
140 | 0 | if (lastConnection) |
141 | 0 | signalLink_->connection_ring_ = nullptr; |
142 | 0 | else if (signalLink_->connection_ring_ == this) |
143 | 0 | signalLink_->connection_ring_ = next_; |
144 | 0 | } |
145 | |
|
146 | 0 | next_->prev_ = prev_; |
147 | 0 | prev_->next_ = next_; |
148 | 0 | } |
149 | |
|
150 | 0 | prev_ = next_ = nullptr; |
151 | 0 | signalLink_ = nullptr; |
152 | 0 | } |
153 | | |
154 | | void Connection::unlinkAll() const noexcept |
155 | 0 | { |
156 | 0 | signalLink_ = nullptr; |
157 | |
|
158 | 0 | const Connection *c = next_; |
159 | 0 | while (c && c != this) { |
160 | 0 | c->signalLink_ = nullptr; |
161 | 0 | auto cnext = c->next_; |
162 | 0 | c->next_ = nullptr; |
163 | 0 | c->prev_ = nullptr; |
164 | 0 | c = cnext; |
165 | 0 | } |
166 | |
|
167 | 0 | next_ = nullptr; |
168 | 0 | prev_ = nullptr; |
169 | 0 | } |
170 | | |
171 | | }}} |