width.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // width.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_WIDTH_HPP_EAN_04_07_2006
  8. #define BOOST_XPRESSIVE_DETAIL_UTILITY_WIDTH_HPP_EAN_04_07_2006
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <climits> // for INT_MAX
  14. #include <boost/mpl/size_t.hpp>
  15. namespace boost { namespace xpressive { namespace detail
  16. {
  17. typedef mpl::size_t<INT_MAX / 2 - 1> unknown_width;
  18. struct width;
  19. bool is_unknown(width const &that);
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // width
  22. struct width
  23. {
  24. width(std::size_t val = 0)
  25. : value_(val)
  26. {
  27. }
  28. bool operator !() const
  29. {
  30. return !this->value_;
  31. }
  32. width &operator +=(width const &that)
  33. {
  34. this->value_ =
  35. !is_unknown(*this) && !is_unknown(that)
  36. ? this->value_ + that.value_
  37. : unknown_width();
  38. return *this;
  39. }
  40. width &operator |=(width const &that)
  41. {
  42. this->value_ =
  43. this->value_ == that.value_
  44. ? this->value_
  45. : unknown_width();
  46. return *this;
  47. }
  48. std::size_t value() const
  49. {
  50. return this->value_;
  51. }
  52. private:
  53. std::size_t value_;
  54. };
  55. inline bool is_unknown(width const &that)
  56. {
  57. return unknown_width::value == that.value();
  58. }
  59. inline bool operator ==(width const &left, width const &right)
  60. {
  61. return left.value() == right.value();
  62. }
  63. inline bool operator !=(width const &left, width const &right)
  64. {
  65. return left.value() != right.value();
  66. }
  67. inline width operator +(width left, width const &right)
  68. {
  69. return left += right;
  70. }
  71. inline width operator |(width left, width const &right)
  72. {
  73. return left |= right;
  74. }
  75. }}} // namespace boost::xpressive::detail
  76. #endif