//Purpose: // Demonstrate that a stream generator causes call to /* // this is a special overload to detect if the output iterator has been // generated by a format_manip object. template < typename T, typename Traits, typename Properties, typename Context , typename Delimiter, typename Attribute > static bool generate( karma::detail::output_iterator< karma::ostream_iterator, Properties >& sink, Context& context, Delimiter const& d , Attribute const& attr) { */ // in boost/spirit/home/karma/stream/stream.hpp. // //#include #include #include namespace client { /////////////////////////////////////////////////////////////////////////// // Use stream generator and list operator to // generator a Attribute. /////////////////////////////////////////////////////////////////////////// template bool generate_by_stream(OutputIterator& sink, Attribute const& v) { using boost::spirit::karma::stream; using boost::spirit::karma::generate; bool r = generate( sink, // destination: output iterator stream, // the generator. Should just use operator<< on v. v // the attribute to output ); return r; } } //////////////////////////////////////////////////////////////////////////// // Following(till Main program), is just to allow breakpoint to be // set in operator<< to allow using debugger to see how // karma implements the stream generator. //////////////////////////////////////////////////////////////////////////// struct wrap_uns { unsigned my_val; wrap_uns(unsigned a_val) : my_val(a_val) {} }; namespace std { //MAINTENANCE_NOTE:2013-04-09.Larry Evans: // Must put following operator<< in std for // ADL, as explained here: // // http://boost.2283326.n4.nabble.com/karma-stream-generator-works-on-int-but-not-on-vector-lt-int-gt-tc4645053.html#a4645055 // std::ostream& operator<< (std::ostream& os, wrap_uns const& z) { os<<"wrap_uns("< sink_iter(ostream_sink); wrap_uns attr(999); ostream_sink<<"***{generation.\n"; bool const ok_gen=client::generate_by_stream(sink_iter, attr); ostream_sink<<"\n***}generation.\n"; ostream_sink.flush(); std::string generated=ostream_sink.str(); if(!ok_gen) { std::cout << "-------------------------\n"; std::cout << "Generating failed\n"; std::cout << "-------------------------\n"; } else { std::cout << "-------------------------\n"; std::cout << "Generated:\n" << generated << "\n"; std::cout << "-------------------------\n"; } return 0; }