1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 http://spirit.sourceforge.net/
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_CONTAINER_FEBRUARY_06_2007_1001AM)
10 #define BOOST_SPIRIT_CONTAINER_FEBRUARY_06_2007_1001AM
16 #include <boost/spirit/home/support/unused.hpp>
17 #include <boost/spirit/home/support/attributes_fwd.hpp>
18 #include <boost/detail/iterator.hpp> // for boost::detail::iterator_traits
19 #include <boost/mpl/has_xxx.hpp>
20 #include <boost/mpl/bool.hpp>
21 #include <boost/optional.hpp>
22 #include <boost/variant.hpp>
23 #include <boost/preprocessor/cat.hpp>
24 #include <boost/preprocessor/repeat.hpp>
25 #include <boost/range/iterator_range.hpp>
27 namespace boost { namespace spirit { namespace traits
29 ///////////////////////////////////////////////////////////////////////////
30 // This file contains some container utils for stl containers. The
31 // utilities provided also accept spirit's unused_type; all no-ops.
32 // Compiler optimization will easily strip these away.
33 ///////////////////////////////////////////////////////////////////////////
37 BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type)
38 BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator)
39 BOOST_MPL_HAS_XXX_TRAIT_DEF(size_type)
40 BOOST_MPL_HAS_XXX_TRAIT_DEF(reference)
43 template <typename T, typename Enable/* = void*/>
46 detail::has_value_type<T>::value &&
47 detail::has_iterator<T>::value &&
48 detail::has_size_type<T>::value &&
49 detail::has_reference<T>::value>
53 struct is_container<T&>
58 struct is_container<boost::optional<T> >
62 #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
64 struct is_container<boost::variant<T> >
68 template<typename T0, typename T1, typename ...TN>
69 struct is_container<boost::variant<T0, T1, TN...> >
70 : mpl::bool_<is_container<T0>::value ||
71 is_container<boost::variant<T1, TN...> >::value>
75 #define BOOST_SPIRIT_IS_CONTAINER(z, N, data) \
76 is_container<BOOST_PP_CAT(T, N)>::value || \
79 // make sure unused variant parameters do not affect the outcome
81 struct is_container<boost::detail::variant::void_>
85 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
86 struct is_container<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
87 : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES
88 , BOOST_SPIRIT_IS_CONTAINER, _) false>
91 #undef BOOST_SPIRIT_IS_CONTAINER
94 template <typename T, typename Enable/* = void*/>
95 struct is_iterator_range
100 struct is_iterator_range<iterator_range<T> >
104 ///////////////////////////////////////////////////////////////////////////
107 template <typename T>
108 struct remove_value_const
113 template <typename T>
114 struct remove_value_const<T const>
115 : remove_value_const<T>
118 template <typename F, typename S>
119 struct remove_value_const<std::pair<F, S> >
121 typedef typename remove_value_const<F>::type first_type;
122 typedef typename remove_value_const<S>::type second_type;
123 typedef std::pair<first_type, second_type> type;
127 ///////////////////////////////////////////////////////////////////////
128 //[customization_container_value_default
129 template <typename Container, typename Enable/* = void*/>
130 struct container_value
131 : detail::remove_value_const<typename Container::value_type>
135 template <typename T>
136 struct container_value<T&>
140 // this will be instantiated if the optional holds a container
141 template <typename T>
142 struct container_value<boost::optional<T> >
146 // this will be instantiated if the variant holds a container
147 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
148 struct container_value<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
151 variant<BOOST_VARIANT_ENUM_PARAMS(T)>::types
154 mpl::find_if<types, is_container<mpl::_1> >::type
157 typedef typename container_value<
159 is_same<iter, typename mpl::end<types>::type>
160 , unused_type, typename mpl::deref<iter>::type
165 //[customization_container_value_unused
167 struct container_value<unused_type>
169 typedef unused_type type;
174 struct container_value<unused_type const>
176 typedef unused_type type;
179 ///////////////////////////////////////////////////////////////////////////
180 template <typename Container, typename Enable/* = void*/>
181 struct container_iterator
183 typedef typename Container::iterator type;
186 template <typename Container>
187 struct container_iterator<Container&>
188 : container_iterator<Container>
191 template <typename Container>
192 struct container_iterator<Container const>
194 typedef typename Container::const_iterator type;
197 template <typename T>
198 struct container_iterator<optional<T> >
199 : container_iterator<T>
202 template <typename T>
203 struct container_iterator<optional<T> const>
204 : container_iterator<T const>
207 template <typename Iterator>
208 struct container_iterator<iterator_range<Iterator> >
210 typedef typename range_const_iterator<
211 iterator_range<Iterator> >::type type;
215 struct container_iterator<unused_type>
217 typedef unused_type const* type;
221 struct container_iterator<unused_type const>
223 typedef unused_type const* type;
226 ///////////////////////////////////////////////////////////////////////////
227 template <typename T, typename Enable/* = void*/>
228 struct optional_attribute
230 typedef T const& type;
232 static type call(T const& val)
237 static bool is_valid(T const&)
243 template <typename T>
244 struct optional_attribute<boost::optional<T> >
246 typedef T const& type;
248 static type call(boost::optional<T> const& val)
250 return boost::get<T>(val);
253 static bool is_valid(boost::optional<T> const& val)
259 template <typename T>
260 typename optional_attribute<T>::type
261 optional_value(T const& val)
263 return optional_attribute<T>::call(val);
266 inline unused_type optional_value(unused_type)
271 template <typename T>
272 bool has_optional_value(T const& val)
274 return optional_attribute<T>::is_valid(val);
277 inline bool has_optional_value(unused_type)
282 ///////////////////////////////////////////////////////////////////////////
283 template <typename Container, typename T>
284 bool push_back(Container& c, T const& val);
286 //[customization_push_back_default
287 template <typename Container, typename T, typename Enable/* = void*/>
288 struct push_back_container
290 static bool call(Container& c, T const& val)
292 c.insert(c.end(), val);
298 template <typename Container, typename T>
299 struct push_back_container<optional<Container>, T>
301 static bool call(boost::optional<Container>& c, T const& val)
305 return push_back(boost::get<Container>(c), val);
311 template <typename T>
312 struct push_back_visitor : public static_visitor<>
314 typedef bool result_type;
316 push_back_visitor(T const& t) : t_(t) {}
318 template <typename Container>
319 bool push_back_impl(Container& c, mpl::true_) const
321 return push_back(c, t_);
324 template <typename T_>
325 bool push_back_impl(T_&, mpl::false_) const
327 // this variant doesn't hold a container
328 BOOST_ASSERT(false && "This variant doesn't hold a container");
332 template <typename T_>
333 bool operator()(T_& c) const
335 return push_back_impl(c, typename is_container<T_>::type());
342 template <BOOST_VARIANT_ENUM_PARAMS(typename T_), typename T>
343 struct push_back_container<variant<BOOST_VARIANT_ENUM_PARAMS(T_)>, T>
345 static bool call(variant<BOOST_VARIANT_ENUM_PARAMS(T_)>& c, T const& val)
347 return apply_visitor(detail::push_back_visitor<T>(val), c);
351 template <typename Container, typename T>
352 bool push_back(Container& c, T const& val)
354 return push_back_container<Container, T>::call(c, val);
357 //[customization_push_back_unused
358 template <typename Container>
359 bool push_back(Container&, unused_type)
365 template <typename T>
366 bool push_back(unused_type, T const&)
371 inline bool push_back(unused_type, unused_type)
376 ///////////////////////////////////////////////////////////////////////////
377 template <typename Container, typename Enable/* = void*/>
378 struct is_empty_container
380 static bool call(Container const& c)
386 template <typename Container>
387 bool is_empty(Container const& c)
389 return is_empty_container<Container>::call(c);
392 inline bool is_empty(unused_type)
397 ///////////////////////////////////////////////////////////////////////////
398 // Ensure the attribute is actually a container type
399 template <typename Container, typename Enable/* = void*/>
400 struct make_container_attribute
402 static void call(Container&)
404 // for static types this function does nothing
408 template <typename T>
409 void make_container(T& t)
411 make_container_attribute<T>::call(t);
414 inline void make_container(unused_type)
418 ///////////////////////////////////////////////////////////////////////////
419 template <typename Container, typename Enable/* = void*/>
420 struct begin_container
422 static typename container_iterator<Container>::type call(Container& c)
428 template <typename Container>
429 typename spirit::result_of::begin<Container>::type
432 return begin_container<Container>::call(c);
435 inline unused_type const*
441 ///////////////////////////////////////////////////////////////////////////
442 template <typename Container, typename Enable/* = void*/>
445 static typename container_iterator<Container>::type call(Container& c)
451 template <typename Container>
452 inline typename spirit::result_of::end<Container>::type
455 return end_container<Container>::call(c);
458 inline unused_type const*
464 ///////////////////////////////////////////////////////////////////////////
465 template <typename Iterator, typename Enable/* = void*/>
466 struct deref_iterator
468 typedef typename boost::detail::iterator_traits<Iterator>::reference type;
469 static type call(Iterator& it)
475 template <typename Iterator>
476 typename deref_iterator<Iterator>::type
479 return deref_iterator<Iterator>::call(it);
483 deref(unused_type const*)
488 ///////////////////////////////////////////////////////////////////////////
489 template <typename Iterator, typename Enable/* = void*/>
492 static void call(Iterator& it)
498 template <typename Iterator>
499 void next(Iterator& it)
501 next_iterator<Iterator>::call(it);
504 inline void next(unused_type const*)
509 ///////////////////////////////////////////////////////////////////////////
510 template <typename Iterator, typename Enable/* = void*/>
511 struct compare_iterators
513 static bool call(Iterator const& it1, Iterator const& it2)
519 template <typename Iterator>
520 bool compare(Iterator& it1, Iterator& it2)
522 return compare_iterators<Iterator>::call(it1, it2);
525 inline bool compare(unused_type const*, unused_type const*)
531 ///////////////////////////////////////////////////////////////////////////////
532 namespace boost { namespace spirit { namespace result_of
534 ///////////////////////////////////////////////////////////////////////////
535 template <typename T>
536 struct optional_value
541 template <typename T>
542 struct optional_value<boost::optional<T> >
547 template <typename T>
548 struct optional_value<boost::optional<T> const>
550 typedef T const type;
554 struct optional_value<unused_type>
556 typedef unused_type type;
560 struct optional_value<unused_type const>
562 typedef unused_type type;
565 ///////////////////////////////////////////////////////////////////////////
566 template <typename Container>
568 : traits::container_iterator<Container>
571 template <typename Container>
573 : traits::container_iterator<Container>
576 template <typename Iterator>
578 : traits::deref_iterator<Iterator>
582 struct deref<unused_type const*>
584 typedef unused_type type;