Parent Directory
|
Revision Log
1 2 3 // Boost.Egg 4 // 5 // Copyright Shunsuke Sogame 2007-2008. 6 // Distributed under the Boost Software License, Version 1.0. 7 // (See accompanying file LICENSE_1_0.txt or copy at 8 // http://www.boost.org/LICENSE_1_0.txt) 9 10 11 #include <boost/version.hpp> 12 #if BOOST_VERSION >= 103500 13 14 15 #include <boost/egg/overload.hpp> 16 #include "./egg_test.hpp" 17 18 19 #include <boost/mpl/vector.hpp> 20 #include <boost/type_traits/is_pointer.hpp> 21 #include <boost/type_traits/remove_reference.hpp> 22 #include <boost/mpl/assert.hpp> 23 #include <boost/type_traits/is_same.hpp> 24 #include <string> 25 #include <boost/egg/const.hpp> 26 #include "./using_egg.hpp" 27 28 29 struct T_fun1 30 { 31 typedef int result_type; 32 33 result_type operator()(int const &i , int j) const 34 { 35 return i + j; 36 } 37 }; 38 39 struct T_fun2 40 { 41 typedef std::string &result_type; 42 43 result_type operator()(std::string &s , std::string t) const 44 { 45 return s += t; 46 } 47 }; 48 49 struct T_fun3 50 { 51 typedef int &result_type; 52 53 result_type operator()(int *p) const 54 { 55 return *p; 56 } 57 }; 58 59 struct T_fun4 60 { 61 typedef char result_type; 62 63 template<class T> 64 result_type operator()(T) const 65 { 66 return 'a'; 67 } 68 }; 69 70 71 typedef 72 boost::mpl::vector< 73 T_fun1(int const &, int), 74 T_fun2(std::string &, std::string const &), 75 T_fun3(boost::is_pointer< boost::remove_reference<boost::mpl::_> >), 76 T_fun4(boost::mpl::_) 77 > 78 overload_set; 79 80 typedef result_of_overload<overload_set>::type T_fun; 81 BOOST_EGG_CONST((T_fun), fun) = BOOST_EGG_OVERLOAD_L {}, {}, {}, {} BOOST_EGG_OVERLOAD_R; 82 83 84 BOOST_MPL_ASSERT((details::is_matched::apply<int &, int &>)); 85 BOOST_MPL_ASSERT((details::is_matched::apply<long *, boost::is_pointer<boost::mpl::_> >)); 86 BOOST_MPL_ASSERT((details::is_matched::apply<double, boost::mpl::_>)); 87 88 BOOST_MPL_ASSERT((details::has_matched_params<T_fun1(int const &, int), boost::tuple<int &, int &> >)); 89 BOOST_MPL_ASSERT((details::has_matched_params<T_fun2(std::string &, std::string const &), boost::tuple<std::string &, std::string &> >)); 90 91 BOOST_MPL_ASSERT_NOT((details::has_matched_params<T_fun4(boost::mpl::_), const boost::tuple<const std::string&, const std::string&> >)); 92 93 94 BOOST_MPL_ASSERT((boost::is_same< 95 details::overload_resolution<overload_set, boost::tuple<int &, int &> >::type, 96 T_fun1 97 >)); 98 99 BOOST_MPL_ASSERT((boost::is_same< 100 details::overload_resolution<overload_set, boost::tuple<std::string &, std::string const &> >::type, 101 T_fun2 102 >)); 103 104 105 struct T_fun5 106 { 107 typedef int result_type; 108 109 result_type operator()() const 110 { 111 return 999; 112 } 113 }; 114 115 typedef 116 boost::mpl::vector< 117 T_fun1(int const &, int), 118 T_fun2(std::string &, std::string const &), 119 T_fun3(boost::is_pointer< boost::remove_reference<boost::mpl::_> >), 120 T_fun4(boost::mpl::_), 121 T_fun5() 122 > 123 overload_set_nullary; 124 125 typedef result_of_overload<overload_set_nullary>::type T_fun_nullary; 126 T_fun_nullary const fun_nullary = BOOST_EGG_OVERLOAD_L {}, {}, {}, {}, {} BOOST_EGG_OVERLOAD_R; 127 128 129 BOOST_MPL_ASSERT((boost::mpl::equal< 130 boost::function_types::parameter_types<int()>::type, 131 boost::mpl::empty_sequence 132 >)); 133 134 BOOST_MPL_ASSERT((boost::is_same< 135 details::nullary_result_of_overload_resolution<overload_set_nullary>::type, 136 int 137 >)); 138 139 140 void egg_test() 141 { 142 { 143 BOOST_CHECK( fun(1, 2) == 3 ); 144 std::string s("abc"), t("de"); 145 BOOST_CHECK( fun(s, t) == "abcde" ); 146 int i = 12; 147 int *p = &i; 148 BOOST_CHECK( &(fun(p)) == &i ); 149 BOOST_CHECK( fun(s) == 'a' ); 150 } 151 { 152 BOOST_CHECK( fun_nullary(1, 2) == 3 ); 153 std::string s("abc"), t("de"); 154 BOOST_CHECK( fun_nullary(s, t) == "abcde" ); 155 int i = 12; 156 int *p = &i; 157 BOOST_CHECK( &(fun_nullary(p)) == &i ); 158 BOOST_CHECK( fun_nullary(s) == 'a' ); 159 BOOST_CHECK( fun_nullary() == 999 ); 160 } 161 } 162 163 164 #else 165 int main() {} 166 #endif