Chris@16
|
1 // Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
2 //
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #if !defined(SPIRIT_KARMA_BUFFER_AUG_03_2009_0949AM)
|
Chris@16
|
7 #define SPIRIT_KARMA_BUFFER_AUG_03_2009_0949AM
|
Chris@16
|
8
|
Chris@16
|
9 #if defined(_MSC_VER)
|
Chris@16
|
10 #pragma once
|
Chris@16
|
11 #endif
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/spirit/home/karma/meta_compiler.hpp>
|
Chris@16
|
14 #include <boost/spirit/home/karma/generator.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/karma/domain.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/support/common_terminals.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/has_semantic_action.hpp>
|
Chris@16
|
21 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
22 #include <boost/spirit/home/karma/detail/attributes.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost { namespace spirit
|
Chris@16
|
25 {
|
Chris@16
|
26 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
27 // Enablers
|
Chris@16
|
28 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
29 template <>
|
Chris@16
|
30 struct use_directive<karma::domain, tag::buffer> // enables buffer
|
Chris@16
|
31 : mpl::true_ {};
|
Chris@16
|
32
|
Chris@16
|
33 }}
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost { namespace spirit { namespace karma
|
Chris@16
|
36 {
|
Chris@16
|
37 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
38 using spirit::buffer;
|
Chris@16
|
39 #endif
|
Chris@16
|
40 using spirit::buffer_type;
|
Chris@16
|
41
|
Chris@16
|
42 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
43 // buffer_directive buffers all generated output of the embedded generator
|
Chris@16
|
44 // and flushes it only if the whole embedded generator succeeds
|
Chris@16
|
45 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
46 template <typename Subject>
|
Chris@16
|
47 struct buffer_directive : unary_generator<buffer_directive<Subject> >
|
Chris@16
|
48 {
|
Chris@16
|
49 typedef Subject subject_type;
|
Chris@16
|
50 typedef mpl::int_<
|
Chris@16
|
51 subject_type::properties::value |
|
Chris@16
|
52 generator_properties::countingbuffer
|
Chris@16
|
53 > properties;
|
Chris@16
|
54
|
Chris@16
|
55 buffer_directive(Subject const& subject)
|
Chris@16
|
56 : subject(subject) {}
|
Chris@16
|
57
|
Chris@16
|
58 template <typename Context, typename Iterator>
|
Chris@16
|
59 struct attribute
|
Chris@16
|
60 : traits::attribute_of<subject_type, Context, Iterator>
|
Chris@16
|
61 {};
|
Chris@16
|
62
|
Chris@16
|
63 template <typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
64 , typename Attribute>
|
Chris@16
|
65 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
|
Chris@16
|
66 , Attribute const& attr) const
|
Chris@16
|
67 {
|
Chris@16
|
68 // wrap the given output iterator to avoid output as long as the
|
Chris@16
|
69 // embedded generator (subject) fails
|
Chris@16
|
70 detail::enable_buffering<OutputIterator> buffering(sink);
|
Chris@16
|
71 bool r = false;
|
Chris@16
|
72 {
|
Chris@16
|
73 detail::disable_counting<OutputIterator> nocounting(sink);
|
Chris@16
|
74 r = subject.generate(sink, ctx, d, attr);
|
Chris@16
|
75 }
|
Chris@16
|
76 if (r)
|
Chris@16
|
77 buffering.buffer_copy();
|
Chris@16
|
78 return r;
|
Chris@16
|
79 }
|
Chris@16
|
80
|
Chris@16
|
81 template <typename Context>
|
Chris@16
|
82 info what(Context& context) const
|
Chris@16
|
83 {
|
Chris@16
|
84 return info("buffer", subject.what(context));
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 Subject subject;
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
91 // Generator generators: make_xxx function (objects)
|
Chris@16
|
92 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
93 template <typename Subject, typename Modifiers>
|
Chris@16
|
94 struct make_directive<tag::buffer, Subject, Modifiers>
|
Chris@16
|
95 {
|
Chris@16
|
96 typedef buffer_directive<Subject> result_type;
|
Chris@16
|
97 result_type operator()(unused_type, Subject const& subject
|
Chris@16
|
98 , unused_type) const
|
Chris@16
|
99 {
|
Chris@16
|
100 return result_type(subject);
|
Chris@16
|
101 }
|
Chris@16
|
102 };
|
Chris@16
|
103
|
Chris@16
|
104 // make sure buffer[buffer[...]] does not result in double buffering
|
Chris@16
|
105 template <typename Subject, typename Modifiers>
|
Chris@16
|
106 struct make_directive<tag::buffer, buffer_directive<Subject>, Modifiers>
|
Chris@16
|
107 {
|
Chris@16
|
108 typedef buffer_directive<Subject> result_type;
|
Chris@16
|
109 result_type operator()(unused_type
|
Chris@16
|
110 , buffer_directive<Subject> const& subject, unused_type) const
|
Chris@16
|
111 {
|
Chris@16
|
112 return subject;
|
Chris@16
|
113 }
|
Chris@16
|
114 };
|
Chris@16
|
115 }}}
|
Chris@16
|
116
|
Chris@16
|
117 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
118 {
|
Chris@16
|
119 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
120 template <typename Subject>
|
Chris@16
|
121 struct has_semantic_action<karma::buffer_directive<Subject> >
|
Chris@16
|
122 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
123
|
Chris@16
|
124 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
125 template <typename Subject, typename Attribute, typename Context
|
Chris@16
|
126 , typename Iterator>
|
Chris@16
|
127 struct handles_container<karma::buffer_directive<Subject>, Attribute
|
Chris@16
|
128 , Context, Iterator>
|
Chris@16
|
129 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
|
Chris@16
|
130 }}}
|
Chris@16
|
131
|
Chris@16
|
132 #endif
|