1 | // Boost.Function library |
---|---|
2 | |
3 | // Copyright Douglas Gregor 2004. |
4 | // Copyright 2005 Peter Dimov |
5 | |
6 | // Use, modification and distribution is subject to |
7 | // the Boost Software License, Version 1.0. |
8 | // (See accompanying file LICENSE_1_0.txt or copy at |
9 | // http://www.boost.org/LICENSE_1_0.txt) |
10 | |
11 | #include <boost/function.hpp> |
12 | #include <boost/core/lightweight_test.hpp> |
13 | |
14 | static int forty_two() |
15 | { |
16 | return 42; |
17 | } |
18 | |
19 | struct Seventeen |
20 | { |
21 | int operator()() const |
22 | { |
23 | return 17; |
24 | } |
25 | }; |
26 | |
27 | bool operator==(const Seventeen&, const Seventeen&) |
28 | { |
29 | return true; |
30 | } |
31 | |
32 | struct ReturnInt |
33 | { |
34 | explicit ReturnInt(int value) : value(value) |
35 | { |
36 | } |
37 | |
38 | int operator()() const |
39 | { |
40 | return value; |
41 | } |
42 | |
43 | int value; |
44 | }; |
45 | |
46 | bool operator==(const ReturnInt& x, const ReturnInt& y) |
47 | { |
48 | return x.value == y.value; |
49 | } |
50 | |
51 | bool operator!=(const ReturnInt& x, const ReturnInt& y) |
52 | { |
53 | return x.value != y.value; |
54 | } |
55 | |
56 | int main() |
57 | { |
58 | boost::function0<int> fn; |
59 | |
60 | fn = &forty_two; |
61 | |
62 | BOOST_TEST( fn() == 42 ); |
63 | |
64 | BOOST_TEST( fn.contains(&forty_two) ); |
65 | BOOST_TEST( !fn.contains( Seventeen() ) ); |
66 | BOOST_TEST( !fn.contains( ReturnInt(0) ) ); |
67 | BOOST_TEST( !fn.contains( ReturnInt(12) ) ); |
68 | |
69 | fn = Seventeen(); |
70 | |
71 | BOOST_TEST( fn() == 17 ); |
72 | |
73 | BOOST_TEST( !fn.contains( &forty_two ) ); |
74 | BOOST_TEST( fn.contains( Seventeen() ) ); |
75 | BOOST_TEST( !fn.contains( ReturnInt(0) ) ); |
76 | BOOST_TEST( !fn.contains( ReturnInt(12) ) ); |
77 | |
78 | fn = ReturnInt(12); |
79 | |
80 | BOOST_TEST( fn() == 12 ); |
81 | |
82 | BOOST_TEST( !fn.contains( &forty_two ) ); |
83 | BOOST_TEST( !fn.contains( Seventeen() ) ); |
84 | BOOST_TEST( !fn.contains( ReturnInt(0) ) ); |
85 | BOOST_TEST( fn.contains( ReturnInt(12) ) ); |
86 | |
87 | return boost::report_errors(); |
88 | } |
89 |