| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2007 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | ==============================================================================*/ |
| 7 | |
| 8 | #include <iostream> |
| 9 | #include <cmath> |
| 10 | #include <boost/detail/lightweight_test.hpp> |
| 11 | #include <boost/phoenix/core.hpp> |
| 12 | #include <boost/phoenix/operator.hpp> |
| 13 | #include <boost/phoenix/function.hpp> |
| 14 | |
| 15 | #define |
| 16 | #include <boost/mpl/multiplies.hpp> |
| 17 | #undef BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS |
| 18 | |
| 19 | struct test_impl |
| 20 | { |
| 21 | typedef void result_type; |
| 22 | void operator()() const |
| 23 | { |
| 24 | std::cout << "Test lazy functions...\n" ; |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | boost::phoenix::function<test_impl> test; |
| 29 | |
| 30 | struct sqr_impl |
| 31 | { |
| 32 | template <typename Signature> |
| 33 | struct result; |
| 34 | |
| 35 | template <typename This, typename Arg> |
| 36 | struct result<This(Arg)> |
| 37 | { |
| 38 | typedef typename boost::remove_reference<Arg>::type type; |
| 39 | }; |
| 40 | |
| 41 | template <typename Arg> |
| 42 | Arg |
| 43 | operator()(Arg n) const |
| 44 | { |
| 45 | return n * n; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | boost::phoenix::function<sqr_impl> sqr; |
| 50 | |
| 51 | struct fact_impl |
| 52 | { |
| 53 | template <typename Signature> |
| 54 | struct result; |
| 55 | |
| 56 | template <typename This, typename Arg> |
| 57 | struct result<This(Arg)> |
| 58 | { |
| 59 | typedef typename boost::remove_reference<Arg>::type type; |
| 60 | }; |
| 61 | |
| 62 | template <typename Arg> |
| 63 | Arg |
| 64 | operator()(Arg n) const |
| 65 | { |
| 66 | return (n <= 0) ? 1 : n * (*this)(n-1); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | boost::phoenix::function<fact_impl> fact; |
| 71 | |
| 72 | struct pow_impl |
| 73 | { |
| 74 | |
| 75 | template <typename Sig> |
| 76 | struct result; |
| 77 | |
| 78 | template <typename This, typename Arg1, typename Arg2> |
| 79 | struct result<This(Arg1, Arg2)> |
| 80 | { |
| 81 | typedef typename boost::remove_reference<Arg1>::type type; |
| 82 | }; |
| 83 | |
| 84 | template <typename Arg1, typename Arg2> |
| 85 | Arg1 |
| 86 | operator()(Arg1 a, Arg2 b) const |
| 87 | { |
| 88 | return std::pow(a, b); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | boost::phoenix::function<pow_impl> power; |
| 93 | |
| 94 | struct add_impl |
| 95 | { |
| 96 | template <typename Sig> |
| 97 | struct result; |
| 98 | |
| 99 | template <typename This, typename Arg1, typename Arg2, typename Arg3, typename Arg4> |
| 100 | struct result<This(Arg1, Arg2, Arg3, Arg4)> |
| 101 | { |
| 102 | typedef typename boost::remove_reference<Arg1>::type type; |
| 103 | }; |
| 104 | |
| 105 | template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> |
| 106 | Arg1 |
| 107 | operator()(Arg1 a, Arg2 b, Arg3 c, Arg4 d) const |
| 108 | { |
| 109 | return a + b + c + d; |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | boost::phoenix::function<add_impl> add; |
| 114 | |
| 115 | int |
| 116 | main() |
| 117 | { |
| 118 | using boost::phoenix::arg_names::arg1; |
| 119 | using boost::phoenix::arg_names::arg2; |
| 120 | |
| 121 | int i5 = 5; |
| 122 | double d5 = 5, d3 = 3; |
| 123 | |
| 124 | test()(); |
| 125 | BOOST_TEST(sqr(arg1)(i5) == (i5*i5)); |
| 126 | BOOST_TEST(fact(4)() == 24); |
| 127 | BOOST_TEST(fact(arg1)(i5) == 120); |
| 128 | BOOST_TEST((int)power(arg1, arg2)(d5, d3) == (int)std::pow(d5, d3)); |
| 129 | BOOST_TEST((sqr(arg1) + 5)(i5) == ((i5*i5)+5)); |
| 130 | BOOST_TEST(add(arg1, arg1, arg1, arg1)(i5) == (5+5+5+5)); |
| 131 | |
| 132 | // testing composition |
| 133 | BOOST_TEST(add(arg1, arg1, arg1, power(arg1, 2))(d5) == (5+5+5+25)); |
| 134 | |
| 135 | int const ic5 = 5; |
| 136 | // testing consts |
| 137 | BOOST_TEST(sqr(arg1)(ic5) == (ic5*ic5)); |
| 138 | |
| 139 | return boost::report_errors(); |
| 140 | } |
| 141 | |