]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright John Maddock 2005. |
2 | // Use, modification and distribution are subject to the | |
3 | // Boost Software License, Version 1.0. (See accompanying file | |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #ifdef TEST_STD_HEADERS | |
7 | #include <functional> | |
8 | #else | |
9 | #include <boost/tr1/functional.hpp> | |
10 | #endif | |
11 | ||
12 | #include <boost/static_assert.hpp> | |
13 | #include <boost/type_traits/is_base_and_derived.hpp> | |
14 | #include "verify_return.hpp" | |
15 | ||
16 | struct test_functor | |
17 | { | |
18 | int operator()()const; | |
19 | long operator()(int)const; | |
20 | double operator()(int, char)const; | |
21 | }; | |
22 | ||
23 | void nothing(){} | |
24 | ||
25 | template <class Func> | |
26 | void test_function(Func*) | |
27 | { | |
28 | typedef typename Func::result_type result_type; | |
29 | ||
30 | test_functor t; | |
31 | Func f1; | |
32 | Func f2(0); | |
33 | Func f3(f1); | |
34 | Func f4(t); | |
35 | ||
36 | f2 = f1; | |
37 | f2 = 0; | |
38 | f2 = t; | |
39 | f2 = std::tr1::ref(t); | |
40 | f2 = std::tr1::cref(t); | |
41 | const Func& cf = f1; | |
42 | ||
43 | std::tr1::swap(f1, f2); | |
44 | if(cf) nothing(); | |
45 | if(cf && f2) nothing(); | |
46 | if(cf || f2) nothing(); | |
47 | if(f2 && !cf) nothing(); | |
48 | ||
49 | const std::type_info& info = cf.target_type(); | |
50 | test_functor* func1 = f1.template target<test_functor>(); | |
51 | const test_functor* func2 = cf.template target<test_functor>(); | |
52 | ||
53 | // comparison with null: | |
54 | if(0 == f1) nothing(); | |
55 | if(0 != f1) nothing(); | |
56 | if(f2 == 0) nothing(); | |
57 | if(f2 != 0) nothing(); | |
58 | } | |
59 | ||
60 | ||
61 | int main() | |
62 | { | |
63 | BOOST_STATIC_ASSERT((::boost::is_base_and_derived<std::exception, std::tr1::bad_function_call>::value)); | |
64 | std::tr1::bad_function_call fe; | |
65 | ||
66 | test_function(static_cast<std::tr1::function<int (void)>* >(0)); | |
67 | test_function(static_cast<std::tr1::function<long (int)>* >(0)); | |
68 | test_function(static_cast<std::tr1::function<double (int,char)>* >(0)); | |
69 | ||
70 | std::tr1::function<int (void)> f1; | |
71 | verify_return_type(f1(), int(0)); | |
72 | std::tr1::function<long (int)> f2; | |
73 | verify_return_type(f2(0), long(0)); | |
74 | std::tr1::function<double (int, char)> f3; | |
75 | verify_return_type(f3(0,0), double(0)); | |
76 | ||
77 | BOOST_STATIC_ASSERT((::boost::is_base_and_derived<std::unary_function<int, long>, std::tr1::function<long (int)> >::value)); | |
78 | BOOST_STATIC_ASSERT((::boost::is_base_and_derived<std::binary_function<int, char, double>, std::tr1::function<double (int,char)> >::value)); | |
79 | return 0; | |
80 | } | |
81 |