Chris@16: // Copyright (c) 2001-2011 Hartmut Kaiser Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(SPIRIT_KARMA_BUFFER_AUG_03_2009_0949AM) Chris@16: #define SPIRIT_KARMA_BUFFER_AUG_03_2009_0949AM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace spirit Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Enablers Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template <> Chris@16: struct use_directive // enables buffer Chris@16: : mpl::true_ {}; Chris@16: Chris@16: }} Chris@16: Chris@16: namespace boost { namespace spirit { namespace karma Chris@16: { Chris@16: #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS Chris@16: using spirit::buffer; Chris@16: #endif Chris@16: using spirit::buffer_type; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // buffer_directive buffers all generated output of the embedded generator Chris@16: // and flushes it only if the whole embedded generator succeeds Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct buffer_directive : unary_generator > Chris@16: { Chris@16: typedef Subject subject_type; Chris@16: typedef mpl::int_< Chris@16: subject_type::properties::value | Chris@16: generator_properties::countingbuffer Chris@16: > properties; Chris@16: Chris@16: buffer_directive(Subject const& subject) Chris@16: : subject(subject) {} Chris@16: Chris@16: template Chris@16: struct attribute Chris@16: : traits::attribute_of Chris@16: {}; Chris@16: Chris@16: template Chris@16: bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d Chris@16: , Attribute const& attr) const Chris@16: { Chris@16: // wrap the given output iterator to avoid output as long as the Chris@16: // embedded generator (subject) fails Chris@16: detail::enable_buffering buffering(sink); Chris@16: bool r = false; Chris@16: { Chris@16: detail::disable_counting nocounting(sink); Chris@16: r = subject.generate(sink, ctx, d, attr); Chris@16: } Chris@16: if (r) Chris@16: buffering.buffer_copy(); Chris@16: return r; Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& context) const Chris@16: { Chris@16: return info("buffer", subject.what(context)); Chris@16: } Chris@16: Chris@16: Subject subject; Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Generator generators: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_directive Chris@16: { Chris@16: typedef buffer_directive result_type; Chris@16: result_type operator()(unused_type, Subject const& subject Chris@16: , unused_type) const Chris@16: { Chris@16: return result_type(subject); Chris@16: } Chris@16: }; Chris@16: Chris@16: // make sure buffer[buffer[...]] does not result in double buffering Chris@16: template Chris@16: struct make_directive, Modifiers> Chris@16: { Chris@16: typedef buffer_directive result_type; Chris@16: result_type operator()(unused_type Chris@16: , buffer_directive const& subject, unused_type) const Chris@16: { Chris@16: return subject; Chris@16: } Chris@16: }; Chris@16: }}} Chris@16: Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct has_semantic_action > Chris@16: : unary_has_semantic_action {}; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct handles_container, Attribute Chris@16: , Context, Iterator> Chris@16: : unary_handles_container {}; Chris@16: }}} Chris@16: Chris@16: #endif