1 /* Copyright (C) 2012 The libsigc++ Development Team
3 * This file is part of libsigc++.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 // The purpose of this test case is threefold.
20 // - Test that C++11 lambda expressions can be used in connection with sigc::slot
22 // - Show that libsigc++ lambda expressions can be replaced by C++11 lambda
23 // expressions. It's shown here as a preparation for deprecating and eventually
24 // deleting the libsigc++ lambda expressions.
25 // See https://bugzilla.gnome.org/show_bug.cgi?id=672555
26 // - Test the code examples in the documentation in sigc++/adaptors/lambda/base.h
27 // and sigc++/adaptors/lambda/group.h.
29 // At present (August 2012) this test case contains approximately the same tests
30 // as test_lambda.cc, but with libsigc++ lambda expressions replaced by C++11
31 // lambda expressions, where possible.
32 // The only real disadvantage of the C++11 lambda expressions is that a slot that
33 // contains an object derived from sigc::trackable is not automatically disconnected
34 // when the object is deleted, if a reference to the object is stored in a C++11
35 // lambda expression, connected to the slot. But if you use sigc::track_obj(),
36 // the slot is automatically disconnected. Thus, the disadvantage is insignificant.
38 // To test the C++11 lambda expressions with gcc 4.6.3 (and probably some later
39 // versions of gcc; gcc 4.7.x also understands -std=c++11):
40 // make CXXFLAGS='-g -O2 -std=c++0x' test_cpp11_lambda
41 // ./test_cpp11_lambda
43 // If test_cpp11_lambda writes nothing and the return code is 0, the test has passed.
46 #include "testutilities.h"
52 #include <sigc++/functors/functors.h>
53 #include <sigc++/bind.h>
54 #include <sigc++/reference_wrapper.h>
55 #include <sigc++/adaptors/track_obj.h>
56 #include <sigc++/signal.h>
61 std::ostringstream result_stream;
65 result_stream << "foo(int " << i << ", int " << j << ") ";
71 result_stream << "foo_void(int " << i << ")";
76 int test(int i, int j)
78 result_stream << "bar::test(int " << i << ", int " << j << ") ";
84 result_stream << "bar::test_void(int " << i << ")";
88 void egon(std::string& str)
90 result_stream << "egon(string '" << str << "')";
91 str = "egon was here";
94 struct book : public sigc::trackable
96 explicit book(const std::string& name) : name_(name) {}
97 operator std::string& () { return name_; }
101 inline std::ostringstream& operator << (std::ostringstream& s, const book& b)
107 void foo_group1(int i, int j)
109 result_stream << "foo_group1(int " << i << ", int " << j << ")";
112 int bar_group1(int i)
114 result_stream << "bar_group1(int " << i << ") ";
118 void foo_group2(int i)
120 result_stream << "foo_group2(int " << i << ")";
123 void foo_group3(int& i)
125 result_stream << "foo_group3(int " << i << ")";
129 struct bar_group4 : public sigc::trackable
133 void foo_group4(bar_group4&)
135 result_stream << "foo_group4(bar_group4&)";
138 } // end anonymous namespace
142 int main(int argc, char* argv[])
144 auto util = TestUtilities::get_instance();
146 if (!util->check_command_args(argc, argv))
147 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
150 // test lambda operators
151 //std::cout << "(_1 + _2) (3,4): " << (_1 + _2) (3,4) << std::endl;
152 result_stream << ([] (int a, int b) -> int { return a + b; }(3,4));
153 util->check_result(result_stream, "7");
155 //std::cout << "(_1 + 1) (3,4): " << (_1 + 1) (3,4) << std::endl;
156 result_stream << ([] (int a, int) -> int { return a + 1; }(3,4));
157 util->check_result(result_stream, "4");
159 //std::cout << "(_2 + 1) (3,4): " << (_2 + 1) (3,4) << std::endl;
160 result_stream << ([] (int, int b) -> int { return b + 1; }(3,4));
161 util->check_result(result_stream, "5");
163 //std::cout << "(2 + _1) (3,4): " << (2 + _1) (3,4) << std::endl;
164 result_stream << ([] (int a, int) -> int { return 2 + a; }(3,4));
165 util->check_result(result_stream, "5");
167 //std::cout << "(2 + _2) (3,4): " << (2 + _2) (3,4) << std::endl;
168 result_stream << ([] (int, int b) -> int { return 2 + b; }(3,4));
169 util->check_result(result_stream, "6");
171 //std::cout << "(_1+_2*_3)(1,2,3): " << (_1+_2*_3)(1,2,3) << std::endl;
172 result_stream << ([] (int a, int b, int c) -> int { return a + b*c; }(1,2,3));
173 util->check_result(result_stream, "7");
175 //std::cout << "((++_1)*2)(1): " << ((++_1)*2)(1) << std::endl;
176 result_stream << ([] (int a) -> int { return ++a * 2; }(1));
177 util->check_result(result_stream, "4");
179 //std::cout << "((++_1)*2)(a): " << ((++_1)*2)(a);
180 //std::cout << "; a: " << a << std::endl;
182 result_stream << ([] (int x) -> int { return ++x * 2; }(a_outer)) << " " << a_outer;
183 util->check_result(result_stream, "4 1");
185 // gcc can't compile libsigc++ lambda expressions with std::ref() parameters.
186 // See https://bugzilla.gnome.org/show_bug.cgi?id=669128
187 // std::cout << "((++_1)*2)(ref(a)): " << ((++_1)*2)(std::ref(a));
188 // std::cout << "; a: " << a << std::endl;
189 result_stream << ([] (std::reference_wrapper<int> x) -> int { return ++x * 2; }(std::ref(a_outer)));
190 result_stream << " " << a_outer;
191 util->check_result(result_stream, "4 2");
192 result_stream << ([] (int& x) -> int { return ++x * 2; }(a_outer));
193 result_stream << " " << a_outer;
194 util->check_result(result_stream, "6 3");
196 //std::cout << "((++(*_1))*2)(&a): " << ((++(*_1))*2)(&a);
197 //std::cout << "; a: " << a << std::endl;
198 result_stream << ([] (int* x) -> int { return ++(*x) * 2; }(&a_outer));
199 result_stream << " " << a_outer;
200 util->check_result(result_stream, "8 4");
202 // std::cout << "((--(*(&_1)))*2)(ref(a)): " << ((--(*(&_1)))*2)(std::ref(a));
203 // std::cout << "; a: " << a << std::endl;
204 result_stream << ([] (std::reference_wrapper<int> x) -> int { return --(*(&x)) * 2; }(std::ref(a_outer)));
205 result_stream << " " << a_outer;
206 util->check_result(result_stream, "6 3");
207 result_stream << ([] (int& x) -> int { return --(*(&x)) * 2; }(a_outer));
208 result_stream << " " << a_outer;
209 util->check_result(result_stream, "4 2");
211 //std::cout << "(-_1) (-5): " << (-_1) (-5) << std::endl;
212 result_stream << ([] (int x) -> int { return -x; }(-5));
213 util->check_result(result_stream, "5");
215 //std::cout << "(var(&a)[0])(): " << (sigc::var(&a)[0])() << std::endl;
216 result_stream << ([&a_outer]() -> int { return a_outer; }());
217 util->check_result(result_stream, "2");
219 //std::cout << "(_1[_2]) (&a,0): " << (_1[_2]) (&a,0) << std::endl;
220 result_stream << ([] (int* x, int y) -> int { return x[y]; }(&a_outer,0));
221 util->check_result(result_stream, "2");
223 //std::cout << "(*_1=_2) (&a,1): " << (*_1=_2) (&a,1) << std::endl;
224 result_stream << ([] (int* x, int y) -> int { *x = y; return *x; }(&a_outer,1));
225 util->check_result(result_stream, "1");
227 // Comma operator, https://bugzilla.gnome.org/show_bug.cgi?id=342911
231 //std::cout << "(var(c) = (var(a) = _1, var(b) = _2))(2,3): "
232 // << (sigc::var(c) = (sigc::var(a) = _1, sigc::var(b) = _2))(2,3);
233 //std::cout << "; a: " << a << "; b: " << b << "; c: " << c << std::endl;
234 result_stream << ([&a_outer,&b_outer,&c_outer](int x, int y) -> int { return c_outer = (a_outer = x, b_outer = y); }(2,3));
235 result_stream << " " << a_outer << " " << b_outer << " " << c_outer;
236 util->check_result(result_stream, "3 2 3 3");
239 // - ref() must be used to indicate that the value shall not be copied
240 // - constant() is used to create a lambda and delay execution of "std::cout << 1"
241 // - var() is used to create a lambda that holds a reference and is interchangable with ref() in lambda operator expressions
242 // - cannot use std::endl without much hackery because it is defined as a template function
243 // - cannot use "\n" without var() because arrays cannot be copied
244 // (std::ref(std::cout) << sigc::constant(1) << sigc::var("\n"))();
245 [](){ result_stream << 1 << "\n"; }();
246 util->check_result(result_stream, "1\n");
248 //(std::ref(std::cout) << _1 << std::string("\n"))("hello world");
249 [](const char* a){ result_stream << a << std::string("\n"); }("hello world");
250 util->check_result(result_stream, "hello world\n");
252 //(std::ref(std::cout) << sigc::static_cast_<int>(_1) << std::string("\n"))(1.234);
253 [](double a){ result_stream << static_cast<int>(a) << std::string("\n"); }(1.234);
254 util->check_result(result_stream, "1\n");
256 // (sigc::var(std::cout) << 1 << sigc::var("\n"))();
257 [](){ result_stream << 1 << "\n"; }();
258 util->check_result(result_stream, "1\n");
260 //(sigc::var(std::cout) << _1 << std::string("\n"))("hello world");
261 [](const char* a){ result_stream << a << std::string("\n"); }("hello world");
262 util->check_result(result_stream, "hello world\n");
265 // Here's an area where the libsigc++ lambda expressions are advantageous.
266 // If you want to auto-disconnect a slot with a C++11 lambda expression
267 // that contains references to sigc::trackable-derived objects, you must use
268 // sigc::track_obj().
269 sigc::slot<void, std::ostringstream&> sl1;
271 book guest_book("karl");
272 //sl1 = (sigc::var(std::cout) << std::ref(guest_book) << sigc::var("\n"));
273 // sl1 = [&guest_book](std::ostringstream& stream){ stream << guest_book << "\n"; }; // no auto-disconnect
274 sl1 = sigc::track_obj([&guest_book](std::ostringstream& stream){ stream << guest_book << "\n"; }, guest_book);
276 util->check_result(result_stream, "karl\n");
281 util->check_result(result_stream, "");
283 // test group adaptor, here replaced by std::bind
285 //std::cout << (sigc::group(&foo, _1, _2)) (1, 2) << std::endl;
286 result_stream << std::bind(&foo, std::placeholders::_1, std::placeholders::_2)(1, 2);
287 util->check_result(result_stream, "foo(int 1, int 2) 6");
289 //std::cout << (sigc::group(&foo, _2, _1)) (1, 2) << std::endl;
290 result_stream << std::bind(&foo, std::placeholders::_2, std::placeholders::_1)(1, 2);
291 util->check_result(result_stream, "foo(int 2, int 1) 9");
293 //std::cout << (sigc::group(sigc::mem_fun(&bar::test), _1, _2, _3)) (std::ref(the_bar), 1, 2) << std::endl;
294 // std::ref(the_bar) is not necessary. It can make the call ambiguous.
295 // Even without std::ref() the_bar is not copied.
296 result_stream << std::bind(std::mem_fn(&bar::test), std::placeholders::_1,
297 std::placeholders::_2, std::placeholders::_3)(the_bar, 1, 2);
298 util->check_result(result_stream, "bar::test(int 1, int 2) 6");
300 // same functionality as bind
301 //std::cout << (sigc::group(&foo, _1, 2)) (1) << std::endl;
302 result_stream << std::bind(&foo, std::placeholders::_1, 2)(1);
303 util->check_result(result_stream, "foo(int 1, int 2) 6");
305 //std::cout << (sigc::group(&foo, 1, 2)) () << std::endl;
306 result_stream << std::bind(&foo, 1, 2)();
307 util->check_result(result_stream, "foo(int 1, int 2) 6");
309 //(sigc::group(sigc::ptr_fun(&foo_void), 1)) ();
310 std::bind(sigc::ptr_fun(&foo_void), 1)();
311 util->check_result(result_stream, "foo_void(int 1)");
313 // std::bind() does not work well together with sigc::slot and sigc::signal::connect().
314 // std::bind() typically creates a functor whose operator()() is a variadic template.
315 // Our functor_trait can't deduce the result type of such a functor.
316 // If the result of std::bind() is assigned to a std::function, the created
317 // functor has an unambiguous operator()().
320 sigc::slot<void> sl2;
322 book guest_book("karl");
323 //sl2 = sigc::group(&egon, std::ref(guest_book));
324 // sl2 = [&guest_book] () { egon(guest_book); }; // no auto-disconnect
325 // sl2 = std::bind(&egon, std::ref(guest_book)); // does not compile (gcc 4.6.3)
326 sl2 = sigc::track_obj([&guest_book] () { egon(guest_book); }, guest_book);
328 util->check_result(result_stream, "egon(string 'karl')");
330 //std::cout << static_cast<std::string&>(guest_book) << std::endl;
331 result_stream << static_cast<std::string&>(guest_book);
332 util->check_result(result_stream, "egon was here");
337 util->check_result(result_stream, "");
339 // More auto-disconnect
341 book guest_book("charlie");
342 //sl2 = sigc::group(&egon, std::ref(guest_book));
343 // sl2 = std::bind(&egon, std::ref(guest_book)); // does not compile (gcc 4.6.3)
344 auto fn2 = std::bind(&egon, std::ref(guest_book));
345 //sl2 = fn2; // no auto-disconnect
346 sl2 = sigc::track_obj(fn2, guest_book);
348 util->check_result(result_stream, "egon(string 'charlie')");
350 //std::cout << static_cast<std::string&>(guest_book) << std::endl;
351 result_stream << static_cast<std::string&>(guest_book);
352 util->check_result(result_stream, "egon was here");
357 util->check_result(result_stream, "");
359 // same functionality as hide
360 //std::cout << (sigc::group(&foo, _1, _2)) (1,2,3) << std::endl;
361 result_stream << std::bind(&foo, std::placeholders::_1, std::placeholders::_2)(1,2,3);
362 util->check_result(result_stream, "foo(int 1, int 2) 6");
364 //(sigc::group(sigc::ptr_fun(&foo_void), _2)) (1, 2);
365 std::bind(&foo_void, std::placeholders::_2)(1, 2);
366 util->check_result(result_stream, "foo_void(int 2)");
368 // same functionality as compose
369 //std::cout << (sigc::group(&foo, sigc::group(&foo, _1, _2), _3)) (1,2,3) << std::endl;
370 result_stream << std::bind(&foo, std::bind(&foo, std::placeholders::_1, std::placeholders::_2),
371 std::placeholders::_3)(1,2,3);
372 util->check_result(result_stream, "foo(int 1, int 2) foo(int 6, int 3) 27");
374 // same functionality as retype
375 //std::cout << (sigc::group(&foo, sigc::static_cast_<int>(_1), 2)) (1.234) << std::endl;
376 result_stream << ([] (double x) -> int { return foo(static_cast<int>(x), 2); }(1.234));
377 util->check_result(result_stream, "foo(int 1, int 2) 6");
379 // Code examples with C++11 lambda expressions and std::bind, which can replace
380 // libsigc++ examples in the documentation of libsigc++ lambdas and sigc::group.
381 // -----------------------------------------------------------------------------
383 //--- sigc++/adaptors/lambda/macros/base.h.m4
385 //std::cout << sigc::_1(10,20,30); // returns 10
386 result_stream << ([] (int x, int, int) -> int { return x; }(10,20,30));
387 util->check_result(result_stream, "10");
389 //std::cout << sigc::_2(10,20,30); // returns 20
390 result_stream << ([] (int, int y, int) -> int { return y; }(10,20,30));
391 util->check_result(result_stream, "20");
393 //std::cout << (sigc::_1 + 5)(3); // returns (3 + 5)
394 result_stream << ([] (int x) -> int { return x + 5; }(3));
395 util->check_result(result_stream, "8");
397 //std::cout << (sigc::_1 * sigc::_2)(7,10); // returns (7 * 10)
398 result_stream << ([] (int x, int y) -> int { return x * y; }(7,10));
399 util->check_result(result_stream, "70");
401 //int main(int argc, char* argv[])
404 // sigc::signal<int> readValue;
406 // readValue.connect(sigc::var(data));
409 // std::cout << readValue() << std::endl; //Prints 3.
412 // std::cout << readValue() << std::endl; //Prints 5.
416 sigc::signal<int> readValue;
418 readValue.connect([&data] () -> int { return data; });
421 result_stream << readValue();
422 util->check_result(result_stream, "3");
425 result_stream << readValue();
426 util->check_result(result_stream, "5");
429 //--- sigc++/adaptors/lambda/macros/group.h.m4
431 // argument binding ...
432 //sigc::group(&foo,10,sigc::_1)(20); //fixes the first argument and calls foo(10,20)
433 std::bind(&foo_group1, 10, std::placeholders::_1)(20);
434 util->check_result(result_stream, "foo_group1(int 10, int 20)");
436 //sigc::group(&foo,sigc::_1,30)(40); //fixes the second argument and calls foo(40,30)
437 std::bind(&foo_group1, std::placeholders::_1, 30)(40);
438 util->check_result(result_stream, "foo_group1(int 40, int 30)");
440 // argument reordering ...
441 //sigc::group(&foo,sigc::_2,sigc::_1)(1,2); //calls foo(2,1)
442 std::bind(&foo_group1, std::placeholders::_2, std::placeholders::_1)(1,2);
443 util->check_result(result_stream, "foo_group1(int 2, int 1)");
445 // argument hiding ...
446 //sigc::group(&foo,sigc::_1,sigc::_2)(1,2,3); //calls foo(1,2)
447 std::bind(&foo_group1, std::placeholders::_1, std::placeholders::_2)(1,2,3);
448 util->check_result(result_stream, "foo_group1(int 1, int 2)");
450 // functor composition ...
451 //sigc::group(&foo,sigc::_1,sigc::group(&bar,sigc::_2))(1,2); //calls foo(1,bar(2))
452 std::bind(&foo_group1, std::placeholders::_1, std::bind(&bar_group1, std::placeholders::_2))(1,2);
453 util->check_result(result_stream, "bar_group1(int 2) foo_group1(int 1, int 4)");
455 // algebraic expressions ...
456 // sigc::group(&foo,sigc::_1*sigc::_2,sigc::_1/sigc::_2)(6,3); //calls foo(6*3,6/3)
457 [] (int x, int y) { foo_group1(x*y, x/y); }(6,3);
458 util->check_result(result_stream, "foo_group1(int 18, int 2)");
461 sigc::signal<void,int,int> some_signal;
462 //some_signal.connect(sigc::group(&foo,sigc::_2));
463 //some_signal.connect(std::bind(&foo_group2, std::placeholders::_2)); // does not compile (gcc 4.6.3)
464 some_signal.connect([](int, int y) { foo_group2(y); });
465 some_signal.emit(1,2);
466 util->check_result(result_stream, "foo_group2(int 2)");
471 sigc::signal<void> some_signal;
472 //some_signal.connect(sigc::group(&foo,std::ref(some_int)));
473 //some_signal.connect(std::bind(&foo_group3, std::ref(some_int))); // does not compile (gcc 4.6.3)
474 //some_signal.connect(sigc::bind(&foo_group3, std::ref(some_int))); // compiles, but we prefer std::bind() or C++11 lambda
475 some_signal.connect([&some_int](){ foo_group3(some_int); });
477 result_stream << " " << some_int;
478 util->check_result(result_stream, "foo_group3(int 0) 1");
482 //struct bar : public sigc::trackable {} some_bar;
483 sigc::signal<void> some_signal;
486 //some_signal.connect(sigc::group(&foo,std::ref(some_bar)));
487 // disconnected automatically if some_bar goes out of scope
488 //some_signal.connect([&some_bar](){ foo_group4(some_bar); }); // no auto-disconnect
489 //some_signal.connect(sigc::bind(&foo_group4, std::ref(some_bar))); // auto-disconnects, but we prefer C++11 lambda
490 some_signal.connect(sigc::track_obj([&some_bar](){ foo_group4(some_bar); }, some_bar));
492 util->check_result(result_stream, "foo_group4(bar_group4&)");
495 util->check_result(result_stream, "");
498 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;