| 1 | // Boost.Signals library |
|---|
| 2 | |
|---|
| 3 | // Copyright Douglas Gregor 2001-2003. 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 <boost/bind.hpp> |
|---|
| 13 | |
|---|
| 14 | struct short_lived : public boost::BOOST_SIGNALS_NAMESPACE::trackable { |
|---|
| 15 | ~short_lived() {} |
|---|
| 16 | }; |
|---|
| 17 | |
|---|
| 18 | struct swallow { |
|---|
| 19 | template<typename T> int operator()(const T*, int i) { return i; } |
|---|
| 20 | }; |
|---|
| 21 | |
|---|
| 22 | template<typename T> |
|---|
| 23 | struct max_or_default { |
|---|
| 24 | typedef T result_type; |
|---|
| 25 | |
|---|
| 26 | template<typename InputIterator> |
|---|
| 27 | T operator()(InputIterator first, InputIterator last) const |
|---|
| 28 | { |
|---|
| 29 | if (first == last) |
|---|
| 30 | return T(); |
|---|
| 31 | |
|---|
| 32 | T max = *first++; |
|---|
| 33 | for (; first != last; ++first) |
|---|
| 34 | max = (*first > max)? *first : max; |
|---|
| 35 | |
|---|
| 36 | return max; |
|---|
| 37 | } |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | int test_main(int, char*[]) |
|---|
| 41 | { |
|---|
| 42 | typedef boost::signal1<int, int, max_or_default<int> > sig_type; |
|---|
| 43 | sig_type s1; |
|---|
| 44 | |
|---|
| 45 | // Test auto-disconnection |
|---|
| 46 | BOOST_CHECK(s1(5) == 0); |
|---|
| 47 | { |
|---|
| 48 | short_lived shorty; |
|---|
| 49 | s1.connect(boost::bind<int>(swallow(), &shorty, _1)); |
|---|
| 50 | BOOST_CHECK(s1(5) == 5); |
|---|
| 51 | } |
|---|
| 52 | BOOST_CHECK(s1(5) == 0); |
|---|
| 53 | |
|---|
| 54 | // Test auto-disconnection of slot before signal connection |
|---|
| 55 | { |
|---|
| 56 | short_lived* shorty = new short_lived(); |
|---|
| 57 | |
|---|
| 58 | sig_type::slot_type slot(boost::bind<int>(swallow(), shorty, _1)); |
|---|
| 59 | delete shorty; |
|---|
| 60 | |
|---|
| 61 | BOOST_CHECK(s1(5) == 0); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | return 0; |
|---|
| 65 | } |
|---|