]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_bind_ref.cc
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / tests / test_bind_ref.cc
1 #include "testutilities.h"
2 #include <sigc++/sigc++.h>
3 #include <sstream>
4 #include <string>
5 #include <cstdlib>
6
7 namespace
8 {
9 std::ostringstream result_stream;
10
11 class Param : public sigc::trackable
12 {
13 public:
14   Param(const std::string& name)
15   : name_(name)
16   {}
17
18   //non-copyable,
19   //so it can only be used with sigc::bind() via std::ref()
20   Param(const Param&) = delete;
21   Param& operator=(const Param&) = delete;
22
23   //non movable:
24   Param(Param&&) = delete;
25   Param& operator=(Param&&) = delete;
26
27   std::string name_;
28 };
29
30 void handler(Param& param)
31 {
32   result_stream << "  handler(param): param.name_=" << param.name_;
33 }
34
35 } // end anonymous namespace
36
37 int main(int argc, char* argv[])
38 {
39   auto util = TestUtilities::get_instance();
40
41   if (!util->check_command_args(argc, argv))
42     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
43
44   auto slot_full = sigc::ptr_fun(&handler);
45   sigc::slot<void> slot_bound;
46
47   slot_bound();
48   util->check_result(result_stream, "");
49
50   {
51     //Because Param derives from sigc::trackable(), std::ref() should disconnect
52     // the signal handler when param is destroyed.
53     Param param("murrayc");
54     // A convoluted way to do
55     // slot_bound = sigc::bind(slot_full, std::ref(param));
56     slot_bound = sigc::bind< -1, std::reference_wrapper<Param> >(slot_full, std::ref(param));
57
58     result_stream << "Calling slot when param exists:";
59     slot_bound();
60     util->check_result(result_stream,
61       "Calling slot when param exists:  handler(param): param.name_=murrayc");
62   } // auto-disconnect
63
64   result_stream << "Calling slot when param does not exist:";
65   slot_bound();
66   util->check_result(result_stream, "Calling slot when param does not exist:");
67   // This causes a crash when using g++ 3.3.4 or 3.3.5 (but not 3.4.x) when not specifying
68   // the exact template specialization in visit_each_type() - see the comments there.
69   // It looks like the auto-disconnect does not work, so the last slot_bound() call tries
70   // to access the param data again.
71
72   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
73 }