| 1 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // cons.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_UTILITY_CONS_HPP_EAN_11_19_2005 |
|---|
| 9 | #define BOOST_XPRESSIVE_DETAIL_UTILITY_CONS_HPP_EAN_11_19_2005 |
|---|
| 10 | |
|---|
| 11 | #include <boost/version.hpp> |
|---|
| 12 | |
|---|
| 13 | #if BOOST_VERSION >= 103300 |
|---|
| 14 | |
|---|
| 15 | // In Boost 1.33+, we have a cons list in Fusion, so just include it. |
|---|
| 16 | # include <boost/spirit/fusion/sequence/cons.hpp> |
|---|
| 17 | |
|---|
| 18 | #else |
|---|
| 19 | |
|---|
| 20 | // For earlier versions of Boost, put the definition of cons here |
|---|
| 21 | # include <boost/call_traits.hpp> |
|---|
| 22 | # include <boost/mpl/if.hpp> |
|---|
| 23 | # include <boost/mpl/eval_if.hpp> |
|---|
| 24 | # include <boost/mpl/identity.hpp> |
|---|
| 25 | # include <boost/type_traits/is_const.hpp> |
|---|
| 26 | # include <boost/type_traits/add_const.hpp> |
|---|
| 27 | # include <boost/type_traits/add_reference.hpp> |
|---|
| 28 | # include <boost/spirit/fusion/detail/config.hpp> |
|---|
| 29 | # include <boost/spirit/fusion/detail/access.hpp> |
|---|
| 30 | # include <boost/spirit/fusion/iterator/next.hpp> |
|---|
| 31 | # include <boost/spirit/fusion/iterator/equal_to.hpp> |
|---|
| 32 | # include <boost/spirit/fusion/iterator/as_fusion_iterator.hpp> |
|---|
| 33 | # include <boost/spirit/fusion/iterator/detail/iterator_base.hpp> |
|---|
| 34 | # include <boost/spirit/fusion/sequence/begin.hpp> |
|---|
| 35 | # include <boost/spirit/fusion/sequence/end.hpp> |
|---|
| 36 | # include <boost/spirit/fusion/sequence/as_fusion_sequence.hpp> |
|---|
| 37 | # include <boost/spirit/fusion/sequence/detail/sequence_base.hpp> |
|---|
| 38 | |
|---|
| 39 | namespace boost { namespace fusion |
|---|
| 40 | { |
|---|
| 41 | struct nil; |
|---|
| 42 | |
|---|
| 43 | struct cons_tag; |
|---|
| 44 | |
|---|
| 45 | template <typename Car, typename Cdr> |
|---|
| 46 | struct cons; |
|---|
| 47 | |
|---|
| 48 | struct cons_iterator_tag; |
|---|
| 49 | |
|---|
| 50 | template <typename Cons> |
|---|
| 51 | struct cons_iterator; |
|---|
| 52 | |
|---|
| 53 | namespace cons_detail |
|---|
| 54 | { |
|---|
| 55 | template <typename Iterator> |
|---|
| 56 | struct deref_traits_impl |
|---|
| 57 | { |
|---|
| 58 | typedef typename Iterator::cons_type cons_type; |
|---|
| 59 | typedef typename cons_type::car_type value_type; |
|---|
| 60 | |
|---|
| 61 | typedef typename mpl::eval_if< |
|---|
| 62 | is_const<cons_type> |
|---|
| 63 | , add_reference<typename add_const<value_type>::type> |
|---|
| 64 | , add_reference<value_type> >::type |
|---|
| 65 | type; |
|---|
| 66 | |
|---|
| 67 | static type |
|---|
| 68 | call(Iterator const& i) |
|---|
| 69 | { |
|---|
| 70 | return detail::ref(i.cons.car); |
|---|
| 71 | } |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | template <typename Iterator> |
|---|
| 75 | struct next_traits_impl |
|---|
| 76 | { |
|---|
| 77 | typedef typename Iterator::cons_type cons_type; |
|---|
| 78 | typedef typename cons_type::cdr_type cdr_type; |
|---|
| 79 | |
|---|
| 80 | typedef cons_iterator< |
|---|
| 81 | typename mpl::eval_if< |
|---|
| 82 | is_const<cons_type> |
|---|
| 83 | , add_const<cdr_type> |
|---|
| 84 | , mpl::identity<cdr_type> |
|---|
| 85 | >::type> |
|---|
| 86 | type; |
|---|
| 87 | |
|---|
| 88 | static type |
|---|
| 89 | call(Iterator const& i) |
|---|
| 90 | { |
|---|
| 91 | return type(detail::ref(i.cons.cdr)); |
|---|
| 92 | } |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | template <typename Iterator> |
|---|
| 96 | struct value_traits_impl |
|---|
| 97 | { |
|---|
| 98 | typedef typename Iterator::cons_type cons_type; |
|---|
| 99 | typedef typename cons_type::car_type type; |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | template <typename Cons> |
|---|
| 103 | struct begin_traits_impl |
|---|
| 104 | { |
|---|
| 105 | typedef cons_iterator<Cons> type; |
|---|
| 106 | |
|---|
| 107 | static type |
|---|
| 108 | call(Cons& t) |
|---|
| 109 | { |
|---|
| 110 | return type(t); |
|---|
| 111 | } |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | template <typename Cons> |
|---|
| 115 | struct end_traits_impl |
|---|
| 116 | { |
|---|
| 117 | typedef cons_iterator< |
|---|
| 118 | typename mpl::if_<is_const<Cons>, nil const, nil>::type> |
|---|
| 119 | type; |
|---|
| 120 | |
|---|
| 121 | static type |
|---|
| 122 | call(Cons& t) |
|---|
| 123 | { |
|---|
| 124 | FUSION_RETURN_DEFAULT_CONSTRUCTED; |
|---|
| 125 | } |
|---|
| 126 | }; |
|---|
| 127 | } // namespace cons_detail |
|---|
| 128 | |
|---|
| 129 | namespace meta |
|---|
| 130 | { |
|---|
| 131 | template <typename Tag> |
|---|
| 132 | struct deref_impl; |
|---|
| 133 | |
|---|
| 134 | template <> |
|---|
| 135 | struct deref_impl<cons_iterator_tag> |
|---|
| 136 | { |
|---|
| 137 | template <typename Iterator> |
|---|
| 138 | struct apply : cons_detail::deref_traits_impl<Iterator> {}; |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | template <typename Tag> |
|---|
| 142 | struct next_impl; |
|---|
| 143 | |
|---|
| 144 | template <> |
|---|
| 145 | struct next_impl<cons_iterator_tag> |
|---|
| 146 | { |
|---|
| 147 | template <typename Iterator> |
|---|
| 148 | struct apply : cons_detail::next_traits_impl<Iterator> {}; |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | template <typename Tag> |
|---|
| 152 | struct value_impl; |
|---|
| 153 | |
|---|
| 154 | template <> |
|---|
| 155 | struct value_impl<cons_iterator_tag> |
|---|
| 156 | { |
|---|
| 157 | template <typename Iterator> |
|---|
| 158 | struct apply : cons_detail::value_traits_impl<Iterator> {}; |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | template <typename Tag> |
|---|
| 162 | struct begin_impl; |
|---|
| 163 | |
|---|
| 164 | template <> |
|---|
| 165 | struct begin_impl<cons_tag> |
|---|
| 166 | { |
|---|
| 167 | template <typename Sequence> |
|---|
| 168 | struct apply : cons_detail::begin_traits_impl<Sequence> |
|---|
| 169 | {}; |
|---|
| 170 | }; |
|---|
| 171 | |
|---|
| 172 | template <typename Tag> |
|---|
| 173 | struct end_impl; |
|---|
| 174 | |
|---|
| 175 | template <> |
|---|
| 176 | struct end_impl<cons_tag> |
|---|
| 177 | { |
|---|
| 178 | template <typename Sequence> |
|---|
| 179 | struct apply : cons_detail::end_traits_impl<Sequence> |
|---|
| 180 | {}; |
|---|
| 181 | }; |
|---|
| 182 | } // namespace meta |
|---|
| 183 | |
|---|
| 184 | template <typename Cons = nil> |
|---|
| 185 | struct cons_iterator : iterator_base<cons_iterator<Cons> > |
|---|
| 186 | { |
|---|
| 187 | typedef cons_iterator_tag tag; |
|---|
| 188 | typedef Cons cons_type; |
|---|
| 189 | |
|---|
| 190 | explicit cons_iterator(cons_type& cons_) |
|---|
| 191 | : cons(cons_) {} |
|---|
| 192 | |
|---|
| 193 | cons_type& cons; |
|---|
| 194 | }; |
|---|
| 195 | |
|---|
| 196 | template <> |
|---|
| 197 | struct cons_iterator<nil> : iterator_base<cons_iterator<nil> > |
|---|
| 198 | { |
|---|
| 199 | typedef cons_iterator_tag tag; |
|---|
| 200 | typedef nil cons_type; |
|---|
| 201 | cons_iterator() {} |
|---|
| 202 | explicit cons_iterator(nil const&) {} |
|---|
| 203 | }; |
|---|
| 204 | |
|---|
| 205 | template <> |
|---|
| 206 | struct cons_iterator<nil const> : iterator_base<cons_iterator<nil const> > |
|---|
| 207 | { |
|---|
| 208 | typedef cons_iterator_tag tag; |
|---|
| 209 | typedef nil const cons_type; |
|---|
| 210 | cons_iterator() {} |
|---|
| 211 | explicit cons_iterator(nil const&) {} |
|---|
| 212 | }; |
|---|
| 213 | |
|---|
| 214 | struct nil : sequence_base<nil> |
|---|
| 215 | { |
|---|
| 216 | typedef cons_tag tag; |
|---|
| 217 | typedef void_t car_type; |
|---|
| 218 | typedef void_t cdr_type; |
|---|
| 219 | }; |
|---|
| 220 | |
|---|
| 221 | template <typename Car, typename Cdr = nil> |
|---|
| 222 | struct cons : sequence_base<cons<Car,Cdr> > |
|---|
| 223 | { |
|---|
| 224 | typedef cons_tag tag; |
|---|
| 225 | typedef typename call_traits<Car>::value_type car_type; |
|---|
| 226 | typedef Cdr cdr_type; |
|---|
| 227 | |
|---|
| 228 | cons() |
|---|
| 229 | : car(), cdr() {} |
|---|
| 230 | |
|---|
| 231 | explicit cons( |
|---|
| 232 | typename call_traits<Car>::param_type car_ |
|---|
| 233 | , typename call_traits<Cdr>::param_type cdr_ = Cdr()) |
|---|
| 234 | : car(car_), cdr(cdr_) {} |
|---|
| 235 | |
|---|
| 236 | car_type car; |
|---|
| 237 | cdr_type cdr; |
|---|
| 238 | }; |
|---|
| 239 | |
|---|
| 240 | template <typename Car> |
|---|
| 241 | inline cons<Car> |
|---|
| 242 | make_cons(Car const& car) |
|---|
| 243 | { |
|---|
| 244 | return cons<Car>(car); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | template <typename Car, typename Cdr> |
|---|
| 248 | inline cons<Car, Cdr> |
|---|
| 249 | make_cons(Car const& car, Cdr const& cdr) |
|---|
| 250 | { |
|---|
| 251 | return cons<Car, Cdr>(car, cdr); |
|---|
| 252 | } |
|---|
| 253 | }} // namespace boost::fusion |
|---|
| 254 | |
|---|
| 255 | namespace boost { namespace mpl |
|---|
| 256 | { |
|---|
| 257 | template <typename Tag> |
|---|
| 258 | struct begin_impl; |
|---|
| 259 | |
|---|
| 260 | template <typename Tag> |
|---|
| 261 | struct end_impl; |
|---|
| 262 | |
|---|
| 263 | template <> |
|---|
| 264 | struct begin_impl<fusion::cons_tag> |
|---|
| 265 | : fusion::meta::begin_impl<fusion::cons_tag> |
|---|
| 266 | { |
|---|
| 267 | }; |
|---|
| 268 | |
|---|
| 269 | template <> |
|---|
| 270 | struct end_impl<fusion::cons_tag> |
|---|
| 271 | : fusion::meta::end_impl<fusion::cons_tag> |
|---|
| 272 | { |
|---|
| 273 | }; |
|---|
| 274 | |
|---|
| 275 | }} // namespace boost::mpl |
|---|
| 276 | |
|---|
| 277 | #endif |
|---|
| 278 | |
|---|
| 279 | // Before Boost v1.33.1, Fusion cons lists were not valid MPL sequences. |
|---|
| 280 | #if BOOST_VERSION < 103301 |
|---|
| 281 | namespace boost { namespace mpl |
|---|
| 282 | { |
|---|
| 283 | template<typename Iterator> |
|---|
| 284 | struct next; |
|---|
| 285 | |
|---|
| 286 | template<typename Cons> |
|---|
| 287 | struct next<fusion::cons_iterator<Cons> > |
|---|
| 288 | : fusion::cons_detail::next_traits_impl<fusion::cons_iterator<Cons> > |
|---|
| 289 | { |
|---|
| 290 | }; |
|---|
| 291 | |
|---|
| 292 | template<typename Iterator> |
|---|
| 293 | struct deref; |
|---|
| 294 | |
|---|
| 295 | template<typename Cons> |
|---|
| 296 | struct deref<fusion::cons_iterator<Cons> > |
|---|
| 297 | : fusion::cons_detail::value_traits_impl<fusion::cons_iterator<Cons> > |
|---|
| 298 | { |
|---|
| 299 | }; |
|---|
| 300 | |
|---|
| 301 | }} // namespace boost::mpl |
|---|
| 302 | #endif |
|---|
| 303 | |
|---|
| 304 | #endif |
|---|