| 1 | // Boost.Signals library |
|---|
| 2 | |
|---|
| 3 | // Copyright Douglas Gregor 2002-2004. Use, modification and |
|---|
| 4 | // distribution is subject to the Boost Software License, Version |
|---|
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | |
|---|
| 8 | // For more information, see http://www.boost.org |
|---|
| 9 | |
|---|
| 10 | #include <boost/test/minimal.hpp> |
|---|
| 11 | #include <boost/signal.hpp> |
|---|
| 12 | #include <iostream> |
|---|
| 13 | #include <vector> |
|---|
| 14 | #include <algorithm> |
|---|
| 15 | #include <cstdlib> |
|---|
| 16 | #include <ctime> |
|---|
| 17 | |
|---|
| 18 | std::vector<int> valuesOutput; |
|---|
| 19 | bool ungrouped1 = false; |
|---|
| 20 | bool ungrouped2 = false; |
|---|
| 21 | bool ungrouped3 = false; |
|---|
| 22 | |
|---|
| 23 | struct emit_int { |
|---|
| 24 | emit_int(int v) : value(v) {} |
|---|
| 25 | |
|---|
| 26 | void operator()() const |
|---|
| 27 | { |
|---|
| 28 | BOOST_CHECK(value == 42 || (!ungrouped1 && !ungrouped2 && !ungrouped3)); |
|---|
| 29 | valuesOutput.push_back(value); |
|---|
| 30 | std::cout << value << ' '; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | private: |
|---|
| 34 | int value; |
|---|
| 35 | }; |
|---|
| 36 | |
|---|
| 37 | struct write_ungrouped1 { |
|---|
| 38 | void operator()() const |
|---|
| 39 | { |
|---|
| 40 | BOOST_CHECK(!ungrouped1); |
|---|
| 41 | ungrouped1 = true; |
|---|
| 42 | std::cout << "(Ungrouped #1)" << ' '; |
|---|
| 43 | } |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | struct write_ungrouped2 { |
|---|
| 47 | void operator()() const |
|---|
| 48 | { |
|---|
| 49 | BOOST_CHECK(!ungrouped2); |
|---|
| 50 | ungrouped2 = true; |
|---|
| 51 | std::cout << "(Ungrouped #2)" << ' '; |
|---|
| 52 | } |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | struct write_ungrouped3 { |
|---|
| 56 | void operator()() const |
|---|
| 57 | { |
|---|
| 58 | BOOST_CHECK(!ungrouped3); |
|---|
| 59 | ungrouped3 = true; |
|---|
| 60 | std::cout << "(Ungrouped #3)" << ' '; |
|---|
| 61 | } |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | int test_main(int, char* []) |
|---|
| 65 | { |
|---|
| 66 | using namespace std; |
|---|
| 67 | srand(time(0)); |
|---|
| 68 | |
|---|
| 69 | std::vector<int> sortedValues; |
|---|
| 70 | |
|---|
| 71 | boost::signal0<void> sig; |
|---|
| 72 | sig.connect(write_ungrouped1()); |
|---|
| 73 | for (int i = 0; i < 100; ++i) { |
|---|
| 74 | #ifdef BOOST_NO_STDC_NAMESPACE |
|---|
| 75 | int v = rand() % 100; |
|---|
| 76 | #else |
|---|
| 77 | int v = std::rand() % 100; |
|---|
| 78 | #endif |
|---|
| 79 | sortedValues.push_back(v); |
|---|
| 80 | sig.connect(v, emit_int(v)); |
|---|
| 81 | |
|---|
| 82 | if (i == 50) { |
|---|
| 83 | sig.connect(write_ungrouped2()); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | sig.connect(write_ungrouped3()); |
|---|
| 87 | |
|---|
| 88 | std::sort(sortedValues.begin(), sortedValues.end()); |
|---|
| 89 | |
|---|
| 90 | // 17 at beginning, 42 at end |
|---|
| 91 | sortedValues.insert(sortedValues.begin(), 17); |
|---|
| 92 | sig.connect(emit_int(17), boost::BOOST_SIGNALS_NAMESPACE::at_front); |
|---|
| 93 | sortedValues.push_back(42); |
|---|
| 94 | sig.connect(emit_int(42)); |
|---|
| 95 | |
|---|
| 96 | sig(); |
|---|
| 97 | std::cout << std::endl; |
|---|
| 98 | |
|---|
| 99 | BOOST_CHECK(valuesOutput == sortedValues); |
|---|
| 100 | BOOST_CHECK(ungrouped1); |
|---|
| 101 | BOOST_CHECK(ungrouped2); |
|---|
| 102 | BOOST_CHECK(ungrouped3); |
|---|
| 103 | return 0; |
|---|
| 104 | } |
|---|