1 // Copyright (c) 2001-2011 Joel de Guzman
2 // Copyright (c) 2001-2011 Hartmut Kaiser
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #if !defined(BOOST_SPIRIT_KARMA_POSITIVE_MAR_03_2007_0945PM)
8 #define BOOST_SPIRIT_KARMA_POSITIVE_MAR_03_2007_0945PM
14 #include <boost/spirit/home/karma/domain.hpp>
15 #include <boost/spirit/home/karma/generator.hpp>
16 #include <boost/spirit/home/karma/meta_compiler.hpp>
17 #include <boost/spirit/home/karma/detail/indirect_iterator.hpp>
18 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
19 #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
20 #include <boost/spirit/home/karma/detail/pass_container.hpp>
21 #include <boost/spirit/home/karma/detail/fail_function.hpp>
22 #include <boost/spirit/home/support/info.hpp>
23 #include <boost/spirit/home/support/unused.hpp>
24 #include <boost/spirit/home/support/container.hpp>
25 #include <boost/spirit/home/support/has_semantic_action.hpp>
26 #include <boost/spirit/home/support/handles_container.hpp>
27 #include <boost/spirit/home/karma/detail/attributes.hpp>
29 #include <boost/type_traits/add_const.hpp>
31 namespace boost { namespace spirit
33 ///////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////
37 struct use_operator<karma::domain, proto::tag::unary_plus> // enables +g
41 ///////////////////////////////////////////////////////////////////////////////
42 namespace boost { namespace spirit { namespace karma
44 template <typename Subject, typename Strict, typename Derived>
45 struct base_plus : unary_generator<Derived>
48 // Ignore return value in relaxed mode (failing subject generators
49 // are just skipped). This allows to selectively generate items in
50 // the provided attribute.
51 template <typename F, typename Attribute>
52 bool generate_subject(F f, Attribute const&, bool& result, mpl::false_) const
57 else if (!f.is_at_end())
62 template <typename F, typename Attribute>
63 bool generate_subject(F f, Attribute const&, bool& result, mpl::true_) const
71 // There is no way to distinguish a failed generator from a
72 // generator to be skipped. We assume the user takes responsibility
73 // for ending the loop if no attribute is specified.
75 bool generate_subject(F f, unused_type, bool& result, mpl::false_) const
83 // template <typename F>
84 // bool generate_subject(F f, unused_type, bool& result, mpl::true_) const
86 // bool r = f(subject);
93 typedef Subject subject_type;
94 typedef typename subject_type::properties properties;
96 // Build a std::vector from the subjects attribute. Note
97 // that build_std_vector may return unused_type if the
98 // subject's attribute is an unused_type.
99 template <typename Context, typename Iterator>
101 : traits::build_std_vector<
102 typename traits::attribute_of<subject_type, Context, Iterator>::type
106 base_plus(Subject const& subject)
107 : subject(subject) {}
110 typename OutputIterator, typename Context, typename Delimiter
111 , typename Attribute>
112 bool generate(OutputIterator& sink, Context& ctx
113 , Delimiter const& d, Attribute const& attr) const
115 typedef detail::fail_function<
116 OutputIterator, Context, Delimiter> fail_function;
118 typedef typename traits::container_iterator<
119 typename add_const<Attribute>::type
120 >::type iterator_type;
123 typename traits::make_indirect_iterator<iterator_type>::type
124 indirect_iterator_type;
125 typedef detail::pass_container<
126 fail_function, Attribute, indirect_iterator_type, mpl::false_>
129 iterator_type it = traits::begin(attr);
130 iterator_type end = traits::end(attr);
132 // plus fails if the parameter is empty
133 if (traits::compare(it, end))
136 pass_container pass(fail_function(sink, ctx, d),
137 indirect_iterator_type(it), indirect_iterator_type(end));
139 // from now on plus fails if the underlying output fails or overall
140 // no subject generators succeeded
142 while (!pass.is_at_end())
144 if (!generate_subject(pass, attr, result, Strict()))
147 return result && detail::sink_is_good(sink);
150 template <typename Context>
151 info what(Context& context) const
153 return info("plus", subject.what(context));
159 template <typename Subject>
161 : base_plus<Subject, mpl::false_, plus<Subject> >
163 typedef base_plus<Subject, mpl::false_, plus> base_plus_;
165 plus(Subject const& subject)
166 : base_plus_(subject) {}
169 template <typename Subject>
171 : base_plus<Subject, mpl::true_, strict_plus<Subject> >
173 typedef base_plus<Subject, mpl::true_, strict_plus> base_plus_;
175 strict_plus(Subject const& subject)
176 : base_plus_(subject) {}
179 ///////////////////////////////////////////////////////////////////////////
180 // Generator generators: make_xxx function (objects)
181 ///////////////////////////////////////////////////////////////////////////
184 template <typename Elements, bool strict_mode = false>
186 : make_unary_composite<Elements, plus>
189 template <typename Elements>
190 struct make_plus<Elements, true>
191 : make_unary_composite<Elements, strict_plus>
195 template <typename Elements, typename Modifiers>
196 struct make_composite<proto::tag::unary_plus, Elements, Modifiers>
197 : detail::make_plus<Elements, detail::get_stricttag<Modifiers>::value>
201 namespace boost { namespace spirit { namespace traits
203 ///////////////////////////////////////////////////////////////////////////
204 template <typename Subject>
205 struct has_semantic_action<karma::plus<Subject> >
206 : unary_has_semantic_action<Subject> {};
208 template <typename Subject>
209 struct has_semantic_action<karma::strict_plus<Subject> >
210 : unary_has_semantic_action<Subject> {};
212 ///////////////////////////////////////////////////////////////////////////
213 template <typename Subject, typename Attribute, typename Context
215 struct handles_container<karma::plus<Subject>, Attribute
219 template <typename Subject, typename Attribute, typename Context
221 struct handles_container<karma::strict_plus<Subject>, Attribute