| 1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
| 2 | // Copyright (c) 2001-2011 Joel de Guzman |
| 3 | // |
| 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) |
| 6 | |
| 7 | #ifndef BOOST_SPIRIT_KARMA_DETAIL_ALTERNATIVE_FUNCTION_HPP |
| 8 | #define BOOST_SPIRIT_KARMA_DETAIL_ALTERNATIVE_FUNCTION_HPP |
| 9 | |
| 10 | #if defined(_MSC_VER) |
| 11 | #pragma once |
| 12 | #endif |
| 13 | |
| 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> |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | namespace boost { namespace spirit { namespace karma { namespace detail |
| 28 | { |
| 29 | /////////////////////////////////////////////////////////////////////////// |
| 30 | // execute a generator if the given Attribute type is compatible |
| 31 | /////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | // this gets instantiated if the Attribute type is _not_ compatible with |
| 34 | // the generator |
| 35 | template <typename Component, typename Attribute, typename Expected |
| 36 | , typename Enable = void> |
| 37 | struct alternative_generate |
| 38 | { |
| 39 | template <typename OutputIterator, typename Context, typename Delimiter> |
| 40 | static bool |
| 41 | call(Component const&, OutputIterator&, Context&, Delimiter const& |
| 42 | , Attribute const&, bool& failed) |
| 43 | { |
| 44 | failed = true; |
| 45 | return false; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | template <typename Component> |
| 50 | struct alternative_generate<Component, unused_type, unused_type> |
| 51 | { |
| 52 | template <typename OutputIterator, typename Context, typename Delimiter> |
| 53 | static bool |
| 54 | call(Component const& component, OutputIterator& sink, Context& ctx |
| 55 | , Delimiter const& d, unused_type, bool&) |
| 56 | { |
| 57 | #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) |
| 58 | (void)component; // suppresses warning: C4100: 'component' : unreferenced formal parameter |
| 59 | #endif |
| 60 | // return true if any of the generators succeed |
| 61 | return component.generate(sink, ctx, d, unused); |
| 62 | } |
| 63 | }; |
| 64 | |
| 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> {}; |
| 70 | |
| 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> {}; |
| 76 | |
| 77 | // this gets instantiated if the Attribute type is compatible to the |
| 78 | // generator |
| 79 | template <typename Component, typename Attribute, typename Expected> |
| 80 | struct alternative_generate<Component, Attribute, Expected |
| 81 | , typename enable_if< |
| 82 | traits::compute_compatible_component<Expected, Attribute, karma::domain> >::type> |
| 83 | { |
| 84 | template <typename OutputIterator, typename Context, typename Delimiter> |
| 85 | static bool |
| 86 | call(Component const& component, OutputIterator& sink |
| 87 | , Context& ctx, Delimiter const& d, Attribute const& attr, bool&) |
| 88 | { |
| 89 | #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) |
| 90 | (void)component; // suppresses warning: C4100: 'component' : unreferenced formal parameter |
| 91 | #endif |
| 92 | return call(component, sink, ctx, d, attr |
| 93 | , spirit::traits::not_is_variant_or_variant_in_optional<Attribute, karma::domain>()); |
| 94 | } |
| 95 | |
| 96 | template <typename OutputIterator, typename Context, typename Delimiter> |
| 97 | static bool |
| 98 | call(Component const& component, OutputIterator& sink |
| 99 | , Context& ctx, Delimiter const& d, Attribute const& attr, mpl::true_) |
| 100 | { |
| 101 | #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) |
| 102 | (void)component; // suppresses warning: C4100: 'component' : unreferenced formal parameter |
| 103 | #endif |
| 104 | return component.generate(sink, ctx, d, attr); |
| 105 | } |
| 106 | |
| 107 | template <typename OutputIterator, typename Context, typename Delimiter> |
| 108 | static bool |
| 109 | call(Component const& component, OutputIterator& sink |
| 110 | , Context& ctx, Delimiter const& d, Attribute const& attr, mpl::false_) |
| 111 | { |
| 112 | #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) |
| 113 | (void)component; // suppresses warning: C4100: 'component' : unreferenced formal parameter |
| 114 | #endif |
| 115 | typedef |
| 116 | traits::compute_compatible_component<Expected, Attribute, domain> |
| 117 | component_type; |
| 118 | |
| 119 | // if we got passed an empty optional, just fail generation |
| 120 | if (!traits::has_optional_value(attr)) |
| 121 | return false; |
| 122 | |
| 123 | // make sure, the content of the passed variant matches our |
| 124 | // expectations |
| 125 | typename traits::optional_attribute<Attribute>::type attr_ = |
| 126 | traits::optional_value(attr); |
| 127 | if (!component_type::is_compatible(spirit::traits::which(attr_))) |
| 128 | return false; |
| 129 | |
| 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_)); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | /////////////////////////////////////////////////////////////////////////// |
| 138 | // alternative_generate_function: a functor supplied to fusion::any which |
| 139 | // will be executed for every generator in a given alternative generator |
| 140 | // expression |
| 141 | /////////////////////////////////////////////////////////////////////////// |
| 142 | #ifdef _MSC_VER |
| 143 | # pragma warning(push) |
| 144 | # pragma warning(disable: 4512) // assignment operator could not be generated. |
| 145 | #endif |
| 146 | template <typename OutputIterator, typename Context, typename Delimiter, |
| 147 | typename Attribute, typename Strict> |
| 148 | struct alternative_generate_function |
| 149 | { |
| 150 | alternative_generate_function(OutputIterator& sink_, Context& ctx_ |
| 151 | , Delimiter const& d, Attribute const& attr_) |
| 152 | : sink(sink_), ctx(ctx_), delim(d), attr(attr_) {} |
| 153 | |
| 154 | template <typename Component> |
| 155 | bool operator()(Component const& component) |
| 156 | { |
| 157 | typedef |
| 158 | typename traits::attribute_of<Component, Context>::type |
| 159 | expected_type; |
| 160 | typedef |
| 161 | alternative_generate<Component, Attribute, expected_type> |
| 162 | generate; |
| 163 | |
| 164 | // wrap the given output iterator avoid output as long as one |
| 165 | // component fails |
| 166 | detail::enable_buffering<OutputIterator> buffering(sink); |
| 167 | bool r = false; |
| 168 | bool failed = false; // will be ignored |
| 169 | { |
| 170 | detail::disable_counting<OutputIterator> nocounting(sink); |
| 171 | r = generate::call(component, sink, ctx, delim, attr, failed); |
| 172 | } |
| 173 | if (r) |
| 174 | buffering.buffer_copy(); |
| 175 | return r; |
| 176 | } |
| 177 | |
| 178 | // avoid double buffering |
| 179 | template <typename Component> |
| 180 | bool operator()(buffer_directive<Component> const& component) |
| 181 | { |
| 182 | typedef typename |
| 183 | traits::attribute_of<Component, Context>::type |
| 184 | expected_type; |
| 185 | typedef alternative_generate< |
| 186 | buffer_directive<Component>, Attribute, expected_type> |
| 187 | generate; |
| 188 | |
| 189 | bool failed = false; // will be ignored |
| 190 | return generate::call(component, sink, ctx, delim, attr, failed); |
| 191 | } |
| 192 | |
| 193 | OutputIterator& sink; |
| 194 | Context& ctx; |
| 195 | Delimiter const& delim; |
| 196 | Attribute const& attr; |
| 197 | }; |
| 198 | |
| 199 | // specialization for strict alternatives |
| 200 | template <typename OutputIterator, typename Context, typename Delimiter, |
| 201 | typename Attribute> |
| 202 | struct alternative_generate_function< |
| 203 | OutputIterator, Context, Delimiter, Attribute, mpl::true_> |
| 204 | { |
| 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) {} |
| 208 | |
| 209 | template <typename Component> |
| 210 | bool operator()(Component const& component) |
| 211 | { |
| 212 | typedef |
| 213 | typename traits::attribute_of<Component, Context>::type |
| 214 | expected_type; |
| 215 | typedef |
| 216 | alternative_generate<Component, Attribute, expected_type> |
| 217 | generate; |
| 218 | |
| 219 | if (failed) |
| 220 | return false; // give up when already failed |
| 221 | |
| 222 | // wrap the given output iterator avoid output as long as one |
| 223 | // component fails |
| 224 | detail::enable_buffering<OutputIterator> buffering(sink); |
| 225 | bool r = false; |
| 226 | { |
| 227 | detail::disable_counting<OutputIterator> nocounting(sink); |
| 228 | r = generate::call(component, sink, ctx, delim, attr, failed); |
| 229 | } |
| 230 | if (r && !failed) |
| 231 | { |
| 232 | buffering.buffer_copy(); |
| 233 | return true; |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | OutputIterator& sink; |
| 239 | Context& ctx; |
| 240 | Delimiter const& delim; |
| 241 | Attribute const& attr; |
| 242 | bool failed; |
| 243 | }; |
| 244 | #ifdef _MSC_VER |
| 245 | # pragma warning(pop) |
| 246 | #endif |
| 247 | }}}} |
| 248 | |
| 249 | #endif |
| 250 | |