stream.qbk 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Hartmut Kaiser
  3. Copyright (C) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ===============================================================================/]
  7. [section:stream Stream Generators]
  8. This module includes the description of the different variants of the `stream`
  9. generator. It can be used to utilize existing streaming operators
  10. (`operator<<(std::ostream&, ...)`) for output generation.
  11. [heading Header]
  12. // forwards to <boost/spirit/home/karma/stream.hpp>
  13. #include <boost/spirit/include/karma_stream.hpp>
  14. Also, see __include_structure__.
  15. [section:stream Stream Generators (`stream`, `wstream`, etc.)]
  16. [heading Description]
  17. The `stream_generator` is a primitive which allows to use pre-existing standard
  18. streaming operators for output generation integrated with __karma__. It
  19. provides a wrapper generator dispatching the value to output to the stream
  20. operator of the corresponding type. Any value `a` to be formatted using the
  21. `stream_generator` will result in invoking the standard streaming operator
  22. for its type `A`, for instance:
  23. std::ostream& operator<< (std::ostream&, A const&);
  24. [heading Header]
  25. // forwards to <boost/spirit/home/karma/stream.hpp>
  26. #include <boost/spirit/include/karma_stream.hpp>
  27. Also, see __include_structure__.
  28. [heading Namespace]
  29. [table
  30. [[Name]]
  31. [[`boost::spirit::stream // alias: boost::spirit::karma::stream`]]
  32. [[`boost::spirit::wstream // alias: boost::spirit::karma::wstream`]]
  33. ]
  34. [heading Synopsis]
  35. template <typename Char>
  36. struct stream_generator;
  37. [heading Template parameters]
  38. [table
  39. [[Parameter] [Description] [Default]]
  40. [[`Char`] [The character type to use to generate
  41. the output. This type will be used while
  42. assigning the generated characters to the
  43. underlying output iterator.] [`char`]]
  44. ]
  45. [heading Model of]
  46. [:__primitive_generator_concept__]
  47. [variablelist Notation
  48. [[`s`] [A variable instance of any type with a defined matching
  49. streaming `operator<<()` or a __karma_lazy_argument__ that
  50. evaluates to any type with a defined matching streaming
  51. `operator<<()`.]]
  52. ]
  53. [heading Expression Semantics]
  54. Semantics of an expression is defined only where it differs from, or is
  55. not defined in __primitive_generator_concept__.
  56. [table
  57. [[Expression] [Description]]
  58. [[`stream`] [Call the streaming `operator<<()` for the type
  59. of the mandatory attribute. The output emitted
  60. by this operator will be the result of the
  61. `stream` generator. This generator never fails
  62. (unless the underlying output stream reports an
  63. error). The character type of the I/O ostream
  64. is assumed to be `char`.]]
  65. [[`stream(s)`] [Call the streaming `operator<<()` for the type
  66. of the immediate value `s`. The output emitted
  67. by this operator will be the result of the
  68. `stream` generator. This generator never fails
  69. (unless the underlying output stream reports an
  70. error). The character type of the I/O ostream
  71. is assumed to be `char`.]]
  72. [[`wstream`] [Call the streaming `operator<<()` for the type
  73. of the mandatory attribute. The output emitted
  74. by this operator will be the result of the
  75. `stream` generator. This generator never fails
  76. (unless the underlying output stream reports an
  77. error). The character type of the I/O ostream
  78. is assumed to be `wchar_t`.]]
  79. [[`wstream(s)`] [Call the streaming `operator<<()` for the type
  80. of the immediate value `s`. The output emitted
  81. by this operator will be the result of the
  82. `stream` generator. This generator never fails
  83. (unless the underlying output stream reports an
  84. error). The character type of the I/O ostream
  85. is assumed to be `wchar_t`.]]
  86. ]
  87. All generators listed in the table above are predefined specializations of the
  88. `stream_generator<Char>` basic stream generator type described below. It is
  89. possible to directly use this type to create stream generators using an
  90. arbitrary underlying character type.
  91. [table
  92. [[Expression] [Semantics]]
  93. [
  94. [``stream_generator<
  95. Char
  96. >()``] [Call the streaming `operator<<()` for the type
  97. of the mandatory attribute. The output emitted
  98. by this operator will be the result of the
  99. `stream` generator. This generator never fails
  100. (unless the underlying output stream reports an
  101. error). The character type of the I/O ostream
  102. is assumed to be `Char`]]
  103. [
  104. [``stream_generator<
  105. Char
  106. >()(s)``] [Call the streaming `operator<<()` for the type
  107. of the immediate value `s`. The output emitted
  108. by this operator will be the result of the
  109. `stream` generator. This generator never fails
  110. (unless the underlying output stream reports an
  111. error). The character type of the I/O ostream
  112. is assumed to be `Char`.]]
  113. ]
  114. [heading Additional Requirements]
  115. All of the stream generators listed above require the type of the value to
  116. generate output for (either the immediate value or the associated attribute) to
  117. implement a streaming operator conforming to the usual I/O streams conventions
  118. (where `attribute_type` is the type of the value to generate output for):
  119. template <typename Ostream>
  120. Ostream& operator<< (Ostream& os, attribute_type const& attr)
  121. {
  122. // type specific output generation
  123. return os;
  124. }
  125. This operator will be called by the stream generators to gather the output for
  126. the attribute of type `attribute_type`. All data streamed into the given
  127. `Ostream` will end up being generated by the corresponding stream generator
  128. instance.
  129. [note If the `stream` generator is invoked inside a [karma_format `format`]
  130. (or [karma_format `format_delimited`]) stream manipulator the `Ostream`
  131. passed to the `operator<<()` will have registered (imbued) the same
  132. standard locale instance as the stream the [karma_format `format`] (or
  133. [karma_format `format_delimited`]) manipulator has been used with.
  134. This ensures all facets registered (imbued) with the original I/O
  135. stream object are used during output generation.
  136. ]
  137. [heading Attributes]
  138. [table
  139. [[Expression] [Attribute]]
  140. [[`stream`] [`hold_any`, attribute is mandatory (otherwise compilation will fail)]]
  141. [[`stream(s)`] [__unused__]]
  142. [[`wstream`] [`whold_any`, attribute is mandatory (otherwise compilation will fail)]]
  143. [[`wstream(s)`] [__unused__]]
  144. [[`stream_generator<Char>()`] [`basic_hold_any<Char>`, attribute is mandatory (otherwise compilation will fail)]]
  145. [[`stream_generator<Char>()(s)`] [__unused__]]
  146. ]
  147. [important The attribute type `hold_any` exposed by some of the stream
  148. generators is semantically and syntactically equivalent to
  149. the type implemented by __boost_any__. It has been added to /Spirit/
  150. as it has better a performance and a smaller footprint if compared to
  151. __boost_any__.
  152. ]
  153. [note In addition to their usual attribute of type `Attrib` all listed generators
  154. accept an instance of a `boost::optional<Attrib>` as well. If the
  155. `boost::optional<>` is initialized (holds a value) the generators behave
  156. as if their attribute was an instance of `Attrib` and emit the value stored
  157. in the `boost::optional<>`. Otherwise the generators will fail.]
  158. [heading Complexity]
  159. [:O(N), where N is the number of characters emitted by the stream generator]
  160. [heading Example]
  161. [note The test harness for the example(s) below is presented in the
  162. __karma_basics_examples__ section.]
  163. Some includes:
  164. [reference_karma_includes]
  165. Some using declarations:
  166. [reference_karma_using_declarations_stream]
  167. And a class definition used in the examples:
  168. [reference_karma_complex]
  169. [reference_karma_stream_complex]
  170. Basic usage of `stream` generators:
  171. [reference_karma_stream]
  172. [endsect]
  173. [endsect]