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(BOOST_SPIRIT_KARMA_STREAM_MAY_01_2007_0310PM) Chris@16: #define BOOST_SPIRIT_KARMA_STREAM_MAY_01_2007_0310PM 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: #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: #include Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { namespace spirit Chris@16: { Chris@16: namespace tag Chris@16: { Chris@16: template Chris@16: struct stream_tag Chris@16: { Chris@16: BOOST_SPIRIT_IS_TAG() Chris@16: }; Chris@16: } Chris@16: Chris@16: namespace karma Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////// Chris@16: // This one is the class that the user can instantiate directly in Chris@16: // order to create a customized int generator Chris@16: template Chris@16: struct stream_generator Chris@16: : spirit::terminal > Chris@16: {}; Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Enablers Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template <> Chris@16: struct use_terminal // enables stream Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> Chris@16: struct use_terminal // enables wstream Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template <> // enables stream(f) Chris@16: struct use_lazy_terminal< Chris@16: karma::domain, tag::stream, 1 /*arity*/ Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template <> // enables wstream(f) Chris@16: struct use_lazy_terminal< Chris@16: karma::domain, tag::wstream, 1 /*arity*/ Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: // enables stream_generator Chris@16: template Chris@16: struct use_terminal > Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal, fusion::vector1 > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_lazy_terminal< Chris@16: karma::domain, tag::stream_tag, 1 /*arity*/ Chris@16: > : mpl::true_ {}; Chris@16: 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::stream; Chris@16: using spirit::wstream; Chris@16: #endif Chris@16: using spirit::stream_type; Chris@16: using spirit::wstream_type; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct any_stream_generator Chris@16: : primitive_generator > Chris@16: { Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: typedef spirit::basic_hold_any type; Chris@16: }; Chris@16: Chris@16: // any_stream_generator has an attached attribute Chris@16: template < Chris@16: typename OutputIterator, typename Context, typename Delimiter Chris@16: , typename Attribute Chris@16: > Chris@16: static bool generate(OutputIterator& sink, Context& context Chris@16: , Delimiter const& d, Attribute const& attr) Chris@16: { Chris@16: typedef karma::detail::iterator_sink< Chris@16: OutputIterator, Char, CharEncoding, Tag Chris@16: > sink_device; Chris@16: Chris@16: if (!traits::has_optional_value(attr)) Chris@16: return false; Chris@16: Chris@16: // use existing operator<<() Chris@16: typedef typename attribute::type attribute_type; Chris@16: Chris@16: { Chris@16: boost::iostreams::stream ostr(sink); Chris@16: ostr << traits::extract_from(attr, context) << std::flush; Chris@16: Chris@16: if (!ostr.good()) Chris@16: return false; Chris@16: } Chris@16: Chris@16: return karma::delimit_out(sink, d); // always do post-delimiting Chris@16: } Chris@16: Chris@16: // this is a special overload to detect if the output iterator has been Chris@16: // generated by a format_manip object. Chris@16: template < Chris@16: typename T, typename Traits, typename Properties, typename Context Chris@16: , typename Delimiter, typename Attribute Chris@16: > Chris@16: static bool generate( Chris@16: karma::detail::output_iterator< Chris@16: karma::ostream_iterator, Properties Chris@16: >& sink, Context& context, Delimiter const& d Chris@16: , Attribute const& attr) Chris@16: { Chris@16: typedef karma::detail::output_iterator< Chris@16: karma::ostream_iterator, Properties Chris@16: > output_iterator; Chris@16: typedef karma::detail::iterator_sink< Chris@16: output_iterator, Char, CharEncoding, Tag Chris@16: > sink_device; Chris@16: Chris@16: if (!traits::has_optional_value(attr)) Chris@16: return false; Chris@16: Chris@16: // use existing operator<<() Chris@16: typedef typename attribute::type attribute_type; Chris@16: Chris@16: { Chris@16: boost::iostreams::stream ostr(sink); Chris@16: ostr.imbue(sink.get_ostream().getloc()); Chris@16: ostr << traits::extract_from(attr, context) Chris@16: << std::flush; Chris@16: if (!ostr.good()) Chris@16: return false; Chris@16: } Chris@16: Chris@16: return karma::delimit_out(sink, d); // always do post-delimiting Chris@16: } Chris@16: Chris@16: // this any_stream has no parameter attached, it needs to have been Chris@16: // initialized from a value/variable Chris@16: template Chris@16: static bool Chris@16: generate(OutputIterator&, Context&, Delimiter const&, unused_type) Chris@16: { Chris@16: // It is not possible (doesn't make sense) to use stream generators Chris@16: // without providing any attribute, as the generator doesn't 'know' Chris@16: // what to output. The following assertion fires if this situation Chris@16: // is detected in your code. Chris@16: BOOST_SPIRIT_ASSERT_FAIL(OutputIterator, stream_not_usable_without_attribute, ()); Chris@16: return false; Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& /*context*/) const Chris@16: { Chris@16: return info("stream"); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct lit_stream_generator Chris@16: : primitive_generator > Chris@16: { Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: typedef unused_type type; Chris@16: }; Chris@16: Chris@16: lit_stream_generator(typename add_reference::type t) Chris@16: : t_(t) Chris@16: {} Chris@16: Chris@16: // lit_stream_generator has an attached parameter Chris@16: Chris@16: // this overload will be used in the normal case (not called from Chris@16: // format_manip). Chris@16: template < Chris@16: typename OutputIterator, typename Context, typename Delimiter Chris@16: , typename Attribute> Chris@16: bool generate(OutputIterator& sink, Context&, Delimiter const& d Chris@16: , Attribute const&) const Chris@16: { Chris@16: typedef karma::detail::iterator_sink< Chris@16: OutputIterator, Char, CharEncoding, Tag Chris@16: > sink_device; Chris@16: Chris@16: boost::iostreams::stream ostr(sink); Chris@16: ostr << t_ << std::flush; // use existing operator<<() Chris@16: Chris@16: if (ostr.good()) Chris@16: return karma::delimit_out(sink, d); // always do post-delimiting Chris@16: return false; Chris@16: } Chris@16: Chris@16: // this is a special overload to detect if the output iterator has been Chris@16: // generated by a format_manip object. Chris@16: template < Chris@16: typename T1, typename Traits, typename Properties Chris@16: , typename Context, typename Delimiter, typename Attribute> Chris@16: bool generate( Chris@16: karma::detail::output_iterator< Chris@16: karma::ostream_iterator, Properties Chris@16: >& sink, Context&, Delimiter const& d, Attribute const&) const Chris@16: { Chris@16: typedef karma::detail::output_iterator< Chris@16: karma::ostream_iterator, Properties Chris@16: > output_iterator; Chris@16: typedef karma::detail::iterator_sink< Chris@16: output_iterator, Char, CharEncoding, Tag Chris@16: > sink_device; Chris@16: Chris@16: { Chris@16: boost::iostreams::stream ostr(sink); Chris@16: ostr.imbue(sink.get_ostream().getloc()); Chris@16: ostr << t_ << std::flush; // use existing operator<<() Chris@16: Chris@16: if (!ostr.good()) Chris@16: return false; Chris@16: } Chris@16: Chris@16: return karma::delimit_out(sink, d); // always do post-delimiting Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& /*context*/) const Chris@16: { Chris@16: return info("any-stream"); Chris@16: } Chris@16: Chris@16: T t_; Chris@16: Chris@16: private: Chris@16: // silence MSVC warning C4512: assignment operator could not be generated Chris@16: lit_stream_generator& operator= (lit_stream_generator const&); Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Generator generators: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_stream Chris@16: { Chris@16: static bool const lower = Chris@16: has_modifier >::value; Chris@16: Chris@16: static bool const upper = Chris@16: has_modifier >::value; Chris@16: Chris@16: typedef any_stream_generator< Chris@16: Char Chris@16: , typename spirit::detail::get_encoding_with_case< Chris@16: Modifiers, unused_type, lower || upper>::type Chris@16: , typename detail::get_casetag::type Chris@16: > result_type; Chris@16: Chris@16: result_type operator()(unused_type, unused_type) const Chris@16: { Chris@16: return result_type(); Chris@16: } Chris@16: }; Chris@16: Chris@16: // stream Chris@16: template Chris@16: struct make_primitive Chris@16: : make_stream {}; Chris@16: Chris@16: // wstream Chris@16: template Chris@16: struct make_primitive Chris@16: : make_stream {}; Chris@16: Chris@16: // any_stream_generator Chris@16: template Chris@16: struct make_primitive, Modifiers> Chris@16: : make_stream {}; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_any_stream Chris@16: { Chris@16: static bool const lower = Chris@16: has_modifier >::value; Chris@16: Chris@16: static bool const upper = Chris@16: has_modifier >::value; Chris@16: Chris@16: typedef typename add_const::type const_attribute; Chris@16: typedef lit_stream_generator< Chris@16: const_attribute, Char Chris@16: , typename spirit::detail::get_encoding_with_case< Chris@16: Modifiers, unused_type, lower || upper>::type Chris@16: , typename detail::get_casetag::type Chris@16: > result_type; Chris@16: Chris@16: template Chris@16: result_type operator()(Terminal const& term, unused_type) const Chris@16: { Chris@16: return result_type(fusion::at_c<0>(term.args)); Chris@16: } Chris@16: }; Chris@16: Chris@16: // stream(...) Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : make_any_stream {}; Chris@16: Chris@16: // wstream(...) Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : make_any_stream {}; Chris@16: Chris@16: // any_stream_generator(...) Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex, fusion::vector1 > Chris@16: , Modifiers> Chris@16: : make_any_stream {}; Chris@16: Chris@16: }}} Chris@16: Chris@16: #endif