| 1 | //Purpose:
|
|---|
| 2 | // Demonstrate that a stream generator causes call to
|
|---|
| 3 | /*
|
|---|
| 4 | // this is a special overload to detect if the output iterator has been
|
|---|
| 5 | // generated by a format_manip object.
|
|---|
| 6 | template <
|
|---|
| 7 | typename T, typename Traits, typename Properties, typename Context
|
|---|
| 8 | , typename Delimiter, typename Attribute
|
|---|
| 9 | >
|
|---|
| 10 | static bool generate(
|
|---|
| 11 | karma::detail::output_iterator<
|
|---|
| 12 | karma::ostream_iterator<T, Char, Traits>, Properties
|
|---|
| 13 | >& sink, Context& context, Delimiter const& d
|
|---|
| 14 | , Attribute const& attr)
|
|---|
| 15 | {
|
|---|
| 16 |
|
|---|
| 17 | */
|
|---|
| 18 | // in boost/spirit/home/karma/stream/stream.hpp.
|
|---|
| 19 | //
|
|---|
| 20 | //#include <boost/utility/demangled_type_name.hpp>
|
|---|
| 21 | #include <boost/spirit/include/karma.hpp>
|
|---|
| 22 |
|
|---|
| 23 | #include <sstream>
|
|---|
| 24 | namespace client
|
|---|
| 25 | {
|
|---|
| 26 | ///////////////////////////////////////////////////////////////////////////
|
|---|
| 27 | // Use stream generator and list operator to
|
|---|
| 28 | // generator a Attribute.
|
|---|
| 29 | ///////////////////////////////////////////////////////////////////////////
|
|---|
| 30 | template <typename OutputIterator, typename Attribute>
|
|---|
| 31 | bool generate_by_stream(OutputIterator& sink, Attribute const& v)
|
|---|
| 32 | {
|
|---|
| 33 | using boost::spirit::karma::stream;
|
|---|
| 34 | using boost::spirit::karma::generate;
|
|---|
| 35 |
|
|---|
| 36 | bool r = generate(
|
|---|
| 37 | sink, // destination: output iterator
|
|---|
| 38 | stream, // the generator. Should just use operator<< on v.
|
|---|
| 39 | v // the attribute to output
|
|---|
| 40 | );
|
|---|
| 41 | return r;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 47 | // Following(till Main program), is just to allow breakpoint to be
|
|---|
| 48 | // set in operator<< to allow using debugger to see how
|
|---|
| 49 | // karma implements the stream generator.
|
|---|
| 50 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 51 | struct wrap_uns
|
|---|
| 52 | {
|
|---|
| 53 | unsigned my_val;
|
|---|
| 54 | wrap_uns(unsigned a_val)
|
|---|
| 55 | : my_val(a_val)
|
|---|
| 56 | {}
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | namespace std
|
|---|
| 60 | {
|
|---|
| 61 | //MAINTENANCE_NOTE:2013-04-09.Larry Evans:
|
|---|
| 62 | // Must put following operator<< in std for
|
|---|
| 63 | // ADL, as explained here:
|
|---|
| 64 | //
|
|---|
| 65 | // http://boost.2283326.n4.nabble.com/karma-stream-generator-works-on-int-but-not-on-vector-lt-int-gt-tc4645053.html#a4645055
|
|---|
| 66 | //
|
|---|
| 67 | std::ostream&
|
|---|
| 68 | operator<< (std::ostream& os, wrap_uns const& z)
|
|---|
| 69 | {
|
|---|
| 70 | os<<"wrap_uns("<<z.my_val<<")";
|
|---|
| 71 | return os;
|
|---|
| 72 | }
|
|---|
| 73 | }//exit namespace std
|
|---|
| 74 |
|
|---|
| 75 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 76 | // Main program
|
|---|
| 77 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 78 | int
|
|---|
| 79 | main()
|
|---|
| 80 | {
|
|---|
| 81 | std::ostringstream ostream_sink;
|
|---|
| 82 | boost::spirit::karma::ostream_iterator
|
|---|
| 83 | < std::ostream::char_type
|
|---|
| 84 | >
|
|---|
| 85 | sink_iter(ostream_sink);
|
|---|
| 86 | wrap_uns attr(999);
|
|---|
| 87 | ostream_sink<<"***{generation.\n";
|
|---|
| 88 | bool const ok_gen=client::generate_by_stream(sink_iter, attr);
|
|---|
| 89 | ostream_sink<<"\n***}generation.\n";
|
|---|
| 90 | ostream_sink.flush();
|
|---|
| 91 | std::string generated=ostream_sink.str();
|
|---|
| 92 | if(!ok_gen)
|
|---|
| 93 | {
|
|---|
| 94 | std::cout << "-------------------------\n";
|
|---|
| 95 | std::cout << "Generating failed\n";
|
|---|
| 96 | std::cout << "-------------------------\n";
|
|---|
| 97 | }
|
|---|
| 98 | else
|
|---|
| 99 | {
|
|---|
| 100 | std::cout << "-------------------------\n";
|
|---|
| 101 | std::cout << "Generated:\n" << generated << "\n";
|
|---|
| 102 | std::cout << "-------------------------\n";
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return 0;
|
|---|
| 106 | }
|
|---|