1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/* Copyright 2002 - 2016, The libsigc++ Development Team
* Assigned to public domain. Use as you wish without restriction.
*/
#include "testutilities.h"
#include <sigc++/trackable.h>
#include <sigc++/signal.h>
#include <memory>
namespace
{
TestUtilities* util = nullptr;
std::ostringstream result_stream;
int
foo(int i)
{
result_stream << "foo(int " << i << ") ";
return 1;
}
struct A : public sigc::trackable
{
int foo(int i)
{
result_stream << "A::foo(int " << i << ") ";
return 1;
}
void bar(std::string& str)
{
result_stream << "A::foo(string '" << str << "') ";
str = "foo was here";
}
};
void
test_empty_signal()
{
// signal
sigc::signal<int(int)> sig;
// emit empty signal
sig(0);
util->check_result(result_stream, "");
}
void
test_simple()
{
sigc::signal<int(int)> sig;
sig.connect([](int i) { return foo(i); });
sig(1);
util->check_result(result_stream, "foo(int 1) ");
}
int
bar(float i)
{
result_stream << "bar(float " << i << ") ";
return 1;
}
template<typename T_signal>
void
test_auto_disconnection()
{
// signal
T_signal sig;
// connect some slots before emitting & test auto-disconnection
{
A a;
sig.connect(sigc::ptr_fun(&foo));
sig.connect_first(sigc::mem_fun(a, &A::foo));
sig.connect_first(sigc::ptr_fun(&bar));
sig(1);
result_stream << sig.size();
util->check_result(result_stream, "bar(float 1) A::foo(int 1) foo(int 1) 3");
} // a dies => auto-disconnect
sig(2);
result_stream << sig.size();
util->check_result(result_stream, "bar(float 2) foo(int 2) 2");
}
void
test_reference()
{
// test reference
A a;
std::string str("guest book");
sigc::signal<void(std::string&)> sigstr;
sigstr.connect(sigc::mem_fun(a, &A::bar));
sigstr(str);
result_stream << str;
util->check_result(result_stream, "A::foo(string 'guest book') foo was here");
}
void
test_make_slot()
{
// test make_slot()
sigc::signal<int(int)> sig;
sig.connect(sigc::ptr_fun(&foo));
sig.connect_first([](float i) { return bar(i); });
sig.connect(sigc::ptr_fun(&foo));
sigc::signal<int(int)> sig2;
sig2.connect(sig.make_slot());
sig2(3);
util->check_result(result_stream, "bar(float 3) foo(int 3) foo(int 3) ");
// Delete a trackable_signal that has been connected to sig2.
sig2.clear();
sig2.connect(sigc::ptr_fun(&bar));
auto sig3 = std::make_unique<sigc::trackable_signal<int(int)>>();
sig3->connect(sigc::ptr_fun(&foo));
sig2.connect(sig3->make_slot());
sig2(2);
sig3.reset();
sig2(42);
util->check_result(result_stream, "bar(float 2) foo(int 2) bar(float 42) ");
}
void
test_clear_called_in_signal_handler()
{
sigc::signal<void()> sig;
sig.connect([]() { result_stream << ", slot 1, "; });
sig.connect(
[&sig]()
{
sig.clear();
result_stream << "slot 2, ";
});
sig.connect([]() { result_stream << "slot 3, "; });
result_stream << sig.size();
sig.emit();
result_stream << sig.size();
sig.emit();
util->check_result(result_stream, "3, slot 1, slot 2, 0");
}
void
test_clear_called_outside_signal_handler()
{
sigc::signal<void()> sig;
sig.connect([]() { result_stream << ", slot 1, "; });
sig.connect([]() { result_stream << "slot 2, "; });
sig.connect([]() { result_stream << "slot 3, "; });
result_stream << sig.size();
sig.emit();
sig.clear();
result_stream << sig.size();
sig.emit();
util->check_result(result_stream, "3, slot 1, slot 2, slot 3, 0");
}
} // end anonymous namespace
int
main(int argc, char* argv[])
{
util = TestUtilities::get_instance();
if (!util->check_command_args(argc, argv))
return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
test_empty_signal();
test_simple();
test_auto_disconnection<sigc::signal<int(int)>>();
test_auto_disconnection<sigc::trackable_signal<int(int)>>();
test_reference();
test_make_slot();
test_clear_called_in_signal_handler();
test_clear_called_outside_signal_handler();
return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
}
|