// Copyright (c) 2001-2010 Hartmut Kaiser // Copyright (c) 2001-2010 Joel de Guzman // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #if !defined(SPIRIT_KARMA_SEQUENCE_FEB_28_2007_0247PM) #define SPIRIT_KARMA_SEQUENCE_FEB_28_2007_0247PM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace spirit { /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// template <> struct use_operator // enables << : mpl::true_ {}; template <> struct flatten_tree // flattens << : mpl::true_ {}; }} /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace spirit { namespace traits { // specialization for sequences template struct sequence_properties { struct element_properties { template struct result; template struct result { typedef properties_of type; }; // never called, but needed for decltype-based result_of (C++0x) template typename result::type operator()(Element&) const; }; typedef typename mpl::accumulate< typename fusion::result_of::transform< Elements, element_properties>::type , mpl::int_ , mpl::bitor_ >::type type; }; }}} /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace spirit { namespace karma { namespace detail { template struct attribute_size : fusion::result_of::size {}; template <> struct attribute_size : mpl::int_<0> {}; template inline typename enable_if< mpl::and_< fusion::traits::is_sequence , mpl::not_ > > , std::size_t >::type attr_size(Attribute const& attr) { return fusion::size(attr); } template inline typename enable_if< traits::is_container, std::size_t >::type attr_size(Attribute const& attr) { return attr.size(); } inline std::size_t attr_size(unused_type) { return 0; } /////////////////////////////////////////////////////////////////////// // This is a wrapper for any iterator allowing to pass a reference of it // to the components of the sequence template class indirect_iterator : public boost::iterator_facade< indirect_iterator , typename boost::detail::iterator_traits::value_type , boost::forward_traversal_tag , typename boost::detail::iterator_traits::value_type const&> { typedef typename boost::detail::iterator_traits::value_type base_value_type; typedef boost::iterator_facade< indirect_iterator, base_value_type , boost::forward_traversal_tag, base_value_type const& > base_type; public: indirect_iterator(Iterator& iter) : iter_(&iter) {} private: friend class boost::iterator_core_access; void increment() { ++*iter_; } bool equal(indirect_iterator const& other) const { return *iter_ == *other.iter_; } typename base_type::reference dereference() const { return **iter_; } private: Iterator* iter_; }; template struct make_indirect_iterator { typedef indirect_iterator type; }; template <> struct make_indirect_iterator { typedef unused_type const* type; }; } template struct base_sequence : nary_generator { typedef typename traits::sequence_properties::type properties; base_sequence(Elements const& elements) : elements(elements) {} typedef Elements elements_type; struct sequence_base_id; template struct attribute { // Put all the element attributes in a tuple typedef typename traits::build_attribute_sequence< Elements, Context, mpl::identity, Iterator>::type all_attributes; // Now, build a fusion vector over the attributes. Note // that build_fusion_vector 1) removes all unused attributes // and 2) may return unused_type if all elements have // unused_type(s). typedef typename traits::build_fusion_vector::type type_; // Finally, strip single element vectors into its // naked form: vector1 --> T typedef typename traits::strip_single_element_vector::type type; }; // standard case. Attribute is a fusion tuple template < typename OutputIterator, typename Context, typename Delimiter , typename Attribute, typename Pred1, typename Pred2> bool generate_impl(OutputIterator& sink, Context& ctx , Delimiter const& d, Attribute& attr_, Pred1, Pred2) const { typedef detail::fail_function< OutputIterator, Context, Delimiter> fail_function; typedef traits::attribute_not_unused predicate; // wrap the attribute in a tuple if it is not a tuple or if the // attribute of this sequence is a single element tuple typedef typename attribute::type_ attr_type_; typename traits::wrap_if_not_tuple , mpl::not_ > >::type >::type attr(attr_); // return false if *any* of the generators fail bool r = spirit::any_if(elements, attr , fail_function(sink, ctx, d), predicate()); // fail generating if sequences have not the same (logical) length return !r && (!Strict::value || // This ignores container element count (which is not good), // but allows valid attributes to succeed. This will lead to // false positives (failing generators, even if they shouldn't) // if the embedded component is restricting the number of // container elements it consumes (i.e. repeat). This solution // is not optimal but much better than letting _all_ repetitive // components fail. Pred1::value || detail::attribute_size::value == detail::attr_size(attr_)); } // Special case when Attribute is an stl container and the sequence's // attribute is not a one element sequence template < typename OutputIterator, typename Context, typename Delimiter , typename Attribute> bool generate_impl(OutputIterator& sink, Context& ctx , Delimiter const& d, Attribute const& attr_ , mpl::true_, mpl::false_) const { // return false if *any* of the generators fail typedef detail::fail_function< OutputIterator, Context, Delimiter> fail_function; typedef typename traits::container_iterator::type iterator_type; typedef typename detail::make_indirect_iterator::type indirect_iterator_type; typedef detail::pass_container< fail_function, Attribute, indirect_iterator_type, Strict> pass_container; iterator_type begin = traits::begin(attr_); iterator_type end = traits::end(attr_); pass_container pass(fail_function(sink, ctx, d), indirect_iterator_type(begin), indirect_iterator_type(end)); bool r = fusion::any(elements, pass); // fail generating if sequences have not the same (logical) length return !r && (!Strict::value || begin == end); } // main generate function. Dispatches to generate_impl depending // on the Attribute type. template < typename OutputIterator, typename Context, typename Delimiter , typename Attribute> bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d , Attribute const& attr) const { typedef typename traits::is_container::type is_container; typedef typename attribute::type_ attr_type_; typedef typename traits::one_element_sequence::type is_one_element_sequence; return generate_impl(sink, ctx, d, attr, is_container() , is_one_element_sequence()); } template info what(Context& context) const { info result("sequence"); fusion::for_each(elements, spirit::detail::what_function(result, context)); return result; } Elements elements; }; template struct sequence : base_sequence > { typedef base_sequence base_sequence_; sequence(Elements const& subject) : base_sequence_(subject) {} }; template struct strict_sequence : base_sequence > { typedef base_sequence base_sequence_; strict_sequence(Elements const& subject) : base_sequence_(subject) {} }; /////////////////////////////////////////////////////////////////////////// // Generator generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// namespace detail { template struct make_sequence : make_nary_composite {}; template struct make_sequence : make_nary_composite {}; } template struct make_composite : detail::make_sequence::value> {}; }}} namespace boost { namespace spirit { namespace traits { template struct has_semantic_action > : nary_has_semantic_action {}; template struct has_semantic_action > : nary_has_semantic_action {}; }}} #endif