Skip to content

Commit 5573e97

Browse files
committed
slot: Allow sigc::slot<R(Args...)> declaration, like std::function.
By adding a template specialization that repeats each slot*<> declarartion, though it would be good to avoid the repetition. Bug 763393
1 parent dc006c7 commit 5573e97

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

sigc++/functors/macros/slot.h.m4

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,74 @@ public:
254254
}
255255
};
256256
257+
258+
/** Convenience wrapper for the numbered sigc::slot$1 template.
259+
* See the base class for useful methods.
260+
* This is the template specialization of the unnumbered sigc::slot
261+
* template for $1 argument(s), specialized for different numbers of arguments
262+
* This is possible because the template has default (nil) template types.
263+
dnl *
264+
dnl * @ingroup slot
265+
*
266+
* This specialization allow use of the sigc::slot<R(Args...)> syntax,
267+
*/
268+
template <LIST(class T_return, LOOP(class T_arg%1, $1))>
269+
class slot<T_return(LIST(LOOP(T_arg%1, $1)))>
270+
: public slot$1<LIST(T_return, LOOP(T_arg%1, $1))>
271+
{
272+
public:
273+
typedef slot$1<LIST(T_return, LOOP(T_arg%1, $1))> parent_type;
274+
275+
inline slot() {}
276+
277+
/** Constructs a slot from an arbitrary functor.
278+
* @param _A_func The desired functor the new slot should be assigned to.
279+
*/
280+
template <class T_functor>
281+
slot(const T_functor& _A_func)
282+
: parent_type(_A_func) {}
283+
284+
// Without static_cast parent_type(const T_functor& _A_func)
285+
// is called instead of the copy constructor.
286+
/** Constructs a slot, copying an existing one.
287+
* @param src The existing slot to copy.
288+
*/
289+
slot(const slot& src)
290+
: parent_type(static_cast<const parent_type&>(src)) {}
291+
292+
// Without static_cast parent_type(const T_functor& _A_func)
293+
// is called instead of the move constructor.
294+
/** Constructs a slot, moving an existing one.
295+
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved.
296+
* @param src The existing slot to move or copy.
297+
*/
298+
slot(slot&& src)
299+
: parent_type(std::move(static_cast<parent_type&>(src))) {}
300+
301+
/** Overrides this slot, making a copy from another slot.
302+
* @param src The slot from which to make a copy.
303+
* @return @p this.
304+
*/
305+
slot& operator=(const slot& src)
306+
{
307+
parent_type::operator=(src);
308+
return *this;
309+
}
310+
311+
/** Overrides this slot, making a move from another slot.
312+
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved.
313+
* @param src The slot from which to move or copy.
314+
* @return @p this.
315+
*/
316+
slot& operator=(slot&& src)
317+
{
318+
parent_type::operator=(std::move(src));
319+
return *this;
320+
}
321+
};
322+
323+
324+
257325
ifelse($1, $2,[dnl
258326
#ifndef DOXYGEN_SHOULD_SKIP_THIS
259327
//template specialization of visitor<>::do_visit_each<>(action, functor):

tests/test_slot.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ void test_simple()
5050
util->check_result(result_stream, "foo(int 2)");
5151
}
5252

53+
void test_std_function_style_syntax()
54+
{
55+
// simple test
56+
sigc::slot<void(int)> s1 = foo();
57+
s1(1);
58+
util->check_result(result_stream, "foo(int 1)");
59+
60+
s1 = foo();
61+
s1(2);
62+
util->check_result(result_stream, "foo(int 2)");
63+
}
64+
5365
void test_implicit_conversion()
5466
{
5567
// test implicit conversion
@@ -100,6 +112,7 @@ int main(int argc, char* argv[])
100112
return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
101113

102114
test_simple();
115+
test_std_function_style_syntax();
103116
test_implicit_conversion();
104117
test_reference();
105118
test_operator_equals();

0 commit comments

Comments
 (0)