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 
8 #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_WIDTH_HPP_EAN_04_07_2006
9 #define BOOST_XPRESSIVE_DETAIL_UTILITY_WIDTH_HPP_EAN_04_07_2006
10 
11 // MS compatible compilers support #pragma once
12 #if defined(_MSC_VER)
13 # pragma once
14 #endif
15 
16 #include <climits> // for INT_MAX
17 #include <boost/mpl/size_t.hpp>
18 
19 namespace boost { namespace xpressive { namespace detail
20 {
21 
22 typedef mpl::size_t<INT_MAX / 2 - 1> unknown_width;
23 struct width;
24 bool is_unknown(width const &that);
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 // width
28 struct width
29 {
widthboost::xpressive::detail::width30     width(std::size_t val = 0)
31       : value_(val)
32     {
33     }
34 
operator !boost::xpressive::detail::width35     bool operator !() const
36     {
37         return !this->value_;
38     }
39 
operator +=boost::xpressive::detail::width40     width &operator +=(width const &that)
41     {
42         this->value_ =
43             !is_unknown(*this) && !is_unknown(that)
44           ? this->value_ + that.value_
45           : unknown_width();
46         return *this;
47     }
48 
operator |=boost::xpressive::detail::width49     width &operator |=(width const &that)
50     {
51         this->value_ =
52             this->value_ == that.value_
53           ? this->value_
54           : unknown_width();
55         return *this;
56     }
57 
valueboost::xpressive::detail::width58     std::size_t value() const
59     {
60         return this->value_;
61     }
62 
63 private:
64     std::size_t value_;
65 };
66 
is_unknown(width const & that)67 inline bool is_unknown(width const &that)
68 {
69     return unknown_width::value == that.value();
70 }
71 
operator ==(width const & left,width const & right)72 inline bool operator ==(width const &left, width const &right)
73 {
74     return left.value() == right.value();
75 }
76 
operator !=(width const & left,width const & right)77 inline bool operator !=(width const &left, width const &right)
78 {
79     return left.value() != right.value();
80 }
81 
operator +(width left,width const & right)82 inline width operator +(width left, width const &right)
83 {
84     return left += right;
85 }
86 
operator |(width left,width const & right)87 inline width operator |(width left, width const &right)
88 {
89     return left |= right;
90 }
91 
92 }}} // namespace boost::xpressive::detail
93 
94 #endif
95