]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_bind_return.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / test_bind_return.cc
1 // -*- c++ -*-
2 /* Copyright 2002, The libsigc++ Development Team
3  *  Assigned to public domain.  Use as you wish without restriction.
4  */
5
6 #include "testutilities.h"
7 #include <sigc++/adaptors/bind_return.h>
8 #include <sigc++/functors/slot.h>
9 #include <sstream>
10 #include <string>
11 #include <cstdlib>
12
13 namespace
14 {
15 std::ostringstream result_stream;
16
17 struct foo
18 {
19   void operator()(int i)
20   {
21     result_stream << "foo(int " << i << ") ";
22   }
23
24   float operator()(float i)
25   {
26     result_stream << "foo(float " << i << ") ";
27     return i*5;
28   }
29 };
30
31 struct bar : public sigc::trackable
32 {
33   bar(int i = 0) : i_(i) {}
34   operator int() { return i_; }
35   int i_;
36 };
37
38 } // end anonymous namespace
39
40 int main(int argc, char* argv[])
41 {
42   auto util = TestUtilities::get_instance();
43
44   if (!util->check_command_args(argc, argv))
45     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
46
47   result_stream << sigc::bind_return(foo(), -12345)(5);
48   util->check_result(result_stream, "foo(int 5) -12345");
49
50   // Here we just build a functor, not a slot. There is no such thing as a
51   // default functor, or an invalidated functor. As such, functors can return
52   // references.
53   std::string str("guest book");
54   // A convoluted way to do
55   // sigc::bind_return(foo(), std::ref(str))(6) = "main";
56   sigc::bind_return<std::reference_wrapper<std::string> >(foo(), std::ref(str))(6) = "main";
57   result_stream << str;
58   util->check_result(result_stream, "foo(int 6) main");
59
60   // Here we build a slot (constructed from a functor). Slots cannot return
61   // references: if they could, then what would be the return value of the
62   // default slot or of an invalidated slot? On the other hand, slots are
63   // guaranteed to be able to construct and return a valid default instance as
64   // long as there exists a default constructor for it.
65   //
66   // Therefore, we use 'bar', and not 'bar&' for this slot signature.
67   sigc::slot<bar, int> sl;
68   {
69     bar choco(-1);
70     sl = sigc::bind_return(foo(),std::ref(choco));
71     result_stream << sl(7);
72     util->check_result(result_stream, "foo(int 7) -1");
73   } // auto-disconnect
74
75   result_stream << sl(8);
76   util->check_result(result_stream, "0");
77
78   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
79 }