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(BOOST_SPIRIT_KARMA_STREAM_MAY_01_2007_0310PM)
|
Chris@16
|
7 #define BOOST_SPIRIT_KARMA_STREAM_MAY_01_2007_0310PM
|
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/support/common_terminals.hpp>
|
Chris@16
|
14 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/support/container.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/support/detail/hold_any.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/support/detail/get_encoding.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/support/detail/is_spirit_tag.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/karma/domain.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/karma/meta_compiler.hpp>
|
Chris@16
|
21 #include <boost/spirit/home/karma/delimit_out.hpp>
|
Chris@16
|
22 #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
|
Chris@16
|
23 #include <boost/spirit/home/karma/stream/detail/format_manip.hpp>
|
Chris@16
|
24 #include <boost/spirit/home/karma/stream/detail/iterator_sink.hpp>
|
Chris@16
|
25 #include <boost/spirit/home/karma/detail/get_casetag.hpp>
|
Chris@16
|
26 #include <boost/spirit/home/karma/detail/extract_from.hpp>
|
Chris@16
|
27 #include <boost/fusion/include/at.hpp>
|
Chris@16
|
28 #include <boost/fusion/include/vector.hpp>
|
Chris@16
|
29 #include <boost/fusion/include/cons.hpp>
|
Chris@16
|
30 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
31 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #include <iosfwd>
|
Chris@16
|
34
|
Chris@16
|
35 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
36 namespace boost { namespace spirit
|
Chris@16
|
37 {
|
Chris@16
|
38 namespace tag
|
Chris@16
|
39 {
|
Chris@16
|
40 template <typename Char = char>
|
Chris@16
|
41 struct stream_tag
|
Chris@16
|
42 {
|
Chris@16
|
43 BOOST_SPIRIT_IS_TAG()
|
Chris@16
|
44 };
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 namespace karma
|
Chris@16
|
48 {
|
Chris@16
|
49 ///////////////////////////////////////////////////////////////////////
|
Chris@16
|
50 // This one is the class that the user can instantiate directly in
|
Chris@16
|
51 // order to create a customized int generator
|
Chris@16
|
52 template <typename Char = char>
|
Chris@16
|
53 struct stream_generator
|
Chris@16
|
54 : spirit::terminal<tag::stream_tag<Char> >
|
Chris@16
|
55 {};
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
59 // Enablers
|
Chris@16
|
60 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
61 template <>
|
Chris@16
|
62 struct use_terminal<karma::domain, tag::stream> // enables stream
|
Chris@16
|
63 : mpl::true_ {};
|
Chris@16
|
64
|
Chris@16
|
65 template <>
|
Chris@16
|
66 struct use_terminal<karma::domain, tag::wstream> // enables wstream
|
Chris@16
|
67 : mpl::true_ {};
|
Chris@16
|
68
|
Chris@16
|
69 template <typename A0>
|
Chris@16
|
70 struct use_terminal<karma::domain // enables stream(...)
|
Chris@16
|
71 , terminal_ex<tag::stream, fusion::vector1<A0> >
|
Chris@16
|
72 > : mpl::true_ {};
|
Chris@16
|
73
|
Chris@16
|
74 template <typename A0>
|
Chris@16
|
75 struct use_terminal<karma::domain // enables wstream(...)
|
Chris@16
|
76 , terminal_ex<tag::wstream, fusion::vector1<A0> >
|
Chris@16
|
77 > : mpl::true_ {};
|
Chris@16
|
78
|
Chris@16
|
79 template <> // enables stream(f)
|
Chris@16
|
80 struct use_lazy_terminal<
|
Chris@16
|
81 karma::domain, tag::stream, 1 /*arity*/
|
Chris@16
|
82 > : mpl::true_ {};
|
Chris@16
|
83
|
Chris@16
|
84 template <> // enables wstream(f)
|
Chris@16
|
85 struct use_lazy_terminal<
|
Chris@16
|
86 karma::domain, tag::wstream, 1 /*arity*/
|
Chris@16
|
87 > : mpl::true_ {};
|
Chris@16
|
88
|
Chris@16
|
89 // enables stream_generator<char_type>
|
Chris@16
|
90 template <typename Char>
|
Chris@16
|
91 struct use_terminal<karma::domain, tag::stream_tag<Char> >
|
Chris@16
|
92 : mpl::true_ {};
|
Chris@16
|
93
|
Chris@16
|
94 template <typename Char, typename A0>
|
Chris@16
|
95 struct use_terminal<karma::domain
|
Chris@16
|
96 , terminal_ex<tag::stream_tag<Char>, fusion::vector1<A0> >
|
Chris@16
|
97 > : mpl::true_ {};
|
Chris@16
|
98
|
Chris@16
|
99 template <typename Char>
|
Chris@16
|
100 struct use_lazy_terminal<
|
Chris@16
|
101 karma::domain, tag::stream_tag<Char>, 1 /*arity*/
|
Chris@16
|
102 > : mpl::true_ {};
|
Chris@16
|
103
|
Chris@16
|
104 }}
|
Chris@16
|
105
|
Chris@16
|
106 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
107 namespace boost { namespace spirit { namespace karma
|
Chris@16
|
108 {
|
Chris@16
|
109 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
110 using spirit::stream;
|
Chris@16
|
111 using spirit::wstream;
|
Chris@16
|
112 #endif
|
Chris@16
|
113 using spirit::stream_type;
|
Chris@16
|
114 using spirit::wstream_type;
|
Chris@16
|
115
|
Chris@16
|
116 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
117 template <typename Char, typename CharEncoding, typename Tag>
|
Chris@16
|
118 struct any_stream_generator
|
Chris@16
|
119 : primitive_generator<any_stream_generator<Char, CharEncoding, Tag> >
|
Chris@16
|
120 {
|
Chris@16
|
121 template <typename Context, typename Unused = unused_type>
|
Chris@16
|
122 struct attribute
|
Chris@16
|
123 {
|
Chris@16
|
124 typedef spirit::basic_hold_any<Char> type;
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 // any_stream_generator has an attached attribute
|
Chris@16
|
128 template <
|
Chris@16
|
129 typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
130 , typename Attribute
|
Chris@16
|
131 >
|
Chris@16
|
132 static bool generate(OutputIterator& sink, Context& context
|
Chris@16
|
133 , Delimiter const& d, Attribute const& attr)
|
Chris@16
|
134 {
|
Chris@16
|
135 typedef karma::detail::iterator_sink<
|
Chris@16
|
136 OutputIterator, Char, CharEncoding, Tag
|
Chris@16
|
137 > sink_device;
|
Chris@16
|
138
|
Chris@16
|
139 if (!traits::has_optional_value(attr))
|
Chris@16
|
140 return false;
|
Chris@16
|
141
|
Chris@16
|
142 // use existing operator<<()
|
Chris@16
|
143 typedef typename attribute<Context>::type attribute_type;
|
Chris@16
|
144
|
Chris@16
|
145 {
|
Chris@16
|
146 boost::iostreams::stream<sink_device> ostr(sink);
|
Chris@16
|
147 ostr << traits::extract_from<attribute_type>(attr, context) << std::flush;
|
Chris@16
|
148
|
Chris@16
|
149 if (!ostr.good())
|
Chris@16
|
150 return false;
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 return karma::delimit_out(sink, d); // always do post-delimiting
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 // this is a special overload to detect if the output iterator has been
|
Chris@16
|
157 // generated by a format_manip object.
|
Chris@16
|
158 template <
|
Chris@16
|
159 typename T, typename Traits, typename Properties, typename Context
|
Chris@16
|
160 , typename Delimiter, typename Attribute
|
Chris@16
|
161 >
|
Chris@16
|
162 static bool generate(
|
Chris@16
|
163 karma::detail::output_iterator<
|
Chris@16
|
164 karma::ostream_iterator<T, Char, Traits>, Properties
|
Chris@16
|
165 >& sink, Context& context, Delimiter const& d
|
Chris@16
|
166 , Attribute const& attr)
|
Chris@16
|
167 {
|
Chris@16
|
168 typedef karma::detail::output_iterator<
|
Chris@16
|
169 karma::ostream_iterator<T, Char, Traits>, Properties
|
Chris@16
|
170 > output_iterator;
|
Chris@16
|
171 typedef karma::detail::iterator_sink<
|
Chris@16
|
172 output_iterator, Char, CharEncoding, Tag
|
Chris@16
|
173 > sink_device;
|
Chris@16
|
174
|
Chris@16
|
175 if (!traits::has_optional_value(attr))
|
Chris@16
|
176 return false;
|
Chris@16
|
177
|
Chris@16
|
178 // use existing operator<<()
|
Chris@16
|
179 typedef typename attribute<Context>::type attribute_type;
|
Chris@16
|
180
|
Chris@16
|
181 {
|
Chris@16
|
182 boost::iostreams::stream<sink_device> ostr(sink);
|
Chris@16
|
183 ostr.imbue(sink.get_ostream().getloc());
|
Chris@16
|
184 ostr << traits::extract_from<attribute_type>(attr, context)
|
Chris@16
|
185 << std::flush;
|
Chris@16
|
186 if (!ostr.good())
|
Chris@16
|
187 return false;
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 return karma::delimit_out(sink, d); // always do post-delimiting
|
Chris@16
|
191 }
|
Chris@16
|
192
|
Chris@16
|
193 // this any_stream has no parameter attached, it needs to have been
|
Chris@16
|
194 // initialized from a value/variable
|
Chris@16
|
195 template <typename OutputIterator, typename Context
|
Chris@16
|
196 , typename Delimiter>
|
Chris@16
|
197 static bool
|
Chris@16
|
198 generate(OutputIterator&, Context&, Delimiter const&, unused_type)
|
Chris@16
|
199 {
|
Chris@16
|
200 // It is not possible (doesn't make sense) to use stream generators
|
Chris@16
|
201 // without providing any attribute, as the generator doesn't 'know'
|
Chris@16
|
202 // what to output. The following assertion fires if this situation
|
Chris@16
|
203 // is detected in your code.
|
Chris@16
|
204 BOOST_SPIRIT_ASSERT_FAIL(OutputIterator, stream_not_usable_without_attribute, ());
|
Chris@16
|
205 return false;
|
Chris@16
|
206 }
|
Chris@16
|
207
|
Chris@16
|
208 template <typename Context>
|
Chris@16
|
209 info what(Context& /*context*/) const
|
Chris@16
|
210 {
|
Chris@16
|
211 return info("stream");
|
Chris@16
|
212 }
|
Chris@16
|
213 };
|
Chris@16
|
214
|
Chris@16
|
215 template <typename T, typename Char, typename CharEncoding, typename Tag>
|
Chris@16
|
216 struct lit_stream_generator
|
Chris@16
|
217 : primitive_generator<lit_stream_generator<T, Char, CharEncoding, Tag> >
|
Chris@16
|
218 {
|
Chris@16
|
219 template <typename Context, typename Unused>
|
Chris@16
|
220 struct attribute
|
Chris@16
|
221 {
|
Chris@16
|
222 typedef unused_type type;
|
Chris@16
|
223 };
|
Chris@16
|
224
|
Chris@16
|
225 lit_stream_generator(typename add_reference<T>::type t)
|
Chris@16
|
226 : t_(t)
|
Chris@16
|
227 {}
|
Chris@16
|
228
|
Chris@16
|
229 // lit_stream_generator has an attached parameter
|
Chris@16
|
230
|
Chris@16
|
231 // this overload will be used in the normal case (not called from
|
Chris@16
|
232 // format_manip).
|
Chris@16
|
233 template <
|
Chris@16
|
234 typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
235 , typename Attribute>
|
Chris@16
|
236 bool generate(OutputIterator& sink, Context&, Delimiter const& d
|
Chris@16
|
237 , Attribute const&) const
|
Chris@16
|
238 {
|
Chris@16
|
239 typedef karma::detail::iterator_sink<
|
Chris@16
|
240 OutputIterator, Char, CharEncoding, Tag
|
Chris@16
|
241 > sink_device;
|
Chris@16
|
242
|
Chris@16
|
243 boost::iostreams::stream<sink_device> ostr(sink);
|
Chris@16
|
244 ostr << t_ << std::flush; // use existing operator<<()
|
Chris@16
|
245
|
Chris@16
|
246 if (ostr.good())
|
Chris@16
|
247 return karma::delimit_out(sink, d); // always do post-delimiting
|
Chris@16
|
248 return false;
|
Chris@16
|
249 }
|
Chris@16
|
250
|
Chris@16
|
251 // this is a special overload to detect if the output iterator has been
|
Chris@16
|
252 // generated by a format_manip object.
|
Chris@16
|
253 template <
|
Chris@16
|
254 typename T1, typename Traits, typename Properties
|
Chris@16
|
255 , typename Context, typename Delimiter, typename Attribute>
|
Chris@16
|
256 bool generate(
|
Chris@16
|
257 karma::detail::output_iterator<
|
Chris@16
|
258 karma::ostream_iterator<T1, Char, Traits>, Properties
|
Chris@16
|
259 >& sink, Context&, Delimiter const& d, Attribute const&) const
|
Chris@16
|
260 {
|
Chris@16
|
261 typedef karma::detail::output_iterator<
|
Chris@16
|
262 karma::ostream_iterator<T1, Char, Traits>, Properties
|
Chris@16
|
263 > output_iterator;
|
Chris@16
|
264 typedef karma::detail::iterator_sink<
|
Chris@16
|
265 output_iterator, Char, CharEncoding, Tag
|
Chris@16
|
266 > sink_device;
|
Chris@16
|
267
|
Chris@16
|
268 {
|
Chris@16
|
269 boost::iostreams::stream<sink_device> ostr(sink);
|
Chris@16
|
270 ostr.imbue(sink.get_ostream().getloc());
|
Chris@16
|
271 ostr << t_ << std::flush; // use existing operator<<()
|
Chris@16
|
272
|
Chris@16
|
273 if (!ostr.good())
|
Chris@16
|
274 return false;
|
Chris@16
|
275 }
|
Chris@16
|
276
|
Chris@16
|
277 return karma::delimit_out(sink, d); // always do post-delimiting
|
Chris@16
|
278 }
|
Chris@16
|
279
|
Chris@16
|
280 template <typename Context>
|
Chris@16
|
281 info what(Context& /*context*/) const
|
Chris@16
|
282 {
|
Chris@16
|
283 return info("any-stream");
|
Chris@16
|
284 }
|
Chris@16
|
285
|
Chris@16
|
286 T t_;
|
Chris@16
|
287
|
Chris@16
|
288 private:
|
Chris@16
|
289 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
290 lit_stream_generator& operator= (lit_stream_generator const&);
|
Chris@16
|
291 };
|
Chris@16
|
292
|
Chris@16
|
293 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
294 // Generator generators: make_xxx function (objects)
|
Chris@16
|
295 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
296 template <typename Char, typename Modifiers>
|
Chris@16
|
297 struct make_stream
|
Chris@16
|
298 {
|
Chris@16
|
299 static bool const lower =
|
Chris@16
|
300 has_modifier<Modifiers, tag::char_code_base<tag::lower> >::value;
|
Chris@16
|
301
|
Chris@16
|
302 static bool const upper =
|
Chris@16
|
303 has_modifier<Modifiers, tag::char_code_base<tag::upper> >::value;
|
Chris@16
|
304
|
Chris@16
|
305 typedef any_stream_generator<
|
Chris@16
|
306 Char
|
Chris@16
|
307 , typename spirit::detail::get_encoding_with_case<
|
Chris@16
|
308 Modifiers, unused_type, lower || upper>::type
|
Chris@16
|
309 , typename detail::get_casetag<Modifiers, lower || upper>::type
|
Chris@16
|
310 > result_type;
|
Chris@16
|
311
|
Chris@16
|
312 result_type operator()(unused_type, unused_type) const
|
Chris@16
|
313 {
|
Chris@16
|
314 return result_type();
|
Chris@16
|
315 }
|
Chris@16
|
316 };
|
Chris@16
|
317
|
Chris@16
|
318 // stream
|
Chris@16
|
319 template <typename Modifiers>
|
Chris@16
|
320 struct make_primitive<tag::stream, Modifiers>
|
Chris@16
|
321 : make_stream<char, Modifiers> {};
|
Chris@16
|
322
|
Chris@16
|
323 // wstream
|
Chris@16
|
324 template <typename Modifiers>
|
Chris@16
|
325 struct make_primitive<tag::wstream, Modifiers>
|
Chris@16
|
326 : make_stream<wchar_t, Modifiers> {};
|
Chris@16
|
327
|
Chris@16
|
328 // any_stream_generator<char_type>
|
Chris@16
|
329 template <typename Char, typename Modifiers>
|
Chris@16
|
330 struct make_primitive<tag::stream_tag<Char>, Modifiers>
|
Chris@16
|
331 : make_stream<Char, Modifiers> {};
|
Chris@16
|
332
|
Chris@16
|
333 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
334 template <typename Char, typename A0, typename Modifiers>
|
Chris@16
|
335 struct make_any_stream
|
Chris@16
|
336 {
|
Chris@16
|
337 static bool const lower =
|
Chris@16
|
338 has_modifier<Modifiers, tag::char_code_base<tag::lower> >::value;
|
Chris@16
|
339
|
Chris@16
|
340 static bool const upper =
|
Chris@16
|
341 has_modifier<Modifiers, tag::char_code_base<tag::upper> >::value;
|
Chris@16
|
342
|
Chris@16
|
343 typedef typename add_const<A0>::type const_attribute;
|
Chris@16
|
344 typedef lit_stream_generator<
|
Chris@16
|
345 const_attribute, Char
|
Chris@16
|
346 , typename spirit::detail::get_encoding_with_case<
|
Chris@16
|
347 Modifiers, unused_type, lower || upper>::type
|
Chris@16
|
348 , typename detail::get_casetag<Modifiers, lower || upper>::type
|
Chris@16
|
349 > result_type;
|
Chris@16
|
350
|
Chris@16
|
351 template <typename Terminal>
|
Chris@16
|
352 result_type operator()(Terminal const& term, unused_type) const
|
Chris@16
|
353 {
|
Chris@16
|
354 return result_type(fusion::at_c<0>(term.args));
|
Chris@16
|
355 }
|
Chris@16
|
356 };
|
Chris@16
|
357
|
Chris@16
|
358 // stream(...)
|
Chris@16
|
359 template <typename Modifiers, typename A0>
|
Chris@16
|
360 struct make_primitive<
|
Chris@16
|
361 terminal_ex<tag::stream, fusion::vector1<A0> >, Modifiers>
|
Chris@16
|
362 : make_any_stream<char, A0, Modifiers> {};
|
Chris@16
|
363
|
Chris@16
|
364 // wstream(...)
|
Chris@16
|
365 template <typename Modifiers, typename A0>
|
Chris@16
|
366 struct make_primitive<
|
Chris@16
|
367 terminal_ex<tag::wstream, fusion::vector1<A0> >, Modifiers>
|
Chris@16
|
368 : make_any_stream<wchar_t, A0, Modifiers> {};
|
Chris@16
|
369
|
Chris@16
|
370 // any_stream_generator<char_type>(...)
|
Chris@16
|
371 template <typename Char, typename Modifiers, typename A0>
|
Chris@16
|
372 struct make_primitive<
|
Chris@16
|
373 terminal_ex<tag::stream_tag<Char>, fusion::vector1<A0> >
|
Chris@16
|
374 , Modifiers>
|
Chris@16
|
375 : make_any_stream<Char, A0, Modifiers> {};
|
Chris@16
|
376
|
Chris@16
|
377 }}}
|
Chris@16
|
378
|
Chris@16
|
379 #endif
|