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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
// safe_slot.h -*-c++-*-
//
// Copyright (C) 2008-2009 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#ifndef SAFE_SLOT_H
#define SAFE_SLOT_H
#include <sigc++/bind.h>
#include <sigc++/slot.h>
#include <cwidget/generic/threads/threads.h>
/** \file safe_slot.h
*
* This file defines "thread-safe" slot-like objects. I place that
* in quote because these slots can \e not be invoked in a
* thread-safe way: they suffer from the usual slot caveats (for
* instance, if an object mentioned in the slot is deleted while the
* slot is executing, the results are unpredictable). However, the
* slots may be freely copied without creating thread-related
* problems, as long as the last copy of a slot is not thrown away in
* any thread but one where it's safe to invoke the slot.
*/
/** \brief The class used in the background to implement safe slots.
*
* This creates a slot on the heap that's reference-counted in a
* thread-safe way; instances of the class are actually
* reference-counting handles. Be careful not to create reference
* cycles!
*
* \typeparam T The slot type to wrap.
*/
template<typename T>
class safe_slot_wrapper
{
class implementation
{
T slot;
int refcount;
cwidget::threads::mutex m;
public:
explicit implementation(const T &_slot)
: slot(_slot),
refcount(0)
{
}
void disconnect()
{
slot.disconnect();
}
const T &get_slot() const
{
return slot;
}
void incref()
{
cwidget::threads::mutex::lock l(m);
++refcount;
}
void decref()
{
int new_count;
{
cwidget::threads::mutex::lock l(m);
--refcount;
new_count = refcount;
}
if(new_count == 0)
delete this;
}
};
implementation *impl;
public:
safe_slot_wrapper(const T &_slot)
: impl(new implementation(_slot))
{
impl->incref();
}
safe_slot_wrapper()
: impl(NULL)
{
}
safe_slot_wrapper(const safe_slot_wrapper &other)
: impl(other.impl)
{
if(impl != NULL)
impl->incref();
}
~safe_slot_wrapper()
{
if(impl != NULL)
impl->decref();
}
safe_slot_wrapper &operator=(const safe_slot_wrapper &other)
{
implementation *old_impl = impl;
impl = other.impl;
if(impl != NULL)
impl->incref();
if(old_impl != NULL)
old_impl->decref();
return *this;
}
void disconnect()
{
if(impl != NULL)
impl->disconnect();
}
/** \brief Return the stored slot, or a default-constructed slot if there is none. */
T get_slot() const
{
if(impl != NULL)
return impl->get_slot();
else
return T();
}
};
template<typename Ret>
class safe_slot0
{
safe_slot_wrapper<sigc::slot0<Ret> > real_slot;
public:
safe_slot0()
{
}
explicit safe_slot0(const sigc::slot0<Ret> &slot)
: real_slot(slot)
{
}
void disconnect() const { real_slot.disconnect(); }
sigc::slot0<Ret> get_slot() const { return real_slot.get_slot(); }
};
template<typename Ret>
safe_slot0<Ret> make_safe_slot(const sigc::slot0<Ret> &slot)
{
return safe_slot0<Ret>(slot);
}
template<typename Ret, typename A1>
class safe_slot1
{
safe_slot_wrapper<sigc::slot1<Ret, A1> > real_slot;
public:
safe_slot1()
{
}
explicit safe_slot1(const sigc::slot1<Ret, A1> &slot)
: real_slot(slot)
{
}
void disconnect() { real_slot.disconnect(); }
sigc::slot1<Ret, A1> get_slot() const { return real_slot.get_slot(); }
};
template<typename Ret, typename A1>
safe_slot1<Ret, A1> make_safe_slot(const sigc::slot1<Ret, A1> &slot)
{
return safe_slot1<Ret, A1>(slot);
}
template<typename Ret, typename A1>
Ret safe_bind_invoke(safe_slot1<Ret, A1> slot, A1 a1)
{
return slot.get_slot()(a1);
}
template<typename Ret, typename A1>
safe_slot0<Ret> safe_bind(const safe_slot1<Ret, A1> &slot, A1 a1)
{
sigc::slot0<Ret> rval =
sigc::bind(sigc::ptr_fun(&safe_bind_invoke<Ret, A1>),
slot, a1);
return make_safe_slot(rval);
}
template<typename Ret, typename A1, typename A2>
class safe_slot2
{
safe_slot_wrapper<sigc::slot2<Ret, A1, A2> > real_slot;
public:
safe_slot2()
{
}
explicit safe_slot2(const sigc::slot2<Ret, A1, A2> &slot)
: real_slot(slot)
{
}
void disconnect() { real_slot.disconnect(); }
sigc::slot2<Ret, A1, A2> get_slot() const { return real_slot.get_slot(); }
};
template<typename Ret, typename A1, typename A2>
safe_slot2<Ret, A1, A2> make_safe_slot(const sigc::slot2<Ret, A1, A2> &slot)
{
return safe_slot2<Ret, A1, A2>(slot);
}
template<typename Ret, typename A1, typename A2>
Ret safe_bind_invoke(safe_slot2<Ret, A1, A2> slot, A1 a1, A2 a2)
{
return slot.get_slot()(a1, a2);
}
template<typename Ret, typename A1, typename A2>
safe_slot0<Ret> safe_bind(const safe_slot2<Ret, A1, A2> &slot, A1 a1, A2 a2)
{
sigc::slot0<Ret> rval = sigc::bind(sigc::ptr_fun(&safe_bind_invoke<Ret, A1, A2>),
slot, a1, a2);
return make_safe_slot(rval);
}
template<typename Ret, typename A1, typename A2>
safe_slot1<Ret, A1> safe_bind(const safe_slot2<Ret, A1, A2> &slot, A2 a2)
{
sigc::slot1<Ret, A1> rval = sigc::bind(sigc::ptr_fun(&safe_bind_invoke<Ret, A1, A2>),
slot, a2);
return make_safe_slot(rval);
}
template<typename Ret, typename A1, typename A2, typename A3>
class safe_slot3
{
safe_slot_wrapper<sigc::slot3<Ret, A1, A2, A3> > real_slot;
public:
safe_slot3()
{
}
explicit safe_slot3(const sigc::slot3<Ret, A1, A2, A3> &slot)
: real_slot(slot)
{
}
void disconnect() { real_slot.disconnect(); }
sigc::slot3<Ret, A1, A2, A3> get_slot() const { return real_slot.get_slot(); }
};
template<typename Ret, typename A1, typename A2, typename A3>
safe_slot3<Ret, A1, A2, A3> make_safe_slot(const sigc::slot3<Ret, A1, A2, A3> &slot)
{
return safe_slot3<Ret, A1, A2, A3>(slot);
}
template<typename Ret, typename A1, typename A2, typename A3>
Ret safe_bind_invoke(safe_slot3<Ret, A1, A2, A3> slot, A1 a1, A2 a2, A3 a3)
{
return slot.get_slot()(a1, a2, a3);
}
template<typename Ret, typename A1, typename A2, typename A3>
safe_slot0<Ret> safe_bind(const safe_slot3<Ret, A1, A2, A3> &slot, A1 a1, A2 a2, A3 a3)
{
sigc::slot0<Ret> rval = sigc::bind(sigc::ptr_fun(&safe_bind_invoke<Ret, A1, A2, A3>),
slot, a1, a2, a3);
return make_safe_slot(rval);
}
template<typename Ret, typename A1, typename A2, typename A3>
safe_slot1<Ret, A1> safe_bind(const safe_slot3<Ret, A1, A2, A3> &slot, A2 a2, A3 a3)
{
sigc::slot1<Ret, A1> rval = sigc::bind(sigc::ptr_fun(&safe_bind_invoke<Ret, A1, A2, A3>),
slot, a2, a3);
return make_safe_slot(rval);
}
template<typename Ret, typename A1, typename A2, typename A3>
safe_slot2<Ret, A1, A2> safe_bind(const safe_slot3<Ret, A1, A2, A3> &slot, A3 a3)
{
sigc::slot2<Ret, A1, A2> rval = sigc::bind(sigc::ptr_fun(&safe_bind_invoke<Ret, A1, A2, A3>),
slot, a3);
return make_safe_slot(rval);
}
// I don't need more than 3 arguments, as it turns out.
#endif // SAFE_SLOT_H
|