operator.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef BOOST_COMPUTE_FUNCTIONAL_OPERATORS_HPP
  11. #define BOOST_COMPUTE_FUNCTIONAL_OPERATORS_HPP
  12. #include <string>
  13. namespace boost {
  14. namespace compute {
  15. namespace detail {
  16. template<class Expr1, class Expr2, class Result>
  17. struct invoked_binary_operator
  18. {
  19. typedef Result result_type;
  20. invoked_binary_operator(const std::string &op,
  21. const Expr1 &arg1,
  22. const Expr2 &arg2)
  23. : m_op(op),
  24. m_expr1(arg1),
  25. m_expr2(arg2)
  26. {
  27. }
  28. std::string op() const
  29. {
  30. return m_op;
  31. }
  32. Expr1 arg1() const
  33. {
  34. return m_expr1;
  35. }
  36. Expr2 arg2() const
  37. {
  38. return m_expr2;
  39. }
  40. std::string m_op;
  41. Expr1 m_expr1;
  42. Expr2 m_expr2;
  43. };
  44. } // end detail namespace
  45. /// \internal_
  46. #define BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(name, op, return_type, arg_type) \
  47. template<class arg_type> \
  48. class name : public function<return_type (arg_type, arg_type)> \
  49. { \
  50. public: \
  51. name() : function<return_type (arg_type, arg_type)>(BOOST_PP_STRINGIZE(name)) { } \
  52. \
  53. template<class Arg1, class Arg2> \
  54. detail::invoked_binary_operator<Arg1, Arg2, T> \
  55. operator()(const Arg1 &x, const Arg2 &y) const \
  56. { \
  57. return detail::invoked_binary_operator<Arg1, Arg2, T>(op, x, y); \
  58. } \
  59. };
  60. // arithmetic operations
  61. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(plus, "+", T, T)
  62. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(minus, "-", T, T)
  63. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(multiplies, "*", T, T)
  64. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(divides, "/", T, T)
  65. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(modulus, "%", T, T)
  66. // comparisons
  67. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(equal_to, "==", T, T)
  68. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(not_equal_to, "!=", T, T)
  69. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(greater, ">", T, T)
  70. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(less, "<", T, T)
  71. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(greater_equal, ">=", T, T)
  72. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(less_equal, "<=", T, T)
  73. // logical operators
  74. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(logical_and, "&&", T, T)
  75. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(logical_or, "||", T, T)
  76. // bitwise operations
  77. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(bit_and, "&", T, T)
  78. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(bit_or, "|", T, T)
  79. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(bit_xor, "^", T, T)
  80. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(shift_left, "<<", T, T)
  81. BOOST_COMPUTE_DECLARE_BINARY_OPERATOR(shift_right, ">>", T, T)
  82. } // end compute namespace
  83. } // end boost namespace
  84. #endif // BOOST_COMPUTE_FUNCTIONAL_OPERATORS_HPP