...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The stream_generator
is a primitive which allows to use pre-existing standard streaming operators
for output generation integrated with Spirit.Karma.
It provides a wrapper generator dispatching the value to output to the
stream operator of the corresponding type. Any value a
to be formatted using the stream_generator
will result in invoking the standard streaming operator for its type
A
, for instance:
std::ostream& operator<< (std::ostream&, A const&);
// forwards to <boost/spirit/home/karma/stream.hpp> #include <boost/spirit/include/karma_stream.hpp>
Also, see Include Structure.
Name |
---|
|
|
template <typename Char> struct stream_generator;
Parameter |
Description |
Default |
---|---|---|
|
The character type to use to generate the output. This type will be used while assigning the generated characters to the underlying output iterator. |
|
Notation
s
A variable instance of any type with a defined matching streaming
operator<<()
or a Lazy
Argument that evaluates to any type with a defined matching
streaming operator<<()
.
Semantics of an expression is defined only where it differs from, or
is not defined in PrimitiveGenerator
.
Expression |
Description |
---|---|
|
Call the streaming |
|
Call the streaming |
|
Call the streaming |
|
Call the streaming |
All generators listed in the table above are predefined specializations
of the stream_generator<Char>
basic stream generator type described
below. It is possible to directly use this type to create stream generators
using an arbitrary underlying character type.
Expression |
Semantics |
---|---|
stream_generator< Char >()
|
Call the streaming |
stream_generator< Char >()(s)
|
Call the streaming |
All of the stream generators listed above require the type of the value
to generate output for (either the immediate value or the associated
attribute) to implement a streaming operator conforming to the usual
I/O streams conventions (where attribute_type
is the type of the value to generate output for):
template <typename Ostream> Ostream& operator<< (Ostream& os, attribute_type const& attr) { // type specific output generation return os; }
This operator will be called by the stream generators to gather the output
for the attribute of type attribute_type
.
All data streamed into the given Ostream
will end up being generated by the corresponding stream generator instance.
Note | |
---|---|
If the |
Expression |
Attribute |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Important | |
---|---|
The attribute type |
Note | |
---|---|
In addition to their usual attribute of type |
O(N), where N is the number of characters emitted by the stream generator
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
Some includes:
#include <boost/spirit/include/karma.hpp> #include <boost/spirit/include/support_utree.hpp> #include <boost/phoenix/core.hpp> #include <boost/phoenix/operator.hpp> #include <boost/fusion/include/std_pair.hpp> #include <boost/proto/deep_copy.hpp> #include <iostream> #include <string>
Some using declarations:
using boost::spirit::karma::stream;
And a class definition used in the examples:
// a simple complex number representation z = a + bi struct complex { complex (double a, double b) : a(a), b(b) {} double a; double b; };
// define streaming operator for the type complex std::ostream& operator<< (std::ostream& os, complex const& z) { os << "{" << z.a << "," << z.b << "}"; return os; }
Basic usage of stream
generators:
test_generator_attr("abc", stream, "abc"); test_generator("abc", stream("abc")); test_generator_attr("{1.2,2.4}", stream, complex(1.2, 2.4)); test_generator("{1.2,2.4}", stream(complex(1.2, 2.4)));