| 1 | /*============================================================================= |
| 2 | Copyright (c) 2011 Thomas Heller |
| 3 | Copyright (c) 2015 John Fletcher |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | ==============================================================================*/ |
| 8 | #include <iostream> |
| 9 | #include <cmath> |
| 10 | #include <boost/detail/lightweight_test.hpp> |
| 11 | #include <boost/phoenix/core.hpp> |
| 12 | #include <boost/phoenix/function.hpp> |
| 13 | |
| 14 | namespace impl |
| 15 | { |
| 16 | void |
| 17 | test() |
| 18 | { |
| 19 | std::cout << "Test adapting functions...\n" ; |
| 20 | } |
| 21 | |
| 22 | int |
| 23 | negate(int n) |
| 24 | { |
| 25 | return -n; |
| 26 | } |
| 27 | |
| 28 | int |
| 29 | plus(int a, int b) |
| 30 | { |
| 31 | return a + b; |
| 32 | } |
| 33 | |
| 34 | template <typename T> |
| 35 | T |
| 36 | plus(T a, T b, T c) |
| 37 | { |
| 38 | return a + b + c; |
| 39 | } |
| 40 | |
| 41 | int |
| 42 | plus4(int a, int b, int c, int d) |
| 43 | { |
| 44 | return a + b + c + d; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY(void, test, impl::test) |
| 49 | BOOST_PHOENIX_ADAPT_FUNCTION(int, negate, impl::negate, 1) |
| 50 | BOOST_PHOENIX_ADAPT_FUNCTION(int, plus, impl::plus, 2) |
| 51 | BOOST_PHOENIX_ADAPT_FUNCTION( |
| 52 | typename boost::remove_reference<A0>::type |
| 53 | , plus |
| 54 | , impl::plus |
| 55 | , 3 |
| 56 | ) |
| 57 | BOOST_PHOENIX_ADAPT_FUNCTION(int, plus4, impl::plus4, 4) |
| 58 | |
| 59 | // Test of solution to bug when using namespace |
| 60 | using namespace boost::phoenix; |
| 61 | |
| 62 | BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY(void, test2, impl::test) |
| 63 | BOOST_PHOENIX_ADAPT_FUNCTION(int, negate2, impl::negate, 1) |
| 64 | |
| 65 | |
| 66 | int |
| 67 | main() |
| 68 | { |
| 69 | using boost::phoenix::arg_names::arg1; |
| 70 | using boost::phoenix::arg_names::arg2; |
| 71 | |
| 72 | int a = 123; |
| 73 | int b = 256; |
| 74 | |
| 75 | test()(); |
| 76 | test2()(); |
| 77 | BOOST_TEST(::negate(arg1)(a) == -a); |
| 78 | BOOST_TEST(::negate2(arg1)(a) == -a); |
| 79 | BOOST_TEST(::plus(arg1, arg2)(a, b) == a+b); |
| 80 | BOOST_TEST(::plus(arg1, arg2, 3)(a, b) == a+b+3); |
| 81 | BOOST_TEST(plus4(arg1, arg2, 3, 4)(a, b) == a+b+3+4); |
| 82 | |
| 83 | return boost::report_errors(); |
| 84 | } |
| 85 | |