-
Notifications
You must be signed in to change notification settings - Fork 935
Expand file tree
/
Copy pathSignalSlot.h
More file actions
189 lines (160 loc) · 3.65 KB
/
SignalSlot.h
File metadata and controls
189 lines (160 loc) · 3.65 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
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
#ifndef MUDUO_BASE_SIGNALSLOT_H
#define MUDUO_BASE_SIGNALSLOT_H
#include "Mutex.h"
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <vector>
namespace muduo
{
namespace detail
{
template<typename Callback>
struct SlotImpl;
template<typename Callback>
struct SignalImpl : boost::noncopyable
{
typedef std::vector<boost::weak_ptr<SlotImpl<Callback> > > SlotList;
SignalImpl()
: slots_(new SlotList)
{
}
void copyOnWrite()
{
mutex_.assertLocked();
if (!slots_.unique())
{
slots_.reset(new SlotList(*slots_));
}
assert(slots_.unique());
}
void clean()
{
MutexLockGuard lock(mutex_);
copyOnWrite();
SlotList& list(*slots_);
typename SlotList::iterator it(list.begin());
while (it != list.end())
{
if (it->expired())
{
it = list.erase(it);
}
else
{
++it;
}
}
}
MutexLock mutex_;
boost::shared_ptr<SlotList> slots_;
};
template<typename Callback>
struct SlotImpl : boost::noncopyable
{
typedef SignalImpl<Callback> Data;
SlotImpl(const boost::shared_ptr<Data>& data, Callback&& cb)
: data_(data), cb_(cb), tie_(), tied_(false)
{
}
SlotImpl(const boost::shared_ptr<Data>& data, Callback&& cb,
const boost::shared_ptr<void>& tie)
: data_(data), cb_(cb), tie_(tie), tied_(true)
{
}
~SlotImpl()
{
boost::shared_ptr<Data> data(data_.lock());
if (data)
{
data->clean();
}
}
boost::weak_ptr<Data> data_;
Callback cb_;
boost::weak_ptr<void> tie_;
bool tied_;
};
}
/// This is the handle for a slot
///
/// The slot will remain connected to the signal fot the life time of the
/// returned Slot object (and its copies).
typedef boost::shared_ptr<void> Slot;
template<typename Signature>
class Signal;
template <typename RET, typename... ARGS>
class Signal<RET(ARGS...)> : boost::noncopyable
{
public:
typedef std::function<void (ARGS...)> Callback;
typedef detail::SignalImpl<Callback> SignalImpl;
typedef detail::SlotImpl<Callback> SlotImpl;
Signal()
: impl_(new SignalImpl)
{
}
~Signal()
{
}
template<typename Func>
Slot connect(Func&& func)
{
boost::shared_ptr<SlotImpl> slotImpl(
new SlotImpl(impl_, std::forward<Func>(func)));
add(slotImpl);
return slotImpl;
}
template<typename Func>
Slot connect(Func&& func, const boost::shared_ptr<void>& tie)
{
boost::shared_ptr<SlotImpl> slotImpl(new SlotImpl(impl_, std::forward<Func>(func), tie));
add(slotImpl);
return slotImpl;
}
template<typename... Args>
void call(Args&&... args)
{
SignalImpl& impl(*impl_);
boost::shared_ptr<typename SignalImpl::SlotList> slots;
{
MutexLockGuard lock(impl.mutex_);
slots = impl.slots_;
}
typename SignalImpl::SlotList& s(*slots);
for (typename SignalImpl::SlotList::const_iterator it = s.begin(); it != s.end(); ++it)
{
boost::shared_ptr<SlotImpl> slotImpl = it->lock();
if (slotImpl)
{
boost::shared_ptr<void> guard;
if (slotImpl->tied_)
{
guard = slotImpl->tie_.lock();
if (guard)
{
slotImpl->cb_(std::forward<Args>(args)...);
}
}
else
{
slotImpl->cb_(std::forward<Args>(args)...);
}
}
}
}
private:
void add(const boost::shared_ptr<SlotImpl>& slot)
{
SignalImpl& impl(*impl_);
{
MutexLockGuard lock(impl.mutex_);
impl.copyOnWrite();
impl.slots_->push_back(slot);
}
}
const boost::shared_ptr<SignalImpl> impl_;
};
}
#endif // MUDUO_BASE_SIGNALSLOT_H