1 //---------------------------------------------------------------------------// 2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com> 3 // 4 // Distributed under the Boost Software License, Version 1.0 5 // See accompanying file LICENSE_1_0.txt or copy at 6 // http://www.boost.org/LICENSE_1_0.txt 7 // 8 // See http://boostorg.github.com/compute for more information. 9 //---------------------------------------------------------------------------// 10 11 #ifndef BOOST_COMPUTE_FUNCTIONAL_OPERATORS_HPP 12 #define BOOST_COMPUTE_FUNCTIONAL_OPERATORS_HPP 13 14 #include <string> 15 16 namespace boost { 17 namespace compute { 18 namespace detail { 19 20 template<class Expr1, class Expr2, class Result> 21 struct invoked_binary_operator 22 { 23 typedef Result result_type; 24 invoked_binary_operatorboost::compute::detail::invoked_binary_operator25 invoked_binary_operator(const std::string &op, 26 const Expr1 &arg1, 27 const Expr2 &arg2) 28 : m_op(op), 29 m_expr1(arg1), 30 m_expr2(arg2) 31 { 32 } 33 opboost::compute::detail::invoked_binary_operator34 std::string op() const 35 { 36 return m_op; 37 } 38 arg1boost::compute::detail::invoked_binary_operator39 Expr1 arg1() const 40 { 41 return m_expr1; 42 } 43 arg2boost::compute::detail::invoked_binary_operator44 Expr2 arg2() const 45 { 46 return m_expr2; 47 } 48 49 std::string m_op; 50 Expr1 m_expr1; 51 Expr2 m_expr2; 52 }; 53 54 } // end detail namespace 55 56 /// \internal_ 57 #define BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(name, op, return_type, arg_type) \ 58 template<class arg_type> \ 59 class name : public function<return_type (arg_type, arg_type)> \ 60 { \ 61 public: \ 62 name() : function<return_type (arg_type, arg_type)>(BOOST_PP_STRINGIZE(name)) { } \ 63 \ 64 template<class Arg1, class Arg2> \ 65 detail::invoked_binary_operator<Arg1, Arg2, T> \ 66 operator()(const Arg1 &x, const Arg2 &y) const \ 67 { \ 68 return detail::invoked_binary_operator<Arg1, Arg2, T>(op, x, y); \ 69 } \ 70 }; 71 72 // arithmetic operations 73 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(plus, "+", T, T) 74 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(minus, "-", T, T) 75 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(multiplies, "*", T, T) 76 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(divides, "/", T, T) 77 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(modulus, "%", T, T) 78 79 // comparisons 80 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(equal_to, "==", T, T) 81 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(not_equal_to, "!=", T, T) 82 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(greater, ">", T, T) 83 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(less, "<", T, T) 84 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(greater_equal, ">=", T, T) 85 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(less_equal, "<=", T, T) 86 87 // logical operators 88 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(logical_and, "&&", T, T) 89 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(logical_or, "||", T, T) 90 91 // bitwise operations 92 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(bit_and, "&", T, T) 93 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(bit_or, "|", T, T) 94 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(bit_xor, "^", T, T) 95 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(shift_left, "<<", T, T) 96 BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(shift_right, ">>", T, T) 97 98 } // end compute namespace 99 } // end boost namespace 100 #endif // BOOST_COMPUTE_FUNCTIONAL_OPERATORS_HPP 101