1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2001-2011 Joel de Guzman
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(SPIRIT_KARMA_ALTERNATIVE_MAR_01_2007_1124AM)
8 #define SPIRIT_KARMA_ALTERNATIVE_MAR_01_2007_1124AM
14 #include <boost/spirit/home/karma/domain.hpp>
15 #include <boost/spirit/home/karma/directive/buffer.hpp>
16 #include <boost/spirit/home/support/unused.hpp>
17 #include <boost/spirit/home/support/utree/utree_traits_fwd.hpp>
18 #include <boost/spirit/home/karma/detail/attributes.hpp>
19 #include <boost/spirit/home/support/detail/hold_any.hpp>
20 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
21 #include <boost/spirit/home/support/container.hpp>
22 #include <boost/utility/enable_if.hpp>
23 #include <boost/variant.hpp>
24 #include <boost/detail/workaround.hpp>
26 ///////////////////////////////////////////////////////////////////////////////
27 namespace boost { namespace spirit { namespace karma { namespace detail
29 ///////////////////////////////////////////////////////////////////////////
30 // execute a generator if the given Attribute type is compatible
31 ///////////////////////////////////////////////////////////////////////////
33 // this gets instantiated if the Attribute type is _not_ compatible with
35 template <typename Component, typename Attribute, typename Expected
36 , typename Enable = void>
37 struct alternative_generate
39 template <typename OutputIterator, typename Context, typename Delimiter>
41 call(Component const&, OutputIterator&, Context&, Delimiter const&
42 , Attribute const&, bool& failed)
49 template <typename Component>
50 struct alternative_generate<Component, unused_type, unused_type>
52 template <typename OutputIterator, typename Context, typename Delimiter>
54 call(Component const& component, OutputIterator& sink, Context& ctx
55 , Delimiter const& d, unused_type, bool&)
57 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
58 component; // suppresses warning: C4100: 'component' : unreferenced formal parameter
60 // return true if any of the generators succeed
61 return component.generate(sink, ctx, d, unused);
65 // this gets instantiated if there is no Attribute given for the
66 // alternative generator
67 template <typename Component, typename Expected>
68 struct alternative_generate<Component, unused_type, Expected>
69 : alternative_generate<Component, unused_type, unused_type> {};
71 // this gets instantiated if the generator does not expect to receive an
72 // Attribute (the generator is self contained).
73 template <typename Component, typename Attribute>
74 struct alternative_generate<Component, Attribute, unused_type>
75 : alternative_generate<Component, unused_type, unused_type> {};
77 // this gets instantiated if the Attribute type is compatible to the
79 template <typename Component, typename Attribute, typename Expected>
80 struct alternative_generate<Component, Attribute, Expected
82 traits::compute_compatible_component<Expected, Attribute, karma::domain> >::type>
84 template <typename OutputIterator, typename Context, typename Delimiter>
86 call(Component const& component, OutputIterator& sink
87 , Context& ctx, Delimiter const& d, Attribute const& attr, bool&)
89 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
90 component; // suppresses warning: C4100: 'component' : unreferenced formal parameter
92 return call(component, sink, ctx, d, attr
93 , spirit::traits::not_is_variant<Attribute, karma::domain>());
96 template <typename OutputIterator, typename Context, typename Delimiter>
98 call(Component const& component, OutputIterator& sink
99 , Context& ctx, Delimiter const& d, Attribute const& attr, mpl::true_)
101 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
102 component; // suppresses warning: C4100: 'component' : unreferenced formal parameter
104 return component.generate(sink, ctx, d, attr);
107 template <typename OutputIterator, typename Context, typename Delimiter>
109 call(Component const& component, OutputIterator& sink
110 , Context& ctx, Delimiter const& d, Attribute const& attr, mpl::false_)
112 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
113 component; // suppresses warning: C4100: 'component' : unreferenced formal parameter
116 traits::compute_compatible_component<Expected, Attribute, domain>
119 // if we got passed an empty optional, just fail generation
120 if (!traits::has_optional_value(attr))
123 // make sure, the content of the passed variant matches our
125 typename traits::optional_attribute<Attribute>::type attr_ =
126 traits::optional_value(attr);
127 if (!component_type::is_compatible(spirit::traits::which(attr_)))
130 // returns true if any of the generators succeed
131 typedef typename component_type::compatible_type compatible_type;
132 return component.generate(sink, ctx, d
133 , boost::get<compatible_type>(attr_));
137 ///////////////////////////////////////////////////////////////////////////
138 // alternative_generate_function: a functor supplied to fusion::any which
139 // will be executed for every generator in a given alternative generator
141 ///////////////////////////////////////////////////////////////////////////
142 template <typename OutputIterator, typename Context, typename Delimiter,
143 typename Attribute, typename Strict>
144 struct alternative_generate_function
146 alternative_generate_function(OutputIterator& sink_, Context& ctx_
147 , Delimiter const& d, Attribute const& attr_)
148 : sink(sink_), ctx(ctx_), delim(d), attr(attr_) {}
150 template <typename Component>
151 bool operator()(Component const& component)
154 typename traits::attribute_of<Component, Context>::type
157 alternative_generate<Component, Attribute, expected_type>
160 // wrap the given output iterator avoid output as long as one
162 detail::enable_buffering<OutputIterator> buffering(sink);
164 bool failed = false; // will be ignored
166 detail::disable_counting<OutputIterator> nocounting(sink);
167 r = generate::call(component, sink, ctx, delim, attr, failed);
170 buffering.buffer_copy();
174 // avoid double buffering
175 template <typename Component>
176 bool operator()(buffer_directive<Component> const& component)
179 traits::attribute_of<Component, Context>::type
181 typedef alternative_generate<
182 buffer_directive<Component>, Attribute, expected_type>
185 bool failed = false; // will be ignored
186 return generate::call(component, sink, ctx, delim, attr, failed);
189 OutputIterator& sink;
191 Delimiter const& delim;
192 Attribute const& attr;
195 // silence MSVC warning C4512: assignment operator could not be generated
196 alternative_generate_function& operator= (alternative_generate_function const&);
199 // specialization for strict alternatives
200 template <typename OutputIterator, typename Context, typename Delimiter,
202 struct alternative_generate_function<
203 OutputIterator, Context, Delimiter, Attribute, mpl::true_>
205 alternative_generate_function(OutputIterator& sink_, Context& ctx_
206 , Delimiter const& d, Attribute const& attr_)
207 : sink(sink_), ctx(ctx_), delim(d), attr(attr_), failed(false) {}
209 template <typename Component>
210 bool operator()(Component const& component)
213 typename traits::attribute_of<Component, Context>::type
216 alternative_generate<Component, Attribute, expected_type>
220 return false; // give up when already failed
222 // wrap the given output iterator avoid output as long as one
224 detail::enable_buffering<OutputIterator> buffering(sink);
227 detail::disable_counting<OutputIterator> nocounting(sink);
228 r = generate::call(component, sink, ctx, delim, attr, failed);
232 buffering.buffer_copy();
238 OutputIterator& sink;
240 Delimiter const& delim;
241 Attribute const& attr;
245 // silence MSVC warning C4512: assignment operator could not be generated
246 alternative_generate_function& operator= (alternative_generate_function const&);