]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_cpp11_lambda.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / test_cpp11_lambda.cc
1 /* Copyright (C) 2012 The libsigc++ Development Team
2  *
3  * This file is part of libsigc++.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 // The purpose of this test case is threefold.
20 // - Test that C++11 lambda expressions can be used in connection with sigc::slot
21 //   and sigc::signal.
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.
28 //
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.
37 //
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
42 //   echo $?
43 // If test_cpp11_lambda writes nothing and the return code is 0, the test has passed.
44
45
46 #include "testutilities.h"
47 #include <string>
48 #include <iostream>
49 #include <sstream>
50 #include <functional>
51 #include <cstdlib>
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>
57
58
59 namespace
60 {
61 std::ostringstream result_stream;
62
63 int foo(int i, int j)
64 {
65   result_stream << "foo(int " << i << ", int " << j << ") ";
66   return 4*i + j;
67 }
68
69 void foo_void(int i)
70 {
71   result_stream << "foo_void(int " << i << ")";
72 }
73
74 struct bar
75 {
76   int test(int i, int j)
77   {
78     result_stream << "bar::test(int " << i << ", int " << j << ") ";
79     return 4*i + j;
80   }
81
82   void test_void(int i)
83   {
84     result_stream << "bar::test_void(int " << i << ")";
85   }
86 };
87
88 void egon(std::string& str)
89 {
90   result_stream << "egon(string '" << str << "')";
91   str = "egon was here";
92 }
93
94 struct book : public sigc::trackable
95 {
96   explicit book(const std::string& name) : name_(name) {}
97   operator std::string& () { return name_; }
98   std::string name_;
99 };
100
101 inline std::ostringstream& operator << (std::ostringstream& s, const book& b)
102 {
103   s << b.name_;
104   return s;
105 }
106
107 void foo_group1(int i, int j)
108 {
109   result_stream << "foo_group1(int " << i << ", int " << j << ")";
110 }
111
112 int bar_group1(int i)
113 {
114   result_stream << "bar_group1(int " << i << ") ";
115   return i + 2;
116 }
117
118 void foo_group2(int i)
119 {
120   result_stream << "foo_group2(int " << i << ")";
121 }
122
123 void foo_group3(int& i)
124 {
125   result_stream << "foo_group3(int " << i << ")";
126   ++i;
127 }
128
129 struct bar_group4 : public sigc::trackable
130 {
131 };
132
133 void foo_group4(bar_group4&)
134 {
135   result_stream << "foo_group4(bar_group4&)";
136 }
137
138 } // end anonymous namespace
139
140
141
142 int main(int argc, char* argv[])
143 {
144   auto util = TestUtilities::get_instance();
145
146   if (!util->check_command_args(argc, argv))
147     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
148
149
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");
154
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");
158
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");
162
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");
166
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");
170
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");
174
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");
178
179   //std::cout << "((++_1)*2)(a):      " << ((++_1)*2)(a);
180   //std::cout << "; a: "                << a                    << std::endl;
181   int a_outer = 1;
182   result_stream << ([] (int x) -> int { return ++x * 2; }(a_outer)) << " " << a_outer;
183   util->check_result(result_stream, "4 1");
184
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");
195
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");
201
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");
210
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");
214
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");
218
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");
222
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");
226
227   // Comma operator, https://bugzilla.gnome.org/show_bug.cgi?id=342911
228   a_outer = -1;
229   int b_outer = -1;
230   int c_outer = -1;
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");
237
238   // c++ restrictions:
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");
247
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");
251
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");
255
256   //  (sigc::var(std::cout) << 1 << sigc::var("\n"))();
257   [](){ result_stream << 1 << "\n"; }();
258   util->check_result(result_stream, "1\n");
259
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");
263
264   // auto-disconnect
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;
270   {
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);
275     sl1(result_stream);
276     util->check_result(result_stream, "karl\n");
277
278   } // auto-disconnect
279
280   sl1(result_stream);
281   util->check_result(result_stream, "");
282
283   // test group adaptor, here replaced by std::bind
284   bar the_bar;
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");
288
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");
292
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");
299
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");
304
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");
308
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)");
312
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()().
318
319   // auto-disconnect
320   sigc::slot<void> sl2;
321   {
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);
327     sl2();
328     util->check_result(result_stream, "egon(string 'karl')");
329
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");
333
334   } // auto-disconnect
335
336   sl2();
337   util->check_result(result_stream, "");
338
339   // More auto-disconnect
340   {
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);
347     sl2();
348     util->check_result(result_stream, "egon(string 'charlie')");
349
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");
353
354   } // auto-disconnect
355
356   sl2();
357   util->check_result(result_stream, "");
358
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");
363
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)");
367
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");
373
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");
378
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   // -----------------------------------------------------------------------------
382
383   //--- sigc++/adaptors/lambda/macros/base.h.m4
384
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");
388
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");
392
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");
396
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");
400
401   //int main(int argc, char* argv[])
402   //{
403   //  int data;
404   //  sigc::signal<int> readValue;
405   //
406   //  readValue.connect(sigc::var(data));
407   //
408   //  data = 3;
409   //  std::cout << readValue() << std::endl; //Prints 3.
410   //
411   //  data = 5;
412   //  std::cout << readValue() << std::endl; //Prints 5.
413   //}
414   {
415     int data;
416     sigc::signal<int> readValue;
417
418     readValue.connect([&data] () -> int { return data; });
419
420     data = 3;
421     result_stream << readValue();
422     util->check_result(result_stream, "3");
423
424     data = 5;
425     result_stream << readValue();
426     util->check_result(result_stream, "5");
427   }
428
429   //--- sigc++/adaptors/lambda/macros/group.h.m4
430
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)");
435
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)");
439
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)");
444
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)");
449
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)");
454
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)");
459
460   {
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)");
467   }
468
469   {
470     int some_int = 0;
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); });
476     some_signal.emit();
477     result_stream << " " << some_int;
478     util->check_result(result_stream, "foo_group3(int 0) 1");
479   }
480
481   {
482     //struct bar : public sigc::trackable {} some_bar;
483     sigc::signal<void> some_signal;
484     {
485       bar_group4 some_bar;
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));
491       some_signal.emit();
492       util->check_result(result_stream, "foo_group4(bar_group4&)");
493     }
494     some_signal.emit();
495     util->check_result(result_stream, "");
496   }
497
498   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
499 } // end main()