generate_api.qbk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  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:generate_api Generator API]
  8. [//////////////////////////////////////////////////////////////////////////////]
  9. [section:iterator_api Iterator Based Generator API]
  10. [heading Description]
  11. The library provides a couple of free functions to make generating a snap.
  12. These generator functions have two forms. The first form, `generate`,
  13. concatenates the output generated by the involved components without inserting
  14. any output in between. The second `generate_delimited` intersperses the output
  15. generated by the involved components using the given delimiter generator.
  16. Both versions can take in attributes by (constant) reference that hold the
  17. attribute values to output.
  18. [heading Header]
  19. // forwards to <boost/spirit/home/karma/generate.hpp>
  20. #include <boost/spirit/include/karma_generate.hpp>
  21. For variadic attributes:
  22. // forwards to <boost/spirit/home/karma/generate_attr.hpp>
  23. #include <boost/spirit/include/karma_generate_attr.hpp>
  24. The variadic attributes version of the API allows one or more
  25. attributes to be passed into the `generate` functions. The functions taking two
  26. or more attributes are usable when the generator expression is a
  27. __karma_sequence__ only. In this case each of the
  28. attributes passed have to match the corresponding part of the sequence.
  29. For the API functions deducing the correct (matching) generator type from the
  30. supplied attribute type:
  31. // forwards to <boost/spirit/home/karma/detail/generate_auto.hpp>
  32. #include <boost/spirit/include/karma_generate_auto.hpp>
  33. Also, see __include_structure__.
  34. [heading Namespace]
  35. [table
  36. [[Name]]
  37. [[`boost::spirit::karma::generate` ]]
  38. [[`boost::spirit::karma::generate_delimited` ]]
  39. [[`boost::spirit::karma::delimit_flag::predelimit` ]]
  40. [[`boost::spirit::karma::delimit_flag::dont_predelimit` ]]
  41. ]
  42. [heading Synopsis]
  43. namespace boost { namespace spirit { namespace karma
  44. {
  45. template <typename OutputIterator, typename Expr>
  46. inline bool
  47. generate(
  48. OutputIterator& sink
  49. , Expr const& expr);
  50. template <typename OutputIterator, typename Expr
  51. , typename Attr1, typename Attr2, ..., typename AttrN>
  52. inline bool
  53. generate(
  54. OutputIterator& sink
  55. , Expr const& expr
  56. , Attr1 const& attr1, Attr2 const& attr2, ..., AttrN const& attrN);
  57. template <typename OutputIterator, typename Expr, typename Delimiter>
  58. inline bool
  59. generate_delimited(
  60. OutputIterator& sink
  61. , Expr const& expr
  62. , Delimiter const& delimiter
  63. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit = delimit_flag::dont_predelimit);
  64. template <typename OutputIterator, typename Expr, typename Delimiter
  65. , typename Attr1, typename Attr2, ..., typename AttrN>
  66. inline bool
  67. generate_delimited(
  68. OutputIterator& sink
  69. , Expr const& expr
  70. , Delimiter const& delimiter
  71. , Attr1 const& attr1, Attr2 const& attr2, ..., AttrN const& attrN);
  72. template <typename OutputIterator, typename Expr, typename Delimiter
  73. , typename Attr1, typename Attr2, ..., typename AttrN>
  74. inline bool
  75. generate_delimited(
  76. OutputIterator& sink
  77. , Expr const& expr
  78. , Delimiter const& delimiter
  79. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit
  80. , Attr1 const& attr1, Attr2 const& attr2, ..., AttrN const& attrN);
  81. }}}
  82. [note Starting with __spirit__ V2.5 (distributed with Boost V1.47) the
  83. placeholder `_val` can be used in semantic actions attached to top level
  84. generator components. In this case `_val` refers to the supplied attribute
  85. as a whole. For API functions taking more than one attribute argument
  86. `_val` will refer to a Fusion vector or references to the attributes.]
  87. __karma__ generator API functions based on the automatic creation of the
  88. matching generator type:
  89. namespace boost { namespace spirit { namespace karma
  90. {
  91. template <typename OutputIterator, typename Attr, typename Delimiter>
  92. inline bool
  93. generate_delimited(
  94. OutputIterator& sink
  95. , Attr const& attr
  96. , Delimiter const& delimiter
  97. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit = delimit_flag::dont_predelimit);
  98. template <typename OutputIterator, typename Attr>
  99. inline bool
  100. generate(
  101. OutputIterator& sink
  102. , Attr const& attr);
  103. }}}
  104. All functions above return `true` if none of the involved generator components
  105. failed, and `false` otherwise. If during the process of the output generation
  106. the underlying output stream reports an error, the return value will be `false`
  107. as well.
  108. The maximum number of supported arguments is limited by the preprocessor
  109. constant `SPIRIT_ARGUMENTS_LIMIT`. This constant defaults to the value defined
  110. by the preprocessor constant `PHOENIX_LIMIT` (which in turn defaults to `10`).
  111. [note The variadic functions with two or more attributes internally combine
  112. (constant) references to all passed attributes into a `fusion::vector`
  113. and forward this as a combined attribute to the corresponding function
  114. taking one attribute.]
  115. The `generate_delimited` functions not taking an explicit `delimit_flag` as one
  116. of their arguments don't invoke the passed delimiter before starting to generate
  117. output from the generator expression. This can be enabled by using the other
  118. version of that function while passing `delimit_flag::predelimit` to the
  119. corresponding argument.
  120. [heading Template parameters]
  121. [table
  122. [[Parameter] [Description]]
  123. [[`OutputIterator`] [__outputiter__ receiving the generated output.]]
  124. [[`Expr`] [An expression that can be converted to a Karma generator.]]
  125. [[`Delimiter`] [Generator used to delimit the output of the expression components.]]
  126. [[`Attr`] [An attribute type utilized to create the corresponding
  127. generator type from.]]
  128. [[`Attr1`, `Attr2`, ..., `AttrN`][One or more attributes.]]
  129. ]
  130. [endsect] [/ Iterator Based Generator API]
  131. [//////////////////////////////////////////////////////////////////////////////]
  132. [section:stream_api Stream Based Generator API]
  133. [heading Description]
  134. The library provides a couple of Standard IO __iomanip__ allowing to integrate
  135. __karma__ output generation facilities with Standard output streams.
  136. These generator manipulators have two forms. The first form, `format`,
  137. concatenates the output generated by the involved components without inserting
  138. any output in between. The second, `format_delimited`, intersperses the output
  139. generated by the involved components using the given delimiter generator.
  140. Both versions can take in attributes by (constant) reference that hold the
  141. attribute values to output.
  142. [heading Header]
  143. // forwards to <boost/spirit/home/karma/stream/format_manip.hpp>
  144. #include <boost/spirit/include/karma_format.hpp>
  145. For variadic attributes:
  146. // forwards to <boost/spirit/home/karma/stream/format_manip_attr.hpp>
  147. #include <boost/spirit/include/karma_format_attr.hpp>
  148. The variadic attributes version of the API allows one or more
  149. attributes to be passed into the `format` manipulators. The manipulators taking
  150. two or more attributes are usable when the generator expression is a
  151. __karma_sequence__ only. In this case each of the attributes passed have to
  152. match the corresponding part of the sequence.
  153. For the API functions deducing the correct (matching) generator type from the
  154. supplied attribute type:
  155. // forwards to <boost/spirit/home/karma/format_auto.hpp>
  156. #include <boost/spirit/include/karma_format_auto.hpp>
  157. Also, see __include_structure__.
  158. [heading Namespace]
  159. [table
  160. [[Name]]
  161. [[`boost::spirit::karma::format` ]]
  162. [[`boost::spirit::karma::format_delimited` ]]
  163. [[`boost::spirit::karma::delimit_flag::predelimit` ]]
  164. [[`boost::spirit::karma::delimit_flag::dont_predelimit` ]]
  165. ]
  166. [heading Synopsis]
  167. namespace boost { namespace spirit { namespace karma
  168. {
  169. template <typename Expr>
  170. inline <unspecified>
  171. format(
  172. Expr const& xpr);
  173. template <typename Expr
  174. , typename Attr1, typename Attr2, ..., typename AttrN>
  175. inline <unspecified>
  176. format(
  177. Expr const& xpr
  178. , Attr1 const& attr1, Attr2 const& attr2, ..., AttrN const& attrN);
  179. template <typename Expr, typename Delimiter>
  180. inline <unspecified>
  181. format_delimited(
  182. Expr const& expr
  183. , Delimiter const& d
  184. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit = delimit_flag::dont_predelimit);
  185. template <typename Expr, typename Delimiter
  186. , typename Attr1, typename Attr2, ..., typename AttrN>
  187. inline <unspecified>
  188. format_delimited(
  189. Expr const& expr
  190. , Delimiter const& d
  191. , Attr1 const& attr1, Attr2 const& attr2, ..., AttrN const& attrN);
  192. template <typename Expr, typename Delimiter
  193. , typename Attr1, typename Attr2, ..., typename AttrN>
  194. inline <unspecified>
  195. format_delimited(
  196. Expr const& expr
  197. , Delimiter const& d
  198. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit
  199. , Attr1 const& attr1, Attr2 const& attr2, ..., AttrN const& attrN);
  200. }}}
  201. __karma__ generator API functions based on the automatic creation of the
  202. matching generator type:
  203. namespace boost { namespace spirit { namespace karma
  204. {
  205. template <typename Attr, typename Delimiter>
  206. inline <unspecified>
  207. format_delimited(
  208. Attr const& attr
  209. , Delimiter const& d
  210. , BOOST_SCOPED_ENUM(delimit_flag) pre_delimit = delimit_flag::dont_predelimit);
  211. template <typename Attr>
  212. inline <unspecified>
  213. format(
  214. Attr const& xpr);
  215. }}}
  216. All functions above return a standard IO stream manipulator instance (see
  217. __iomanip__), which when streamed to an output stream will result in generating
  218. the output as emitted by the embedded __karma__ generator expression. Any error
  219. occurring during the invocation of the __karma__ generators will be reflected
  220. in the streams status flag (`std::ios_base::failbit` will be set).
  221. The maximum number of supported arguments is limited by the preprocessor
  222. constant `SPIRIT_ARGUMENTS_LIMIT`. This constant defaults to the value defined
  223. by the preprocessor constant `PHOENIX_LIMIT` (which in turn defaults to `10`).
  224. [note The variadic manipulators with two or more attributes internally combine
  225. (constant) references to all passed attributes into a `fusion::vector`
  226. and forward this as a combined attribute to the corresponding manipulator
  227. taking one attribute.]
  228. The `format_delimited` manipulators not taking an explicit `delimit_flag` as one
  229. of their arguments don't invoke the passed delimiter before starting to generate
  230. output from the generator expression. This can be enabled by using the other
  231. version of that manipulator while passing `delimit_flag::predelimit` to the
  232. corresponding argument.
  233. [heading Template parameters]
  234. [table
  235. [[Parameter] [Description]]
  236. [[`Expr`] [An expression that can be converted to a Karma generator.]]
  237. [[`Delimiter`] [Generator used to delimit the output of the expression components.]]
  238. [[`Attr`] [An attribute type utilized to create the corresponding
  239. generator type from.]]
  240. [[`Attr1`, `Attr2`, ..., `AttrN`][One or more attributes.]]
  241. ]
  242. [endsect] [/ Stream Based Generator API]
  243. [//////////////////////////////////////////////////////////////////////////////]
  244. [section:create_generator API for Automatic Generator Creation]
  245. [heading Description]
  246. The library implements a special API returning a generator instance for a
  247. supplied attribute type. This function finds the best matching generator type
  248. for the attribute based on a set of simple matching rules (as outlined in the
  249. table below) applied recursively to the attribute type. The returned generator
  250. can be utilized to emit output for the provided attribute.
  251. [heading Header]
  252. // forwards to <boost/spirit/home/karma/auto.hpp>
  253. #include <boost/spirit/include/karma_auto.hpp>
  254. Also, see __include_structure__.
  255. [heading Namespace]
  256. [table
  257. [[Name]]
  258. [[`boost::spirit::karma::create_generator`]]
  259. [[`boost::spirit::traits::create_generator_exists`]]
  260. ]
  261. [heading Synopsis]
  262. namespace boost { namespace spirit { namespace karma
  263. {
  264. template <typename Attr>
  265. inline <unspecified>
  266. create_generator();
  267. }}}
  268. The returned instance can be directly passed as the generator (or the
  269. delimiting generator) to any of the __karma__ API functions. Additionally it
  270. can be assigned to a rule as the rules right hand side expression. This
  271. function will return a valid generator type only if the meta function
  272. `traits::create_generator_exists` returns `mpl::true_`. Otherwise it will fail
  273. compiling.
  274. namespace boost { namespace spirit { namespace traits
  275. {
  276. template <typename Attr>
  277. struct create_generator_exists;
  278. }}}
  279. The meta function evaluates to `mpl::true_` if `create_generator` would return
  280. a valid generator for the given type `Attr`.
  281. The following table outlines the mapping rules from the attribute type to the
  282. generator type. These rules are applied recursively to create the generator
  283. type which can be used to generate output from the given attribute type.
  284. [table
  285. [[Attribute type] [Generator type]]
  286. [[`char`, `wchar_t`] [`standard::char_`, `standard_wide::char_`]]
  287. [[`short`, `int`, `long`] [`short_`, `int_`, `long_`]]
  288. [[`unsigned short`, `unsigned int`, `unsigned long`]
  289. [`ushort_`, `uint_`, `ulong_`]]
  290. [[`float`, `double`, `long double`] [`float_`, `double_`, `long_double`]]
  291. [[`short`, `int`, `long`] [`short_`, `int_`, `long_`]]
  292. [[`long long`, `unsigned long long`]
  293. [`long_long`, `ulong_long`]]
  294. [[`bool`] [`bool_`]]
  295. [[Any string (`char const*`, `std::string`, etc.)]
  296. [`string`]]
  297. [[Any (STL) container] [Kleene Star (unary `'*'`)]]
  298. [[Any Fusion sequence] [Sequence operator (`'<<'`)]]
  299. [[`boost::optional<>`] [Optional operator (unary `'-'`)]]
  300. [[`boost::variant<>`] [Alternative operator (`'|'`)]]
  301. ]
  302. [important The mapping for the generators `long_long` and `ulong_long` are only
  303. available on platforms where the preprocessor constant
  304. `BOOST_HAS_LONG_LONG` is defined (i.e. on platforms having native
  305. support for `long long` and `unsigned long long` (64 bit) signed and
  306. unsigned integer types).]
  307. [heading Template parameters]
  308. [table
  309. [[Parameter] [Description]]
  310. [[`Attr`] [An attribute type utilized to create the corresponding
  311. generator type from.]]
  312. ]
  313. [endsect] [/ API for Automatic Generator Creation]
  314. [endsect]