| 1 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // width_of.hpp |
|---|
| 3 | // |
|---|
| 4 | // Copyright 2004 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_STATIC_WIDTH_OF_HPP_EAN_10_04_2005 |
|---|
| 9 | #define BOOST_XPRESSIVE_DETAIL_STATIC_WIDTH_OF_HPP_EAN_10_04_2005 |
|---|
| 10 | |
|---|
| 11 | // MS compatible compilers support #pragma once |
|---|
| 12 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
|---|
| 13 | # pragma once |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 16 | #include <vector> |
|---|
| 17 | #include <boost/ref.hpp> |
|---|
| 18 | #include <boost/shared_ptr.hpp> |
|---|
| 19 | #include <boost/mpl/if.hpp> |
|---|
| 20 | #include <boost/mpl/fold.hpp> |
|---|
| 21 | #include <boost/mpl/plus.hpp> |
|---|
| 22 | #include <boost/mpl/front.hpp> |
|---|
| 23 | #include <boost/mpl/times.hpp> |
|---|
| 24 | #include <boost/mpl/assert.hpp> |
|---|
| 25 | #include <boost/mpl/lambda.hpp> |
|---|
| 26 | #include <boost/mpl/size_t.hpp> |
|---|
| 27 | #include <boost/mpl/logical.hpp> |
|---|
| 28 | #include <boost/mpl/identity.hpp> |
|---|
| 29 | #include <boost/mpl/equal_to.hpp> |
|---|
| 30 | #include <boost/mpl/transform_view.hpp> |
|---|
| 31 | #include <boost/type_traits/is_same.hpp> |
|---|
| 32 | #include <boost/xpressive/detail/detail_fwd.hpp> |
|---|
| 33 | #include <boost/xpressive/detail/static/as_xpr.hpp> |
|---|
| 34 | |
|---|
| 35 | namespace boost { namespace xpressive { namespace detail |
|---|
| 36 | { |
|---|
| 37 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | // add_width |
|---|
| 39 | template<typename X, typename Y> |
|---|
| 40 | struct add_width |
|---|
| 41 | : mpl::eval_if |
|---|
| 42 | < |
|---|
| 43 | mpl::or_ |
|---|
| 44 | < |
|---|
| 45 | mpl::equal_to<X, unknown_width> |
|---|
| 46 | , mpl::equal_to<Y, unknown_width> |
|---|
| 47 | > |
|---|
| 48 | , mpl::identity<unknown_width> |
|---|
| 49 | , mpl::plus<X, Y> |
|---|
| 50 | >::type |
|---|
| 51 | { |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 55 | // mult_width |
|---|
| 56 | template<typename X, typename Y> |
|---|
| 57 | struct mult_width |
|---|
| 58 | : mpl::eval_if |
|---|
| 59 | < |
|---|
| 60 | mpl::or_ |
|---|
| 61 | < |
|---|
| 62 | mpl::equal_to<X, unknown_width> |
|---|
| 63 | , mpl::equal_to<Y, unknown_width> |
|---|
| 64 | > |
|---|
| 65 | , mpl::identity<unknown_width> |
|---|
| 66 | , mpl::times<X, Y> |
|---|
| 67 | >::type |
|---|
| 68 | { |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 72 | // equal_width |
|---|
| 73 | template<typename X, typename Y> |
|---|
| 74 | struct equal_width |
|---|
| 75 | : mpl::if_ |
|---|
| 76 | < |
|---|
| 77 | mpl::equal_to<X, Y> |
|---|
| 78 | , X |
|---|
| 79 | , unknown_width |
|---|
| 80 | >::type |
|---|
| 81 | { |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 85 | // width_of |
|---|
| 86 | // |
|---|
| 87 | template<typename Xpr> |
|---|
| 88 | struct width_of; |
|---|
| 89 | |
|---|
| 90 | template<> |
|---|
| 91 | struct width_of<no_next> |
|---|
| 92 | : mpl::size_t<0> |
|---|
| 93 | { |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | template<typename Matcher> |
|---|
| 97 | struct width_of<proto::unary_op<Matcher, proto::noop_tag> > |
|---|
| 98 | : as_matcher_type<Matcher>::type::width |
|---|
| 99 | { |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | template<typename Left, typename Right> |
|---|
| 103 | struct width_of<proto::binary_op<Left, Right, proto::right_shift_tag> > |
|---|
| 104 | : add_width<width_of<Left>, width_of<Right> > |
|---|
| 105 | { |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | template<typename Left, typename Right> |
|---|
| 109 | struct width_of<proto::binary_op<Left, Right, proto::bitor_tag> > |
|---|
| 110 | : equal_width<width_of<Left>, width_of<Right> > |
|---|
| 111 | { |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | template<typename Right> |
|---|
| 115 | struct width_of<proto::binary_op<mark_tag, Right, proto::assign_tag> > |
|---|
| 116 | : width_of<Right> |
|---|
| 117 | { |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | template<typename Right> |
|---|
| 121 | struct width_of<proto::binary_op<set_initializer_type, Right, proto::assign_tag> > |
|---|
| 122 | : mpl::size_t<1> |
|---|
| 123 | { |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | template<typename Modifier, typename Xpr> |
|---|
| 127 | struct width_of<proto::binary_op<Modifier, Xpr, modifier_tag> > |
|---|
| 128 | : width_of<Xpr> |
|---|
| 129 | { |
|---|
| 130 | }; |
|---|
| 131 | |
|---|
| 132 | template<typename Xpr, bool Positive> |
|---|
| 133 | struct width_of<proto::unary_op<Xpr, lookahead_tag<Positive> > > |
|---|
| 134 | : mpl::size_t<0> |
|---|
| 135 | { |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | template<typename Xpr, bool Positive> |
|---|
| 139 | struct width_of<proto::unary_op<Xpr, lookbehind_tag<Positive> > > |
|---|
| 140 | : mpl::size_t<0> |
|---|
| 141 | { |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | template<typename Xpr> |
|---|
| 145 | struct width_of<proto::unary_op<Xpr, keeper_tag> > |
|---|
| 146 | : width_of<Xpr> |
|---|
| 147 | { |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | template<typename Matcher, typename Next> |
|---|
| 151 | struct width_of<static_xpression<Matcher, Next> > |
|---|
| 152 | : add_width<typename Matcher::width, width_of<Next> > |
|---|
| 153 | { |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | template<typename BidiIter> |
|---|
| 157 | struct width_of<shared_ptr<matchable<BidiIter> const> > |
|---|
| 158 | : unknown_width |
|---|
| 159 | { |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | template<typename BidiIter> |
|---|
| 163 | struct width_of<std::vector<shared_ptr<matchable<BidiIter> const> > > |
|---|
| 164 | : unknown_width |
|---|
| 165 | { |
|---|
| 166 | }; |
|---|
| 167 | |
|---|
| 168 | template<typename BidiIter> |
|---|
| 169 | struct width_of<proto::unary_op<basic_regex<BidiIter>, proto::noop_tag> > |
|---|
| 170 | : unknown_width |
|---|
| 171 | { |
|---|
| 172 | }; |
|---|
| 173 | |
|---|
| 174 | template<typename BidiIter> |
|---|
| 175 | struct width_of<proto::unary_op<reference_wrapper<basic_regex<BidiIter> const>, proto::noop_tag> > |
|---|
| 176 | : unknown_width |
|---|
| 177 | { |
|---|
| 178 | }; |
|---|
| 179 | |
|---|
| 180 | template<typename Op> |
|---|
| 181 | struct width_of<proto::unary_op<Op, proto::unary_plus_tag> > |
|---|
| 182 | : unknown_width |
|---|
| 183 | { |
|---|
| 184 | }; |
|---|
| 185 | |
|---|
| 186 | template<typename Op> |
|---|
| 187 | struct width_of<proto::unary_op<Op, proto::unary_star_tag> > |
|---|
| 188 | : unknown_width |
|---|
| 189 | { |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| 192 | template<typename Op> |
|---|
| 193 | struct width_of<proto::unary_op<Op, proto::logical_not_tag> > |
|---|
| 194 | : unknown_width |
|---|
| 195 | { |
|---|
| 196 | }; |
|---|
| 197 | |
|---|
| 198 | template<typename Op, uint_t Min, uint_t Max> |
|---|
| 199 | struct width_of<proto::unary_op<Op, generic_quant_tag<Min, Max> > > |
|---|
| 200 | : mpl::if_c<Min==Max, mult_width<width_of<Op>, mpl::size_t<Min> >, unknown_width>::type |
|---|
| 201 | { |
|---|
| 202 | }; |
|---|
| 203 | |
|---|
| 204 | template<typename Op> |
|---|
| 205 | struct width_of<proto::unary_op<Op, proto::unary_minus_tag> > |
|---|
| 206 | : width_of<Op> |
|---|
| 207 | { |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | // when complementing a set or an assertion, the width is that of the set (1) or the assertion (0) |
|---|
| 211 | template<typename Op> |
|---|
| 212 | struct width_of<proto::unary_op<Op, proto::complement_tag> > |
|---|
| 213 | : width_of<Op> |
|---|
| 214 | { |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | // The comma is used in list-initialized sets, and the width of sets are 1 |
|---|
| 218 | template<typename Left, typename Right> |
|---|
| 219 | struct width_of<proto::binary_op<Left, Right, proto::comma_tag> > |
|---|
| 220 | : mpl::size_t<1> |
|---|
| 221 | { |
|---|
| 222 | }; |
|---|
| 223 | |
|---|
| 224 | // The subscript operator[] is used for sets, as in set['a' | range('b','h')], |
|---|
| 225 | // or for actions as in (any >> expr)[ action ] |
|---|
| 226 | template<typename Left, typename Right> |
|---|
| 227 | struct width_of<proto::binary_op<Left, Right, proto::subscript_tag> > |
|---|
| 228 | : mpl::if_<is_same<Left, set_initializer_type>, mpl::size_t<1>, width_of<Left> >::type |
|---|
| 229 | { |
|---|
| 230 | // If Left is "set" then make sure that Right has a width_of 1 |
|---|
| 231 | BOOST_MPL_ASSERT |
|---|
| 232 | (( |
|---|
| 233 | mpl::or_ |
|---|
| 234 | < |
|---|
| 235 | mpl::not_<is_same<Left, set_initializer_type> > |
|---|
| 236 | , mpl::equal_to<width_of<Right>, mpl::size_t<1> > |
|---|
| 237 | > |
|---|
| 238 | )); |
|---|
| 239 | }; |
|---|
| 240 | |
|---|
| 241 | // The width of a list of alternates is N if all the alternates have width N, otherwise unknown_width |
|---|
| 242 | template<typename Widths> |
|---|
| 243 | struct alt_width_of |
|---|
| 244 | : mpl::fold |
|---|
| 245 | < |
|---|
| 246 | Widths |
|---|
| 247 | , typename mpl::front<Widths>::type |
|---|
| 248 | , equal_width<mpl::_1, mpl::_2> |
|---|
| 249 | >::type |
|---|
| 250 | { |
|---|
| 251 | }; |
|---|
| 252 | |
|---|
| 253 | template<typename Alternates> |
|---|
| 254 | struct width_of<alternates_list<Alternates> > |
|---|
| 255 | : alt_width_of<mpl::transform_view<Alternates, width_of<mpl::_1> > > |
|---|
| 256 | { |
|---|
| 257 | }; |
|---|
| 258 | |
|---|
| 259 | template<typename Op, typename Arg> |
|---|
| 260 | struct width_of<proto::op_proxy<Op, Arg> > |
|---|
| 261 | : width_of<Op> |
|---|
| 262 | { |
|---|
| 263 | }; |
|---|
| 264 | |
|---|
| 265 | }}} // namespace boost::xpressive::detail |
|---|
| 266 | |
|---|
| 267 | #endif |
|---|