2 /* Copyright 2002, The libsigc++ Development Team
3 * Assigned to public domain. Use as you wish without restriction.
6 #include "testutilities.h"
7 #include <sigc++/adaptors/bind_return.h>
8 #include <sigc++/functors/slot.h>
15 std::ostringstream result_stream;
19 void operator()(int i)
21 result_stream << "foo(int " << i << ") ";
24 float operator()(float i)
26 result_stream << "foo(float " << i << ") ";
31 struct bar : public sigc::trackable
33 bar(int i = 0) : i_(i) {}
34 operator int() { return i_; }
38 } // end anonymous namespace
40 int main(int argc, char* argv[])
42 auto util = TestUtilities::get_instance();
44 if (!util->check_command_args(argc, argv))
45 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
47 result_stream << sigc::bind_return(foo(), -12345)(5);
48 util->check_result(result_stream, "foo(int 5) -12345");
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
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";
58 util->check_result(result_stream, "foo(int 6) main");
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.
66 // Therefore, we use 'bar', and not 'bar&' for this slot signature.
67 sigc::slot<bar, int> sl;
70 sl = sigc::bind_return(foo(),std::ref(choco));
71 result_stream << sl(7);
72 util->check_result(result_stream, "foo(int 7) -1");
75 result_stream << sl(8);
76 util->check_result(result_stream, "0");
78 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;