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
|
// Slot.hh for FbTk, Fluxbox Toolkit
// Copyright (c) 2008 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#ifndef FBTK_SLOT_HH
#define FBTK_SLOT_HH
namespace FbTk {
/// \namespace Implementation details for signals, do not use anything in this namespace
namespace SigImpl {
class CallbackHolder;
/// Placeholder type for typed callbacks
typedef void* (*CallbackFunc)(void *);
/// Clone function callback type for cloning typed callback holders
typedef CallbackHolder* (*CloneFunc)(CallbackHolder*);
/// Kill function callback type for destroying type specific information in
/// FunctorHolder
typedef void (*KillFunc)(CallbackHolder*);
/// Holds clone, functor callback, and the kill function for FunctorHolder.
class CallbackHolder {
public:
/**
* @param callback The callback to call when a slot receives a signal.
* @param clone The callback to use for cloning a type specific instance of
* this classinstance.
* @param kill The callback that knows how to free the memory in type
* specific instance of this class.
*/
CallbackHolder(CallbackFunc callback,
CloneFunc clone,
KillFunc kill):
m_callback(callback),
m_kill(kill),
m_clone(clone) { }
~CallbackHolder() {
(*m_kill)(this);
}
/// @return a clone of this instance
CallbackHolder* clone() {
return (*m_clone)(this);
}
/// \c Callback to \c Functor specific callback
CallbackFunc m_callback;
protected:
CallbackHolder& operator = (const CallbackHolder& other) {
if ( this == &other ) {
return *this;
}
m_callback = other.m_callback;
m_clone = other.m_clone;
m_kill = other.m_kill;
return *this;
}
CallbackHolder(const CallbackHolder& other) {
*this = other;
}
private:
/// This function is called to kill this instance
KillFunc m_kill;
/// Functions that knows how to clone a specific \c Functor type
CloneFunc m_clone;
};
/// Holds the functor and creates a clone callback for \c Functor specific type
template <typename Functor>
class FunctorHolder: public CallbackHolder {
public:
/// This type.
typedef FunctorHolder<Functor> Self;
/**
* @param functor The functor to be used when a signal is emitted.
* @param callback The callback to call when a signal is emitted.
*/
FunctorHolder(const Functor& functor, CallbackFunc callback):
CallbackHolder(callback, &clone, &kill),
m_functor(functor) {
}
/// Specific clone for this Functor type
static CallbackHolder* clone(CallbackHolder* self) {
return new Self( static_cast<Self&>(*self));
}
static void kill(CallbackHolder* self) {
// Destroy functor
static_cast<Self*>( self )->m_functor.~Functor();
}
Functor m_functor; ///< the functor to use when a signal is emitted.
};
/// Callback with no arguments.
template <typename Functor, typename ReturnType >
struct Callback0 {
static ReturnType callback(CallbackHolder* base) {
static_cast< FunctorHolder<Functor>* >( base )->m_functor();
return ReturnType();
}
static CallbackFunc functionAddress() {
return reinterpret_cast<CallbackFunc>(&callback);
}
};
/// Callback with one argument
template <typename Functor, typename ReturnType, typename Arg1>
struct Callback1 {
typedef ReturnType (Functor::* CallbackType)(CallbackHolder*, Arg1);
static ReturnType callback(CallbackHolder* base, Arg1 arg1) {
static_cast< FunctorHolder<Functor>* >( base )->m_functor(arg1);
return ReturnType();
}
static CallbackFunc functionAddress() {
return reinterpret_cast<CallbackFunc>(&callback);
}
};
/// Callback with two arguments
template <typename Functor, typename ReturnType,
typename Arg1, typename Arg2>
struct Callback2 {
typedef ReturnType (Functor::* CallbackType)(CallbackHolder*, Arg1, Arg2);
static ReturnType callback(CallbackHolder* base, Arg1 arg1, Arg2 arg2) {
static_cast< FunctorHolder<Functor>* >( base )->m_functor(arg1, arg2);
return ReturnType();
}
static CallbackFunc functionAddress() {
return reinterpret_cast<CallbackFunc>(&callback);
}
};
/// Callback with three arguments
template <typename Functor, typename ReturnType,
typename Arg1, typename Arg2, typename Arg3>
struct Callback3 {
typedef ReturnType (Functor::* CallbackType)(CallbackHolder*, Arg1, Arg2, Arg3);
static ReturnType callback(CallbackHolder* base, Arg1 arg1, Arg2 arg2, Arg3 arg3) {
static_cast< FunctorHolder<Functor>* >( base )->m_functor( arg1, arg2, arg3 );
return ReturnType();
}
static CallbackFunc functionAddress() {
return reinterpret_cast<CallbackFunc>(&callback);
}
};
/// Holds callback holder and handles the copying of callback holders for the
/// \c Slots.
class SlotHolder {
public:
SlotHolder(const SlotHolder& other):
m_holder( other.m_holder ? other.m_holder->clone() : 0 ) {
}
~SlotHolder() {
delete m_holder;
}
SlotHolder& operator = (const SlotHolder& other) {
if ( &other == this ) {
return *this;
}
delete m_holder;
if ( other.m_holder ) {
m_holder = other.m_holder->clone();
} else {
m_holder = 0;
}
return *this;
}
SlotHolder():m_holder( 0 ) { }
protected:
explicit SlotHolder(CallbackHolder* holder):
m_holder( holder ) {
}
CallbackHolder* m_holder;
};
/// Slot with no argument.
template <typename ReturnType>
class Slot0: public SlotHolder {
public:
typedef ReturnType (*CallbackType)(CallbackHolder*);
template <typename Functor>
Slot0( const Functor& functor ):
SlotHolder( new FunctorHolder<Functor>
(functor, Callback0<Functor, ReturnType>::functionAddress())) {
}
void operator()() {
if (m_holder)
reinterpret_cast<CallbackType>(m_holder->m_callback)( m_holder );
}
};
/// Slot with one argument.
template <typename ReturnType, typename Arg1>
class Slot1:public SlotHolder {
public:
typedef ReturnType (*CallbackType)(CallbackHolder*, Arg1);
template <typename Functor>
Slot1( const Functor& functor ):
SlotHolder( new FunctorHolder<Functor>
(functor, Callback1<Functor, ReturnType, Arg1>::functionAddress())){
}
void operator()(Arg1 arg) {
if (m_holder)
reinterpret_cast<CallbackType>(m_holder->m_callback)(m_holder, arg);
}
};
/// Slot with two arguments
template <typename ReturnType, typename Arg1, typename Arg2>
class Slot2: public SlotHolder {
public:
typedef ReturnType (*CallbackType)(CallbackHolder*, Arg1, Arg2);
template <typename Functor>
Slot2( const Functor& functor ):
SlotHolder( new FunctorHolder<Functor>
(functor, Callback2<Functor, ReturnType, Arg1, Arg2>::functionAddress())){
}
void operator()(Arg1 arg1, Arg2 arg2) {
if (m_holder)
reinterpret_cast<CallbackType>(m_holder->m_callback)(m_holder, arg1, arg2);
}
};
/// Slot with three arguments
template <typename ReturnType, typename Arg1, typename Arg2, typename Arg3>
class Slot3: public SlotHolder {
public:
typedef ReturnType (*CallbackType)(CallbackHolder*, Arg1, Arg2, Arg3);
template <typename Functor>
Slot3( const Functor& functor ):
SlotHolder( new FunctorHolder<Functor>
(functor, Callback3<Functor, ReturnType, Arg1, Arg2, Arg3>::functionAddress())){
}
void operator()(Arg1 arg1, Arg2 arg2, Arg3 arg3) {
if (m_holder)
reinterpret_cast<CallbackType>(m_holder->m_callback)
( m_holder, arg1, arg2, arg3 );
}
};
} // namespace SigImpl
} // namespace FbTk
#endif // FBTK_SLOT_H
|