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.h>
8 #include <sigc++/functors/slot.h>
15 std::ostringstream result_stream;
17 struct foo : public sigc::functor_base
19 // choose a type that can hold all return values
20 typedef int result_type;
24 result_stream << "foo(int " << i << ") ";
28 int operator()(int i, int j)
30 result_stream << "foo(int " << i << ", int "<< j << ") ";
34 int operator()(int i, int j, int k)
36 result_stream << "foo(int " << i << ", int " << j << ", int " << k << ") ";
41 struct foo_void : public sigc::functor_base
43 typedef void result_type;
45 void operator()(int i)
47 result_stream << "foo_void(int " << i << ")";
53 result_stream << "bar(int " << i << ", int " << j << ") ";
57 bool simple(bool test)
59 result_stream << "simple(bool " << test << ") ";
63 void egon(std::string& str)
65 result_stream << "egon(string '" << str << "')";
66 str = "egon was here";
70 struct book : public sigc::trackable
72 book(const std::string& name) : name_(name) {}
75 book(const book&) = delete;
76 book& operator=(const book&) = delete;
79 book(book&&) = delete;
80 book& operator=(book&&) = delete;
82 std::string& get_name() { return name_; }
83 operator std::string& () { return get_name(); }
89 } // end anonymous namespace
91 int main(int argc, char* argv[])
93 auto util = TestUtilities::get_instance();
95 if (!util->check_command_args(argc, argv))
96 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
98 // replacing bind1st, bind2nd
99 result_stream << sigc::bind<0>(foo(), -12345)(5);
100 util->check_result(result_stream, "foo(int -12345, int 5) -12340");
102 result_stream << sigc::bind<1>(foo(), -12345)(5);
103 util->check_result(result_stream, "foo(int 5, int -12345) -12340");
106 sigc::bind(foo(), 1, 2)();
107 util->check_result(result_stream, "foo(int 1, int 2) ");
110 sigc::bind<-1>(foo(), 4)(3);
111 util->check_result(result_stream, "foo(int 3, int 4) ");
113 sigc::bind(foo(), 4)(3);
114 util->check_result(result_stream, "foo(int 3, int 4) ");
117 sigc::bind<0>(sigc::bind<0>(foo(), 7), 8)();
118 util->check_result(result_stream, "foo(int 7, int 8) ");
121 sigc::bind(foo(), 9, 10)(11); // (only returned void if typeof() would be supported)
122 util->check_result(result_stream, "foo(int 11, int 9, int 10) ");
124 sigc::bind(foo_void(), 12)();
125 util->check_result(result_stream, "foo_void(int 12)");
127 // function pointer instead of functor
128 sigc::bind(&bar, 13, 14)();
129 util->check_result(result_stream, "bar(int 13, int 14) ");
131 // method pointer instead of functor
132 book test_book("otto");
133 result_stream << sigc::bind<0>(&book::get_name, std::ref(test_book))();
134 util->check_result(result_stream, "otto");
136 // test return type of bind_functor::operator() overload with no arguments
137 result_stream << sigc::bind(foo(), 15)();
138 util->check_result(result_stream, "foo(int 15) 1");
140 result_stream << sigc::bind(&simple, true)();
141 util->check_result(result_stream, "simple(bool 1) 1");
144 std::string str("guest book");
145 sigc::bind(&egon, std::ref(str))(); // Tell bind that it shall store a reference.
146 result_stream << " " << str; // (This cannot be the default behaviour: just think about what happens if str dies!)
147 util->check_result(result_stream, "egon(string 'guest book') egon was here");
151 book guest_book("karl");
152 sl = sigc::bind(&egon, std::ref(guest_book));
154 result_stream << " " << static_cast<std::string&>(guest_book);
155 util->check_result(result_stream, "egon(string 'karl') egon was here");
159 util->check_result(result_stream, "");
160 // This causes a crash when using g++ 3.3.4 or 3.3.5 (but not 3.4.x) when not specifying
161 // the exact template specialization in visit_each_type() - see the comments there.
162 // It looks like the auto-disconnect does not work, so the last sl() call tries
163 // to access the guest_book data again.
165 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;