1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2012 Hartmut Kaiser
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #if !defined(BOOST_SPIRIT_ATTRIBUTES_JANUARY_29_2007_0954AM)
9 #define BOOST_SPIRIT_ATTRIBUTES_JANUARY_29_2007_0954AM
15 #include <boost/spirit/home/support/unused.hpp>
16 #include <boost/spirit/home/support/has_semantic_action.hpp>
17 #include <boost/spirit/home/support/attributes_fwd.hpp>
18 #include <boost/spirit/home/support/container.hpp>
19 #include <boost/spirit/home/support/detail/hold_any.hpp>
20 #include <boost/spirit/home/support/detail/as_variant.hpp>
21 #include <boost/optional/optional.hpp>
22 #include <boost/fusion/include/transform.hpp>
23 #include <boost/fusion/include/filter_if.hpp>
24 #include <boost/fusion/include/as_vector.hpp>
25 #include <boost/fusion/include/push_front.hpp>
26 #include <boost/fusion/include/pop_front.hpp>
27 #include <boost/fusion/include/is_sequence.hpp>
28 #include <boost/fusion/include/for_each.hpp>
29 #include <boost/fusion/include/is_view.hpp>
30 #include <boost/fusion/include/mpl.hpp>
31 #include <boost/foreach.hpp>
32 #include <boost/utility/value_init.hpp>
33 #include <boost/type_traits/is_same.hpp>
34 #include <boost/type_traits/is_convertible.hpp>
35 #include <boost/mpl/eval_if.hpp>
36 #include <boost/mpl/end.hpp>
37 #include <boost/mpl/find_if.hpp>
38 #include <boost/mpl/identity.hpp>
39 #include <boost/mpl/deref.hpp>
40 #include <boost/mpl/distance.hpp>
41 #include <boost/mpl/or.hpp>
42 #include <boost/mpl/has_xxx.hpp>
43 #include <boost/mpl/equal.hpp>
44 #include <boost/proto/proto_fwd.hpp>
45 #include <boost/utility/enable_if.hpp>
46 #include <boost/variant.hpp>
47 #include <boost/range/iterator_range.hpp>
48 #include <boost/config.hpp>
53 ///////////////////////////////////////////////////////////////////////////////
54 namespace boost { namespace spirit { namespace traits
56 ///////////////////////////////////////////////////////////////////////////
57 // This file deals with attribute related functions and meta-functions
58 // including generalized attribute transformation utilities for Spirit
60 ///////////////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////////////
63 // Find out if T can be a (strong) substitute for Expected attribute
66 template <typename T, typename Expected>
67 struct value_type_is_substitute
69 typename container_value<T>::type
70 , typename container_value<Expected>::type>
73 template <typename T, typename Expected, typename Enable = void>
74 struct is_substitute_impl : is_same<T, Expected> {};
76 template <typename T, typename Expected>
77 struct is_substitute_impl<T, Expected,
80 fusion::traits::is_sequence<T>,
81 fusion::traits::is_sequence<Expected>,
82 mpl::equal<T, Expected, is_substitute<mpl::_1, mpl::_2> >
87 template <typename T, typename Expected>
88 struct is_substitute_impl<T, Expected,
92 is_container<Expected>,
93 detail::value_type_is_substitute<T, Expected>
99 template <typename T, typename Expected, typename Enable /*= void*/>
101 : detail::is_substitute_impl<T, Expected> {};
103 template <typename T, typename Expected>
104 struct is_substitute<optional<T>, optional<Expected> >
105 : is_substitute<T, Expected> {};
107 template <typename T>
108 struct is_substitute<T, T
109 , typename enable_if<not_is_optional<T> >::type>
112 ///////////////////////////////////////////////////////////////////////////
113 // Find out if T can be a weak substitute for Expected attribute
116 // A type, which is convertible to the attribute is at the same time
117 // usable as its weak substitute.
118 template <typename T, typename Expected, typename Enable = void>
119 struct is_weak_substitute_impl : is_convertible<T, Expected> {};
121 // // An exposed attribute is a weak substitute for a supplied container
122 // // attribute if it is a weak substitute for its value_type. This is
123 // // true as all character parsers are compatible with a container
124 // // attribute having the corresponding character type as its value_type.
125 // template <typename T, typename Expected>
126 // struct is_weak_substitute_for_value_type
127 // : is_weak_substitute<T, typename container_value<Expected>::type>
130 // template <typename T, typename Expected>
131 // struct is_weak_substitute_impl<T, Expected,
132 // typename enable_if<
134 // mpl::not_<is_string<T> >
135 // , is_string<Expected>
136 // , is_weak_substitute_for_value_type<T, Expected> >
141 // An exposed container attribute is a weak substitute for a supplied
142 // container attribute if and only if their value_types are weak
144 template <typename T, typename Expected>
145 struct value_type_is_weak_substitute
146 : is_weak_substitute<
147 typename container_value<T>::type
148 , typename container_value<Expected>::type>
151 template <typename T, typename Expected>
152 struct is_weak_substitute_impl<T, Expected,
156 , is_container<Expected>
157 , value_type_is_weak_substitute<T, Expected> >
161 // Two fusion sequences are weak substitutes if and only if their
162 // elements are pairwise weak substitutes.
163 template <typename T, typename Expected>
164 struct is_weak_substitute_impl<T, Expected,
167 fusion::traits::is_sequence<T>
168 , fusion::traits::is_sequence<Expected>
169 , mpl::equal<T, Expected, is_weak_substitute<mpl::_1, mpl::_2> > >
173 // If this is not defined, the main template definition above will return
174 // true if T is convertible to the first type in a fusion::vector. We
175 // globally declare any non-Fusion sequence T as not compatible with any
176 // Fusion sequence 'Expected'.
177 template <typename T, typename Expected>
178 struct is_weak_substitute_impl<T, Expected,
181 mpl::not_<fusion::traits::is_sequence<T> >
182 , fusion::traits::is_sequence<Expected> >
187 // main template forwards to detail namespace, this helps older compilers
188 // to disambiguate things
189 template <typename T, typename Expected, typename Enable /*= void*/>
190 struct is_weak_substitute
191 : detail::is_weak_substitute_impl<T, Expected> {};
193 template <typename T, typename Expected>
194 struct is_weak_substitute<optional<T>, optional<Expected> >
195 : is_weak_substitute<T, Expected> {};
197 template <typename T, typename Expected>
198 struct is_weak_substitute<optional<T>, Expected>
199 : is_weak_substitute<T, Expected> {};
201 template <typename T, typename Expected>
202 struct is_weak_substitute<T, optional<Expected> >
203 : is_weak_substitute<T, Expected> {};
205 #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
206 template <typename T, typename Expected>
207 struct is_weak_substitute<boost::variant<T>, Expected>
208 : is_weak_substitute<T, Expected>
211 template <typename T0, typename T1, typename ...TN, typename Expected>
212 struct is_weak_substitute<boost::variant<T0, T1, TN...>,
214 : mpl::bool_<is_weak_substitute<T0, Expected>::type::value &&
215 is_weak_substitute<boost::variant<T1, TN...>, Expected>::type::value>
218 #define BOOST_SPIRIT_IS_WEAK_SUBSTITUTE(z, N, _) \
219 is_weak_substitute<BOOST_PP_CAT(T, N), Expected>::type::value && \
222 // make sure unused variant parameters do not affect the outcome
223 template <typename Expected>
224 struct is_weak_substitute<boost::detail::variant::void_, Expected>
228 template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Expected>
229 struct is_weak_substitute<
230 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Expected>
231 : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES
232 , BOOST_SPIRIT_IS_WEAK_SUBSTITUTE, _) true>
235 #undef BOOST_SPIRIT_IS_WEAK_SUBSTITUTE
238 template <typename T>
239 struct is_weak_substitute<T, T
240 , typename enable_if<
241 mpl::and_<not_is_optional<T>, not_is_variant<T> >
245 ///////////////////////////////////////////////////////////////////////////
246 template <typename T, typename Enable/* = void*/>
247 struct is_proxy : mpl::false_ {};
249 template <typename T>
253 fusion::traits::is_sequence<T>,
254 fusion::traits::is_view<T>
261 // By declaring a nested struct in your class/struct, you tell
262 // spirit that it is regarded as a variant type. The minimum
263 // required interface for such a variant is that it has constructors
264 // for various types supported by your variant and a typedef 'types'
265 // which is an mpl sequence of the contained types.
267 // This is an intrusive interface. For a non-intrusive interface,
268 // use the not_is_variant trait.
269 BOOST_MPL_HAS_XXX_TRAIT_DEF(adapted_variant_tag)
272 template <typename T, typename Domain, typename Enable/* = void*/>
273 struct not_is_variant
274 : mpl::not_<detail::has_adapted_variant_tag<T> >
277 template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Domain>
278 struct not_is_variant<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Domain>
282 template <typename T, typename Domain>
283 struct not_is_variant<boost::optional<T>, Domain>
284 : not_is_variant<T, Domain>
287 // we treat every type as if it where the variant (as this meta function is
288 // invoked for variant types only)
289 template <typename T>
294 template <typename T>
295 struct variant_type<boost::optional<T> >
299 ///////////////////////////////////////////////////////////////////////////
300 // The compute_compatible_component_variant
301 ///////////////////////////////////////////////////////////////////////////
304 // A component is compatible to a given Attribute type if the
305 // Attribute is the same as the expected type of the component or if
306 // it is convertible to the expected type.
307 template <typename Expected, typename Attribute>
308 struct attribute_is_compatible
309 : is_convertible<Attribute, Expected>
312 template <typename Expected, typename Attribute>
313 struct attribute_is_compatible<Expected, boost::optional<Attribute> >
314 : is_convertible<Attribute, Expected>
317 template <typename Container>
318 struct is_hold_any_container
319 : traits::is_hold_any<typename traits::container_value<Container>::type>
323 template <typename Attribute, typename Expected
324 , typename IsNotVariant = mpl::false_, typename Enable = void>
325 struct compute_compatible_component_variant
327 traits::detail::attribute_is_compatible<Expected, Attribute>
328 , traits::is_hold_any<Expected>
330 is_container<Expected>
331 , traits::detail::is_hold_any_container<Expected>
337 BOOST_MPL_HAS_XXX_TRAIT_DEF(types)
340 template <typename Variant, typename Expected>
341 struct compute_compatible_component_variant<Variant, Expected, mpl::false_
342 , typename enable_if<detail::has_types<Variant> >::type>
344 typedef typename traits::variant_type<Variant>::type variant_type;
345 typedef typename variant_type::types types;
346 typedef typename mpl::end<types>::type end;
349 mpl::find_if<types, is_same<Expected, mpl::_1> >::type
352 typedef typename mpl::distance<
353 typename mpl::begin<types>::type, iter
356 // true_ if the attribute matches one of the types in the variant
357 typedef typename mpl::not_<is_same<iter, end> >::type type;
358 enum { value = type::value };
360 // return the type in the variant the attribute is compatible with
362 mpl::eval_if<type, mpl::deref<iter>, mpl::identity<unused_type> >::type
365 // return whether the given type is compatible with the Expected type
366 static bool is_compatible(int which)
368 return which == distance::value;
372 template <typename Expected, typename Attribute, typename Domain>
373 struct compute_compatible_component
374 : compute_compatible_component_variant<Attribute, Expected
375 , typename spirit::traits::not_is_variant<Attribute, Domain>::type> {};
377 template <typename Expected, typename Domain>
378 struct compute_compatible_component<Expected, unused_type, Domain>
381 template <typename Attribute, typename Domain>
382 struct compute_compatible_component<unused_type, Attribute, Domain>
385 template <typename Domain>
386 struct compute_compatible_component<unused_type, unused_type, Domain>
389 ///////////////////////////////////////////////////////////////////////////
390 // return the type currently stored in the given variant
391 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
392 struct variant_which<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
394 static int call(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& v)
400 template <typename T>
401 int which(T const& v)
403 return variant_which<T>::call(v);
406 ///////////////////////////////////////////////////////////////////////////
407 template <typename T, typename Domain, typename Enable/* = void*/>
408 struct not_is_optional
412 template <typename T, typename Domain>
413 struct not_is_optional<boost::optional<T>, Domain>
417 ///////////////////////////////////////////////////////////////////////////
420 // Get the component's attribute
421 ///////////////////////////////////////////////////////////////////////////
422 template <typename Component
423 , typename Context = unused_type, typename Iterator = unused_type>
426 typedef typename Component::template
427 attribute<Context, Iterator>::type type;
430 ///////////////////////////////////////////////////////////////////////////
431 // attribute_not_unused
433 // An mpl meta-function class that determines whether a component's
434 // attribute is not unused.
435 ///////////////////////////////////////////////////////////////////////////
436 template <typename Context, typename Iterator = unused_type>
437 struct attribute_not_unused
439 template <typename Component>
441 : not_is_unused<typename
442 attribute_of<Component, Context, Iterator>::type>
446 ///////////////////////////////////////////////////////////////////////////
447 // Retrieve the attribute type to use from the given type
449 // This is needed to extract the correct attribute type from proxy classes
450 // as utilized in FUSION_ADAPT_ADT et. al.
451 ///////////////////////////////////////////////////////////////////////////
452 template <typename Attribute, typename Enable/* = void*/>
453 struct attribute_type : mpl::identity<Attribute> {};
455 ///////////////////////////////////////////////////////////////////////////
456 // Retrieve the size of a fusion sequence (compile time)
457 ///////////////////////////////////////////////////////////////////////////
458 template <typename T>
460 : fusion::result_of::size<T>
464 struct sequence_size<unused_type>
468 ///////////////////////////////////////////////////////////////////////////
469 // Retrieve the size of an attribute (runtime)
470 ///////////////////////////////////////////////////////////////////////////
473 template <typename Attribute, typename Enable = void>
474 struct attribute_size_impl
476 typedef std::size_t type;
478 static type call(Attribute const&)
484 template <typename Attribute>
485 struct attribute_size_impl<Attribute
486 , typename enable_if<
488 fusion::traits::is_sequence<Attribute>
489 , mpl::not_<traits::is_container<Attribute> >
493 typedef typename fusion::result_of::size<Attribute>::value_type type;
495 static type call(Attribute const& attr)
497 return fusion::size(attr);
501 template <typename Attribute>
502 struct attribute_size_impl<Attribute
503 , typename enable_if<
505 traits::is_container<Attribute>
506 , mpl::not_<traits::is_iterator_range<Attribute> >
510 typedef typename Attribute::size_type type;
512 static type call(Attribute const& attr)
519 template <typename Attribute, typename Enable/* = void*/>
520 struct attribute_size
521 : detail::attribute_size_impl<Attribute>
524 template <typename Attribute>
525 struct attribute_size<optional<Attribute> >
527 typedef typename attribute_size<Attribute>::type type;
529 static type call(optional<Attribute> const& val)
533 return traits::size(val.get());
539 struct attribute_size_visitor : static_visitor<std::size_t>
541 template <typename T>
542 std::size_t operator()(T const& val) const
544 return spirit::traits::size(val);
549 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
550 struct attribute_size<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
552 typedef std::size_t type;
554 static type call(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& val)
556 return apply_visitor(detail::attribute_size_visitor(), val);
560 template <typename Iterator>
561 struct attribute_size<iterator_range<Iterator> >
563 typedef typename boost::detail::iterator_traits<Iterator>::
564 difference_type type;
566 static type call(iterator_range<Iterator> const& r)
568 return boost::detail::distance(r.begin(), r.end());
573 struct attribute_size<unused_type>
575 typedef std::size_t type;
577 static type call(unused_type)
583 template <typename Attribute>
584 typename attribute_size<Attribute>::type
585 size (Attribute const& attr)
587 return attribute_size<Attribute>::call(attr);
590 ///////////////////////////////////////////////////////////////////////////
593 // Determines how we pass attributes to semantic actions. This
594 // may be specialized. By default, all attributes are wrapped in
595 // a fusion sequence, because the attribute has to be treated as being
596 // a single value in any case (even if it actually already is a fusion
597 // sequence in its own).
598 ///////////////////////////////////////////////////////////////////////////
599 template <typename Component, typename Attribute, typename Enable/* = void*/>
600 struct pass_attribute
602 typedef fusion::vector1<Attribute&> type;
605 ///////////////////////////////////////////////////////////////////////////
606 // Subclass a pass_attribute specialization from this to wrap
607 // the attribute in a tuple only IFF it is not already a fusion tuple.
608 ///////////////////////////////////////////////////////////////////////////
609 template <typename Attribute, typename Force = mpl::false_>
610 struct wrap_if_not_tuple
612 fusion::traits::is_sequence<Attribute>
613 , Attribute&, fusion::vector1<Attribute&> >
616 template <typename Attribute>
617 struct wrap_if_not_tuple<Attribute, mpl::true_>
619 typedef fusion::vector1<Attribute&> type;
623 struct wrap_if_not_tuple<unused_type, mpl::false_>
625 typedef unused_type type;
629 struct wrap_if_not_tuple<unused_type const, mpl::false_>
631 typedef unused_type type;
634 ///////////////////////////////////////////////////////////////////////////
637 // Build a boost::optional from T. Return unused_type if T is unused_type.
638 ///////////////////////////////////////////////////////////////////////////
639 template <typename T>
640 struct build_optional
642 typedef boost::optional<T> type;
645 template <typename T>
646 struct build_optional<boost::optional<T> >
648 typedef boost::optional<T> type;
652 struct build_optional<unused_type>
654 typedef unused_type type;
657 ///////////////////////////////////////////////////////////////////////////
660 // Build a std::vector from T. Return unused_type if T is unused_type.
661 ///////////////////////////////////////////////////////////////////////////
662 template <typename T>
663 struct build_std_vector
665 typedef std::vector<T> type;
669 struct build_std_vector<unused_type>
671 typedef unused_type type;
674 ///////////////////////////////////////////////////////////////////////////
675 // filter_unused_attributes
677 // Remove unused_types from a sequence
678 ///////////////////////////////////////////////////////////////////////////
680 // Compute the list of all *used* attributes of sub-components
681 // (filter all unused attributes from the list)
682 template <typename Sequence>
683 struct filter_unused_attributes
684 : fusion::result_of::filter_if<Sequence, not_is_unused<mpl::_> >
687 ///////////////////////////////////////////////////////////////////////////
688 // sequence_attribute_transform
690 // This transform is invoked for every attribute in a sequence allowing
691 // to modify the attribute type exposed by a component to the enclosing
692 // sequence component. By default no transformation is performed.
693 ///////////////////////////////////////////////////////////////////////////
694 template <typename Attribute, typename Domain>
695 struct sequence_attribute_transform
696 : mpl::identity<Attribute>
699 ///////////////////////////////////////////////////////////////////////////
700 // permutation_attribute_transform
702 // This transform is invoked for every attribute in a sequence allowing
703 // to modify the attribute type exposed by a component to the enclosing
704 // permutation component. By default a build_optional transformation is
706 ///////////////////////////////////////////////////////////////////////////
707 template <typename Attribute, typename Domain>
708 struct permutation_attribute_transform
709 : traits::build_optional<Attribute>
712 ///////////////////////////////////////////////////////////////////////////
713 // sequential_or_attribute_transform
715 // This transform is invoked for every attribute in a sequential_or allowing
716 // to modify the attribute type exposed by a component to the enclosing
717 // sequential_or component. By default a build_optional transformation is
719 ///////////////////////////////////////////////////////////////////////////
720 template <typename Attribute, typename Domain>
721 struct sequential_or_attribute_transform
722 : traits::build_optional<Attribute>
725 ///////////////////////////////////////////////////////////////////////////
726 // build_fusion_vector
728 // Build a fusion vector from a fusion sequence. All unused attributes
729 // are filtered out. If the result is empty after the removal of unused
730 // types, return unused_type. If the input sequence is an unused_type,
731 // also return unused_type.
732 ///////////////////////////////////////////////////////////////////////////
733 template <typename Sequence>
734 struct build_fusion_vector
736 // Remove all unused attributes
738 filter_unused_attributes<Sequence>::type
741 // Build a fusion vector from a fusion sequence (Sequence),
742 // But *only if* the sequence is not empty. i.e. if the
743 // sequence is empty, our result will be unused_type.
747 fusion::result_of::empty<filtered_attributes>
748 , mpl::identity<unused_type>
749 , fusion::result_of::as_vector<filtered_attributes>
755 struct build_fusion_vector<unused_type>
757 typedef unused_type type;
760 ///////////////////////////////////////////////////////////////////////////
761 // build_attribute_sequence
763 // Build a fusion sequence attribute sequence from a sequence of
764 // components. Transform<T>::type is called on each element.
765 ///////////////////////////////////////////////////////////////////////////
766 template <typename Sequence, typename Context
767 , template <typename T, typename D> class Transform
768 , typename Iterator = unused_type, typename Domain = unused_type>
769 struct build_attribute_sequence
771 struct element_attribute
773 template <typename T>
776 template <typename F, typename Element>
777 struct result<F(Element)>
781 typename attribute_of<Element, Context, Iterator>::type
787 // never called, but needed for decltype-based result_of (C++0x)
788 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
789 template <typename Element>
790 typename result<element_attribute(Element)>::type
791 operator()(Element&&) const;
795 // Compute the list of attributes of all sub-components
797 fusion::result_of::transform<Sequence, element_attribute>::type
801 ///////////////////////////////////////////////////////////////////////////
804 // Test if there are no unused attributes in Sequence
805 ///////////////////////////////////////////////////////////////////////////
806 template <typename Sequence>
809 typename mpl::find_if<Sequence, is_same<mpl::_, unused_type> >::type
810 , typename mpl::end<Sequence>::type>
815 template <typename Sequence, bool no_unused
816 , int size = mpl::size<Sequence>::value>
817 struct build_collapsed_variant;
819 // N element case, no unused
820 template <typename Sequence, int size>
821 struct build_collapsed_variant<Sequence, true, size>
822 : spirit::detail::as_variant<Sequence> {};
824 // N element case with unused
825 template <typename Sequence, int size>
826 struct build_collapsed_variant<Sequence, false, size>
828 typedef boost::optional<
829 typename spirit::detail::as_variant<
830 typename fusion::result_of::pop_front<Sequence>::type
835 // 1 element case, no unused
836 template <typename Sequence>
837 struct build_collapsed_variant<Sequence, true, 1>
838 : mpl::front<Sequence> {};
840 // 1 element case, with unused
841 template <typename Sequence>
842 struct build_collapsed_variant<Sequence, false, 1>
843 : mpl::front<Sequence> {};
845 // 2 element case, no unused
846 template <typename Sequence>
847 struct build_collapsed_variant<Sequence, true, 2>
848 : spirit::detail::as_variant<Sequence> {};
850 // 2 element case, with unused
851 template <typename Sequence>
852 struct build_collapsed_variant<Sequence, false, 2>
854 typedef boost::optional<
857 typename mpl::begin<Sequence>::type
865 ///////////////////////////////////////////////////////////////////////////
866 // alternative_attribute_transform
868 // This transform is invoked for every attribute in an alternative allowing
869 // to modify the attribute type exposed by a component to the enclosing
870 // alternative component. By default no transformation is performed.
871 ///////////////////////////////////////////////////////////////////////////
872 template <typename Attribute, typename Domain>
873 struct alternative_attribute_transform
874 : mpl::identity<Attribute>
877 ///////////////////////////////////////////////////////////////////////////
880 // Build a boost::variant from a fusion sequence. build_variant makes sure
881 // that 1) all attributes in the variant are unique 2) puts the unused
882 // attribute, if there is any, to the front and 3) collapses single element
883 // variants, variant<T> to T.
884 ///////////////////////////////////////////////////////////////////////////
885 template <typename Sequence>
888 // Remove all unused attributes.
890 filter_unused_attributes<Sequence>::type
893 typedef has_no_unused<Sequence> no_unused;
895 // If the original attribute list does not contain any unused
896 // attributes, it is used, otherwise a single unused_type is
897 // pushed to the front of the list. This is to make sure that if
898 // there is an unused_type in the list, it is the first one.
902 mpl::identity<Sequence>,
903 fusion::result_of::push_front<filtered_attributes, unused_type>
907 // Make sure each of the types occur only once in the type list
910 attribute_sequence, mpl::vector<>,
912 mpl::contains<mpl::_1, mpl::_2>,
913 mpl::_1, mpl::push_back<mpl::_1, mpl::_2>
918 // If there is only one type in the list of types we strip off the
919 // variant. IOTW, collapse single element variants, variant<T> to T.
920 // Take note that this also collapses variant<unused_type, T> to T.
922 traits::detail::build_collapsed_variant<
923 no_duplicates, no_unused::value>::type
927 ///////////////////////////////////////////////////////////////////////////
928 // transform_attribute
930 // Sometimes the user needs to transform the attribute types for certain
931 // attributes. This template can be used as a customization point, where
932 // the user is able specify specific transformation rules for any attribute
934 ///////////////////////////////////////////////////////////////////////////
935 template <typename Exposed, typename Transformed, typename Domain
936 , typename Enable/* = void*/>
937 struct transform_attribute;
939 ///////////////////////////////////////////////////////////////////////////
940 template <typename Domain, typename Transformed, typename Exposed>
941 typename spirit::result_of::pre_transform<Exposed, Transformed, Domain>::type
942 pre_transform(Exposed& attr BOOST_PROTO_DISABLE_IF_IS_CONST(Exposed))
944 return transform_attribute<Exposed, Transformed, Domain>::pre(attr);
947 template <typename Domain, typename Transformed, typename Exposed>
948 typename spirit::result_of::pre_transform<Exposed const, Transformed, Domain>::type
949 pre_transform(Exposed const& attr)
951 return transform_attribute<Exposed const, Transformed, Domain>::pre(attr);
954 ///////////////////////////////////////////////////////////////////////////
957 // All parsers and generators have specific attribute types.
958 // Spirit parsers and generators are passed an attribute; these are either
959 // references to the expected type, or an unused_type -- to flag that we do
960 // not care about the attribute. For semantic actions, however, we need to
961 // have a real value to pass to the semantic action. If the client did not
962 // provide one, we will have to synthesize the value. This class takes care
963 // of that. *Note that this behavior has changed. From Boost 1.47, semantic
964 // actions always take in the passed attribute as-is if the PP constant:
965 // BOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT is defined.
966 ///////////////////////////////////////////////////////////////////////////
967 template <typename Attribute, typename ActualAttribute>
968 struct make_attribute
970 typedef typename remove_const<Attribute>::type attribute_type;
973 is_same<typename remove_const<ActualAttribute>::type, unused_type>
975 , ActualAttribute&>::type
980 is_same<typename remove_const<ActualAttribute>::type, unused_type>
982 , ActualAttribute>::type
985 static Attribute call(unused_type)
987 // synthesize the attribute/parameter
988 return boost::get(value_initialized<attribute_type>());
991 template <typename T>
992 static T& call(T& value)
994 return value; // just pass the one provided
998 template <typename Attribute, typename ActualAttribute>
999 struct make_attribute<Attribute&, ActualAttribute>
1000 : make_attribute<Attribute, ActualAttribute>
1003 template <typename Attribute, typename ActualAttribute>
1004 struct make_attribute<Attribute const&, ActualAttribute>
1005 : make_attribute<Attribute const, ActualAttribute>
1008 template <typename ActualAttribute>
1009 struct make_attribute<unused_type, ActualAttribute>
1011 typedef unused_type type;
1012 typedef unused_type value_type;
1013 static unused_type call(unused_type)
1019 ///////////////////////////////////////////////////////////////////////////
1022 // Swap (with proper handling of unused_types)
1023 ///////////////////////////////////////////////////////////////////////////
1024 template <typename A, typename B>
1025 void swap_impl(A& a, B& b)
1032 template <typename T>
1033 void swap_impl(T& a, T& b)
1035 using namespace std;
1039 template <typename A>
1040 void swap_impl(A&, unused_type)
1044 template <typename A>
1045 void swap_impl(unused_type, A&)
1049 inline void swap_impl(unused_type, unused_type)
1053 ///////////////////////////////////////////////////////////////////////////
1054 // Strips single element fusion vectors into its 'naked'
1055 // form: vector<T> --> T
1056 ///////////////////////////////////////////////////////////////////////////
1057 template <typename T>
1058 struct strip_single_element_vector
1063 #if !defined(BOOST_FUSION_HAS_VARIADIC_VECTOR)
1064 template <typename T>
1065 struct strip_single_element_vector<fusion::vector1<T> >
1070 template <typename T>
1071 struct strip_single_element_vector<fusion::vector<T> >
1076 ///////////////////////////////////////////////////////////////////////////
1077 // meta function to return whether the argument is a one element fusion
1079 ///////////////////////////////////////////////////////////////////////////
1080 template <typename T
1081 , bool IsFusionSeq = fusion::traits::is_sequence<T>::value
1082 , bool IsProtoExpr = proto::is_expr<T>::value>
1083 struct one_element_sequence
1087 template <typename T>
1088 struct one_element_sequence<T, true, false>
1089 : mpl::bool_<mpl::size<T>::value == 1>
1092 ///////////////////////////////////////////////////////////////////////////
1095 // Clear data efficiently
1096 ///////////////////////////////////////////////////////////////////////////
1097 template <typename T>
1102 // this is used by the variant and fusion sequence dispatch
1103 struct clear_visitor : static_visitor<>
1105 template <typename T>
1106 void operator()(T& val) const
1108 spirit::traits::clear(val);
1113 template <typename T>
1114 void clear_impl2(T& val, mpl::false_)
1119 // for fusion sequences
1120 template <typename T>
1121 void clear_impl2(T& val, mpl::true_)
1123 fusion::for_each(val, clear_visitor());
1126 // dispatch default or fusion sequence
1127 template <typename T>
1128 void clear_impl(T& val, mpl::false_)
1130 clear_impl2(val, fusion::traits::is_sequence<T>());
1134 template <typename T>
1135 void clear_impl(T& val, mpl::true_)
1141 template <typename T, typename Enable/* = void*/>
1144 static void call(T& val)
1146 detail::clear_impl(val, typename is_container<T>::type());
1151 template <typename T>
1152 struct clear_value<boost::optional<T> >
1154 static void call(boost::optional<T>& val)
1157 val = none; // leave optional uninitialized
1162 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
1163 struct clear_value<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
1165 static void call(variant<BOOST_VARIANT_ENUM_PARAMS(T)>& val)
1167 apply_visitor(detail::clear_visitor(), val);
1172 template <typename T>
1173 struct clear_value<iterator_range<T> >
1175 static void call(iterator_range<T>& val)
1177 val = iterator_range<T>(val.end(), val.end());
1182 template <typename T>
1185 clear_value<T>::call(val);
1189 inline void clear(unused_type)
1193 ///////////////////////////////////////////////////////////////////////////
1196 template <typename Out>
1197 struct print_fusion_sequence
1199 print_fusion_sequence(Out& out_)
1200 : out(out_), is_first(true) {}
1202 typedef void result_type;
1204 template <typename T>
1205 void operator()(T const& val) const
1211 spirit::traits::print_attribute(out, val);
1215 mutable bool is_first;
1218 // print elements in a variant
1219 template <typename Out>
1220 struct print_visitor : static_visitor<>
1222 print_visitor(Out& out_) : out(out_) {}
1224 template <typename T>
1225 void operator()(T const& val) const
1227 spirit::traits::print_attribute(out, val);
1234 template <typename Out, typename T, typename Enable>
1235 struct print_attribute_debug
1237 // for plain data types
1238 template <typename T_>
1239 static void call_impl3(Out& out, T_ const& val, mpl::false_)
1244 // for fusion data types
1245 template <typename T_>
1246 static void call_impl3(Out& out, T_ const& val, mpl::true_)
1249 fusion::for_each(val, detail::print_fusion_sequence<Out>(out));
1253 // non-stl container
1254 template <typename T_>
1255 static void call_impl2(Out& out, T_ const& val, mpl::false_)
1257 call_impl3(out, val, fusion::traits::is_sequence<T_>());
1261 template <typename T_>
1262 static void call_impl2(Out& out, T_ const& val, mpl::true_)
1265 if (!traits::is_empty(val))
1268 typename container_iterator<T_ const>::type iend = traits::end(val);
1269 for (typename container_iterator<T_ const>::type i = traits::begin(val);
1270 !traits::compare(i, iend); traits::next(i))
1275 spirit::traits::print_attribute(out, traits::deref(i));
1281 // for variant types
1282 template <typename T_>
1283 static void call_impl(Out& out, T_ const& val, mpl::false_)
1285 apply_visitor(detail::print_visitor<Out>(out), val);
1288 // for non-variant types
1289 template <typename T_>
1290 static void call_impl(Out& out, T_ const& val, mpl::true_)
1292 call_impl2(out, val, is_container<T_>());
1296 static void call(Out& out, T const& val)
1298 call_impl(out, val, not_is_variant<T>());
1302 template <typename Out, typename T>
1303 struct print_attribute_debug<Out, boost::optional<T> >
1305 static void call(Out& out, boost::optional<T> const& val)
1308 spirit::traits::print_attribute(out, *val);
1314 ///////////////////////////////////////////////////////////////////////////
1315 template <typename Out, typename T>
1316 inline void print_attribute(Out& out, T const& val)
1318 print_attribute_debug<Out, T>::call(out, val);
1321 template <typename Out>
1322 inline void print_attribute(Out&, unused_type)
1326 ///////////////////////////////////////////////////////////////////////////
1327 // generate debug output for lookahead token (character) stream
1330 struct token_printer_debug_for_chars
1332 template<typename Out, typename Char>
1333 static void print(Out& o, Char c)
1335 using namespace std; // allow for ADL to find the proper iscntrl
1337 if (c == static_cast<Char>('\a'))
1339 else if (c == static_cast<Char>('\b'))
1341 else if (c == static_cast<Char>('\f'))
1343 else if (c == static_cast<Char>('\n'))
1345 else if (c == static_cast<Char>('\r'))
1347 else if (c == static_cast<Char>('\t'))
1349 else if (c == static_cast<Char>('\v'))
1351 else if (c >= 0 && c < 127 && iscntrl(c))
1352 o << "\\" << std::oct << static_cast<int>(c);
1354 o << static_cast<char>(c);
1358 // for token types where the comparison with char constants wouldn't work
1359 struct token_printer_debug
1361 template<typename Out, typename T>
1362 static void print(Out& o, T const& val)
1369 template <typename T, typename Enable>
1370 struct token_printer_debug
1373 is_convertible<T, char>, is_convertible<char, T> >
1374 , detail::token_printer_debug_for_chars
1375 , detail::token_printer_debug>::type
1378 template <typename Out, typename T>
1379 inline void print_token(Out& out, T const& val)
1381 // allow to customize the token printer routine
1382 token_printer_debug<T>::print(out, val);
1386 ///////////////////////////////////////////////////////////////////////////////
1387 namespace boost { namespace spirit { namespace result_of
1389 template <typename Exposed, typename Transformed, typename Domain>
1390 struct pre_transform
1391 : traits::transform_attribute<Exposed, Transformed, Domain>