Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame^] | 1 | /*============================================================================= |
| 2 | Copyright (c) 2007 Tobias Schwinger |
| 3 | |
| 4 | Use modification and distribution are subject to the Boost Software |
| 5 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | http://www.boost.org/LICENSE_1_0.txt). |
| 7 | ==============================================================================*/ |
| 8 | |
| 9 | #include <boost/config.hpp> |
| 10 | |
| 11 | #ifdef BOOST_MSVC |
| 12 | # pragma warning(disable: 4244) // no conversion warnings, please |
| 13 | #endif |
| 14 | |
| 15 | #include <boost/detail/lightweight_test.hpp> |
| 16 | #include <boost/functional/forward_adapter.hpp> |
| 17 | |
| 18 | #include <boost/type_traits/is_same.hpp> |
| 19 | |
| 20 | #include <boost/blank.hpp> |
| 21 | #include <boost/noncopyable.hpp> |
| 22 | |
| 23 | #include <memory> |
| 24 | |
| 25 | template <class Base = ndnboost::blank> |
| 26 | class test_func : public Base |
| 27 | { |
| 28 | int val; |
| 29 | public: |
| 30 | test_func(int v) : val(v) { } |
| 31 | |
| 32 | template<class B> |
| 33 | test_func(test_func<B> const & that) |
| 34 | : val(that.val) |
| 35 | { } |
| 36 | |
| 37 | template<class B> friend class test_func; |
| 38 | |
| 39 | int operator()(int & l, int const & r) const |
| 40 | { |
| 41 | return l=r+val; |
| 42 | } |
| 43 | long operator()(int & l, int const & r) |
| 44 | { |
| 45 | return -(l=r+val); |
| 46 | } |
| 47 | |
| 48 | template <typename Sig> |
| 49 | struct result |
| 50 | { |
| 51 | typedef void type; |
| 52 | }; |
| 53 | |
| 54 | // ensure result_of argument types are what's expected |
| 55 | // note: this is *not* how client code should look like |
| 56 | template <class Self> |
| 57 | struct result< Self const(int&,int const&) > { typedef int type; }; |
| 58 | |
| 59 | template <class Self> |
| 60 | struct result< Self(int&,int const&) > { typedef long type; }; |
| 61 | |
| 62 | template <class Self> |
| 63 | struct result< Self(int&,int&) > { typedef char type; }; |
| 64 | }; |
| 65 | |
| 66 | enum { int_, long_, char_ }; |
| 67 | |
| 68 | int type_of(int) { return int_; } |
| 69 | int type_of(long) { return long_; } |
| 70 | int type_of(char) { return char_; } |
| 71 | |
| 72 | int main() |
| 73 | { |
| 74 | { |
| 75 | using ndnboost::is_same; |
| 76 | using ndnboost::result_of; |
| 77 | typedef ndnboost::forward_adapter< test_func<> > f; |
| 78 | |
| 79 | // lvalue,rvalue |
| 80 | BOOST_TEST(( is_same< |
| 81 | result_of< f(int&, int) >::type, long >::value )); |
| 82 | BOOST_TEST(( is_same< |
| 83 | result_of< f const (int&, int) >::type, int >::value )); |
| 84 | // lvalue,const lvalue |
| 85 | BOOST_TEST(( is_same< |
| 86 | result_of< f(int&, int const &) >::type, long >::value )); |
| 87 | BOOST_TEST(( is_same< |
| 88 | result_of< f const (int&, int const &) >::type, int >::value )); |
| 89 | // lvalue,lvalue |
| 90 | BOOST_TEST(( is_same< |
| 91 | result_of< f(int&, int&) >::type, char >::value )); |
| 92 | BOOST_TEST(( is_same< |
| 93 | result_of< f const (int&, int&) >::type, char >::value )); |
| 94 | } |
| 95 | |
| 96 | { |
| 97 | using ndnboost::noncopyable; |
| 98 | using ndnboost::forward_adapter; |
| 99 | |
| 100 | int x = 0; |
| 101 | test_func<noncopyable> f(7); |
| 102 | forward_adapter< test_func<> > func(f); |
| 103 | forward_adapter< test_func<noncopyable> & > func_ref(f); |
| 104 | forward_adapter< test_func<noncopyable> & > const func_ref_c(f); |
| 105 | forward_adapter< test_func<> const > func_c(f); |
| 106 | forward_adapter< test_func<> > const func_c2(f); |
| 107 | forward_adapter< test_func<noncopyable> const & > func_c_ref(f); |
| 108 | |
| 109 | BOOST_TEST( type_of( func(x,1) ) == long_ ); |
| 110 | BOOST_TEST( type_of( func_ref(x,1) ) == long_ ); |
| 111 | BOOST_TEST( type_of( func_ref_c(x,1) ) == long_ ); |
| 112 | BOOST_TEST( type_of( func_c(x,1) ) == int_ ); |
| 113 | BOOST_TEST( type_of( func_c2(x,1) ) == int_ ); |
| 114 | BOOST_TEST( type_of( func_c_ref(x,1) ) == int_ ); |
| 115 | BOOST_TEST( type_of( func(x,x) ) == char_ ); |
| 116 | |
| 117 | BOOST_TEST( func(x,1) == -8 ); |
| 118 | BOOST_TEST( func_ref(x,1) == -8 ); |
| 119 | BOOST_TEST( func_ref_c(x,1) == -8 ); |
| 120 | BOOST_TEST( func_c(x,1) == 8 ); |
| 121 | BOOST_TEST( func_c2(x,1) == 8 ); |
| 122 | BOOST_TEST( func_c_ref(x,1) == 8 ); |
| 123 | } |
| 124 | |
| 125 | return ndnboost::report_errors(); |
| 126 | } |
| 127 | |
| 128 | |