cover_operators.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // boost/integer/cover_operators.hpp ----------------------------------------//
  2. // Copyright Darin Adler 2000
  3. // Copyright Beman Dawes 2008
  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. // If the class being covered has a non-explicit conversion to an integer type
  8. // then a smaller number of cover operations are needed. Define the macro
  9. // BOOST_MINIMAL_INTEGER_COVER_OPERATORS to indicate this.
  10. // Define BOOST_NO_IO_COVER_OPERATORS if I/O cover operations are not desired.
  11. //----------------------------------------------------------------------------//
  12. #ifndef BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP
  13. #define BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP
  14. #if defined(_MSC_VER)
  15. #pragma once
  16. #endif
  17. # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
  18. # include <boost/operators.hpp>
  19. # endif
  20. #include <iosfwd>
  21. namespace boost { namespace spirit
  22. {
  23. namespace endian
  24. {
  25. // A class that adds integer operators to an integer cover class
  26. template <typename T, typename IntegerType>
  27. class cover_operators
  28. # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
  29. : boost::operators<T>
  30. # endif
  31. {
  32. // The other operations take advantage of the type conversion that's
  33. // built into unary +.
  34. // Unary operations.
  35. friend IntegerType operator+(const T& x) { return x; }
  36. # ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
  37. friend IntegerType operator-(const T& x) { return -+x; }
  38. friend IntegerType operator~(const T& x) { return ~+x; }
  39. friend IntegerType operator!(const T& x) { return !+x; }
  40. // The basic ordering operations.
  41. friend bool operator==(const T& x, IntegerType y) { return +x == y; }
  42. friend bool operator<(const T& x, IntegerType y) { return +x < y; }
  43. # endif
  44. // The basic arithmetic operations.
  45. friend T& operator+=(T& x, IntegerType y) { return x = +x + y; }
  46. friend T& operator-=(T& x, IntegerType y) { return x = +x - y; }
  47. friend T& operator*=(T& x, IntegerType y) { return x = +x * y; }
  48. friend T& operator/=(T& x, IntegerType y) { return x = +x / y; }
  49. friend T& operator%=(T& x, IntegerType y) { return x = +x % y; }
  50. friend T& operator&=(T& x, IntegerType y) { return x = +x & y; }
  51. friend T& operator|=(T& x, IntegerType y) { return x = +x | y; }
  52. friend T& operator^=(T& x, IntegerType y) { return x = +x ^ y; }
  53. friend T& operator<<=(T& x, IntegerType y) { return x = +x << y; }
  54. friend T& operator>>=(T& x, IntegerType y) { return x = +x >> y; }
  55. // A few binary arithmetic operations not covered by operators base class.
  56. friend IntegerType operator<<(const T& x, IntegerType y) { return +x << y; }
  57. friend IntegerType operator>>(const T& x, IntegerType y) { return +x >> y; }
  58. // Auto-increment and auto-decrement can be defined in terms of the
  59. // arithmetic operations.
  60. friend T& operator++(T& x) { return x += 1; }
  61. friend T& operator--(T& x) { return x -= 1; }
  62. # ifdef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
  63. friend T operator++(T& x, int)
  64. {
  65. T tmp(x);
  66. x += 1;
  67. return tmp;
  68. }
  69. friend T operator--(T& x, int)
  70. {
  71. T tmp(x);
  72. x -= 1;
  73. return tmp;
  74. }
  75. # endif
  76. # ifndef BOOST_NO_IO_COVER_OPERATORS
  77. // TODO: stream I/O needs to be templatized on the stream type, so will
  78. // work with wide streams, etc.
  79. // Stream input and output.
  80. friend std::ostream& operator<<(std::ostream& s, const T& x)
  81. { return s << +x; }
  82. friend std::istream& operator>>(std::istream& s, T& x)
  83. {
  84. IntegerType i;
  85. if (s >> i)
  86. x = i;
  87. return s;
  88. }
  89. # endif
  90. };
  91. } // namespace endian
  92. }} // namespace boost::spirit
  93. #endif // BOOST_SPIRIT_INTEGER_COVER_OPERATORS_HPP