numeric.qbk 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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:numeric Numeric Generators]
  8. The library includes a couple of predefined objects for generating booleans,
  9. signed and unsigned integers, and real numbers. These generators are fully
  10. parametric. Most of the important aspects of numeric generation can be
  11. finely adjusted to suit. This includes the radix base, the exponent, the
  12. fraction etc. Policies control the real number generators' behavior. There are
  13. some predefined policies covering the most common real number formats but the
  14. user can supply her own when needed.
  15. The numeric parsers are fine tuned (employing loop unrolling and
  16. extensive template metaprogramming) with exceptional performance that
  17. rivals the low level C functions such as `ltoa`, `ssprintf`, and `_gcvt`.
  18. Benchmarks reveal up to 2X speed over the C counterparts (see here:
  19. __sec_karma_numeric_performance__). This goes to show that you can write
  20. extremely tight generic C++ code that rivals, if not surpasses C.
  21. [heading Module Header]
  22. // forwards to <boost/spirit/home/karma/numeric.hpp>
  23. #include <boost/spirit/include/karma_numeric.hpp>
  24. Also, see __include_structure__.
  25. [/////////////////////////////////////////////////////////////////////////////]
  26. [section:unsigned_int Unsigned Integer Number Generators (`uint_`, etc.)]
  27. [heading Description]
  28. The `uint_generator` class is the simplest among the members of the
  29. numerics package. The `uint_generator` can generate unsigned integers of
  30. arbitrary length and size. The `uint_generator` generator can be used to
  31. generate ordinary primitive C/C++ integers or even user defined scalars such as
  32. bigints (unlimited precision integers) if the type follows
  33. certain expression requirements (for more information about the requirements, see
  34. [link spirit.karma.reference.numeric.unsigned_int.additional_requirements below])).
  35. The `uint_generator` is a template class. Template parameters fine tune its behavior.
  36. [heading Header]
  37. // forwards to <boost/spirit/home/karma/numeric/uint.hpp>
  38. #include <boost/spirit/include/karma_uint.hpp>
  39. Also, see __include_structure__.
  40. [heading Namespace]
  41. [table
  42. [[Name]]
  43. [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
  44. [[`boost::spirit::bin // alias: boost::spirit::karma::bin`]]
  45. [[`boost::spirit::oct // alias: boost::spirit::karma::oct`]]
  46. [[`boost::spirit::hex // alias: boost::spirit::karma::hex`]]
  47. [[`boost::spirit::ushort_ // alias: boost::spirit::karma::ushort_`]]
  48. [[`boost::spirit::ulong_ // alias: boost::spirit::karma::ulong_`]]
  49. [[`boost::spirit::uint_ // alias: boost::spirit::karma::uint_`]]
  50. [[`boost::spirit::ulong_long // alias: boost::spirit::karma::ulong_long`]]
  51. ]
  52. [note The generators `ulong_long` and `ulong_long(num)` are only available on
  53. platforms where the preprocessor constant `BOOST_HAS_LONG_LONG` is
  54. defined (i.e. on platforms having native support for `unsigned long long`
  55. (64 bit) unsigned integer types).]
  56. [note `lit` is reused by the [karma_string String Generators], the
  57. __karma_char__, and the Numeric Generators. In
  58. general, a char generator is created when you pass in a
  59. character, a string generator is created when you pass in a string, and a
  60. numeric generator is created when you use a numeric literal.]
  61. [heading Synopsis]
  62. template <
  63. typename Num
  64. , unsigned Radix>
  65. struct uint_generator;
  66. [heading Template parameters]
  67. [table
  68. [[Parameter] [Description] [Default]]
  69. [[`Num`] [The numeric base type of the
  70. numeric generator.] [`unsigned int`]]
  71. [[`Radix`] [The radix base. This can be
  72. any value in the (inclusive) range from `2` .. `36`.] [`10`]]
  73. ]
  74. [heading Model of]
  75. [:__primitive_generator_concept__]
  76. [variablelist Notation
  77. [[`num`] [Numeric literal, any unsigned integer value, or
  78. a __karma_lazy_argument__ that evaluates to an
  79. unsigned integer value of type `Num`]]
  80. [[`Num`] [Type of `num`: any unsigned integer type, or in case
  81. of a __karma_lazy_argument__, its return value]]
  82. [[`Radix`] [An integer literal specifying the required radix for
  83. the output conversion. Valid values are from the
  84. (inclusive) range `2` .. `36`.]]
  85. ]
  86. [heading Expression Semantics]
  87. Semantics of an expression is defined only where it differs from, or is
  88. not defined in __primitive_generator_concept__.
  89. [table
  90. [[Expression] [Semantics]]
  91. [[`lit(num)`] [Generate the unsigned integer literal `num` using the
  92. default formatting (radix is `10`). This generator never
  93. fails (unless the underlying output stream reports
  94. an error).]]
  95. [
  96. [``ushort_
  97. uint_
  98. ulong_
  99. ulong_long``] [Generate the unsigned integer provided by a mandatory
  100. attribute using the default formatting (radix is `10`).
  101. This generator never fails (unless the underlying
  102. output stream reports an error).]]
  103. [
  104. [``ushort_(num)
  105. uint_(num)
  106. ulong_(num)
  107. ulong_long(num)``] [Generate the unsigned integer provided by the
  108. immediate literal value the generator is initialized
  109. from using the default formatting (radix is `10`). If
  110. this generator has an associated attribute it succeeds
  111. only if the attribute is equal to the
  112. immediate literal (unless the underlying output
  113. stream reports an error). Otherwise this generator
  114. fails and does not generate any output.]]
  115. [
  116. [``bin
  117. oct
  118. hex``] [Generate the unsigned integer provided by a mandatory
  119. attribute using the default formatting and the
  120. corresponding radix (`bin`: radix is `2`, `oct`: radix is `8`,
  121. `hex`: radix is `16`). This generator never fails (unless
  122. the underlying output stream reports an error).]]
  123. [
  124. [``bin(num)
  125. oct(num)
  126. hex(num)``] [Generate the unsigned integer provided by the
  127. immediate literal value the generator is initialized
  128. from using the default formatting and the
  129. corresponding radix (`bin`: radix is `2`, `oct`:
  130. radix is `8`, `hex`: radix is `16`). If
  131. this generator has an associated attribute it succeeds
  132. only if the attribute is equal to the
  133. immediate literal (unless the underlying output
  134. stream reports an error). Otherwise this generator
  135. fails and does not generate any output.]]
  136. ]
  137. All generators listed in the table above (except `lit(num)`) are predefined
  138. specializations of the `uint_generator<Num, Radix>` basic unsigned integer
  139. number generator type described below. It is possible to directly use this
  140. type to create unsigned integer generators using a wide range of formatting
  141. options.
  142. [table
  143. [[Expression] [Semantics]]
  144. [
  145. [``uint_generator<
  146. Num, Radix
  147. >()``]
  148. [Generate the unsigned integer of type `Num` provided
  149. by a mandatory attribute using the specified `Radix`
  150. (allowed values are from the (inclusive) range from
  151. `2` .. `36`, the
  152. default value is `10`).This generator never fails
  153. (unless the underlying output stream reports an
  154. error).]]
  155. [
  156. [``uint_generator<
  157. Num, Radix
  158. >()(num)``]
  159. [Generate the unsigned integer of type `Num` provided
  160. by the immediate literal value the generator is
  161. initialized from, using the specified `Radix`
  162. (allowed values are from the (inclusive) range from
  163. `2` .. `36`, the
  164. default value is `10`). If this generator has an
  165. associated attribute it succeeds only if the
  166. attribute is equal to the immediate literal (unless
  167. the underlying output stream reports an error).
  168. Otherwise this generator fails and does not generate
  169. any output.]]
  170. ]
  171. [heading Additional Requirements]
  172. The following lists enumerate the requirements which must be met in order to
  173. use a certain type `Num` to instantiate and use a
  174. `uint_generator<Num, Radix>`.
  175. If `boost::is_integral<Num>::value` is `true` the type `Num` must have defined:
  176. * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
  177. * numeric operators for: `+`, `-`, `/`, `*`, and `%`
  178. If `boost::is_integral<Num>::value` is `false` the type `Num` must have defined:
  179. * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
  180. * numeric operators for: `+`, `-`, `/`, `*`, and `%`
  181. * helper functions implementing the interface and the semantics of: `std::fmod`,
  182. `std::pow`, `std::lround`, `std::ltrunc`, `std::floor`, and `std::ceil`.
  183. These need to be defined in a way so that they will be found using argument
  184. dependent lookup (ADL).
  185. [heading Attributes]
  186. [table
  187. [[Expression] [Attribute]]
  188. [[`lit(num)`] [__unused__]]
  189. [[`ushort_`] [`unsigned short`, attribute is mandatory (otherwise
  190. compilation will fail)]]
  191. [[`ushort_(num)`] [`unsigned short`, attribute is optional, if it is
  192. supplied, the generator compares the attribute with
  193. `num` and succeeds only if both are equal, failing
  194. otherwise.]]
  195. [[`uint_`] [`unsigned int`, attribute is mandatory (otherwise
  196. compilation will fail)]]
  197. [[`uint_(num)`] [`unsigned int`, attribute is optional, if it is
  198. supplied, the generator compares the attribute with
  199. `num` and succeeds only if both are equal, failing
  200. otherwise.]]
  201. [[`ulong_`] [`unsigned long`, attribute is mandatory (otherwise
  202. compilation will fail)]]
  203. [[`ulong_(num)`] [`unsigned long`, attribute is optional, if it is
  204. supplied, the generator compares the attribute with
  205. `num` and succeeds only if both are equal, failing
  206. otherwise.]]
  207. [[`ulong_long`] [`unsigned long long`, attribute is mandatory
  208. (otherwise compilation will fail)]]
  209. [[`ulong_long(num)`][`unsigned long long`, attribute is optional, if it is
  210. supplied, the generator compares the attribute with
  211. `num` and succeeds only if both are equal, failing
  212. otherwise.]]
  213. [
  214. [``bin
  215. oct
  216. hex``] [`unsigned int`, attribute is mandatory
  217. (otherwise compilation will fail)]]
  218. [
  219. [``bin(num)
  220. oct(num)
  221. hex(num)``] [`unsigned int`, attribute is optional, if it is
  222. supplied, the generator compares the attribute with
  223. `num` and succeeds only if both are equal, failing
  224. otherwise.]]
  225. [
  226. [``uint_generator<
  227. Num, Radix
  228. >()``] [`Num`, attribute is mandatory (otherwise compilation
  229. will fail)]]
  230. [
  231. [``uint_generator<
  232. Num, Radix
  233. >()(num)``] [`Num`, attribute is optional, if it is supplied, the
  234. generator compares the attribute with `num` and
  235. succeeds only if both are equal, failing otherwise.]]
  236. ]
  237. [note In addition to their usual attribute of type `Num` all listed generators
  238. accept an instance of a `boost::optional<Num>` as well. If the
  239. `boost::optional<>` is initialized (holds a value) the generators behave
  240. as if their attribute was an instance of `Num` and emit the value stored
  241. in the `boost::optional<>`. Otherwise the generators will fail.]
  242. [heading Complexity]
  243. [:O(N), where `N` is the number of digits needed to represent the generated
  244. integer number]
  245. [heading Example]
  246. [note The test harness for the example(s) below is presented in the
  247. __karma_basics_examples__ section.]
  248. Some includes:
  249. [reference_karma_includes]
  250. Some using declarations:
  251. [reference_karma_using_declarations_uint]
  252. Basic usage of an `uint` generator:
  253. [reference_karma_uint]
  254. [endsect]
  255. [/////////////////////////////////////////////////////////////////////////////]
  256. [section:signed_int Signed Integer Number Generators (`int_`, etc.)]
  257. [heading Description]
  258. The `int_generator` can generate signed integers of arbitrary length and size.
  259. This is almost the same as the `uint_generator`. The only difference is the
  260. additional task of generating the `'+'` or `'-'` sign preceding the number.
  261. The class interface is the same as that of the `uint_generator`.
  262. The `int_generator` generator can be used to emit ordinary primitive C/C++
  263. integers or even user defined scalars such as bigints (unlimited
  264. precision integers) if the type follows certain expression
  265. requirements (for more information about the requirements, see
  266. [link spirit.karma.reference.numeric.signed_int.additional_requirements below]).
  267. [heading Header]
  268. // forwards to <boost/spirit/home/karma/numeric/int.hpp>
  269. #include <boost/spirit/include/karma_int.hpp>
  270. Also, see __include_structure__.
  271. [heading Namespace]
  272. [table
  273. [[Name]]
  274. [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
  275. [[`boost::spirit::short_ // alias: boost::spirit::karma::short_`]]
  276. [[`boost::spirit::int_ // alias: boost::spirit::karma::int_`]]
  277. [[`boost::spirit::long_ // alias: boost::spirit::karma::long_`]]
  278. [[`boost::spirit::long_long // alias: boost::spirit::karma::long_long`]]
  279. ]
  280. [note The generators `long_long` and `long_long(num)` are only available on
  281. platforms where the preprocessor constant `BOOST_HAS_LONG_LONG` is
  282. defined (i.e. on platforms having native support for `long long`
  283. (64 bit) integer types).]
  284. [note `lit` is reused by the [karma_string String Generators], the
  285. __karma_char__, and the Numeric Generators. In
  286. general, a char generator is created when you pass in a
  287. character, a string generator is created when you pass in a string, and a
  288. numeric generator is created when you use a numeric literal.]
  289. [heading Synopsis]
  290. template <
  291. typename T
  292. , unsigned Radix
  293. , bool force_sign>
  294. struct int_generator;
  295. [heading Template parameters]
  296. [table
  297. [[Parameter] [Description] [Default]]
  298. [[`T`] [The numeric base type of the
  299. numeric parser.] [`int`]]
  300. [[`Radix`] [The radix base. This can be
  301. either 2: binary, 8: octal,
  302. 10: decimal and 16: hexadecimal.] [`10`]]
  303. [[`force_sign`] [If `true`, all numbers will
  304. have a sign (space for zero)] [`false`]]
  305. ]
  306. [heading Model of]
  307. [:__primitive_generator_concept__]
  308. [variablelist Notation
  309. [[`num`] [Numeric literal, any signed integer value, or
  310. a __karma_lazy_argument__ that evaluates to a signed
  311. integer value of type `Num`]]
  312. [[`Num`] [Type of `num`: any signed integer type]]
  313. [[`Radix`] [A constant integer literal specifying the required
  314. radix for the output conversion. Valid values are `2`,
  315. `8`, `10`, and `16`.]]
  316. [[`force_sign`] [A constant boolean literal specifying whether the
  317. generated number should always have a sign (`'+'` for
  318. positive numbers, `'-'` for negative numbers and a
  319. '` `' for zero).]]
  320. ]
  321. [heading Expression Semantics]
  322. Semantics of an expression is defined only where it differs from, or is
  323. not defined in __primitive_generator_concept__.
  324. [table
  325. [[Expression] [Semantics]]
  326. [[`lit(num)`] [Generate the integer literal `num` using the default
  327. formatting (radix is `10`, sign is only printed for
  328. negative literals). This generator never fails (unless
  329. the underlying output stream reports an error).]]
  330. [
  331. [``short_
  332. int_
  333. long_
  334. long_long``] [Generate the integer provided by a mandatory attribute
  335. using the default formatting (radix is `10`, sign is
  336. only printed for negative literals). This generator
  337. never fails (unless the underlying output stream
  338. reports an error).]]
  339. [
  340. [``short_(num)
  341. int_(num)
  342. long_(num)
  343. long_long(num)``] [Generate the integer provided by the immediate literal
  344. value the generator is initialized from using the
  345. default formatting (radix is `10`, sign is only printed
  346. for negative literals). If this generator has an
  347. associated attribute it succeeds only if the
  348. attribute is equal to the immediate literal (unless
  349. the underlying output stream reports an error).
  350. Otherwise this generator fails and does not generate
  351. any output.]]
  352. ]
  353. All generators listed in the table above (except `lit(num)`) are predefined
  354. specializations of the `int_generator<Num, Radix, force_sign>` basic integer
  355. number generator type described below. It is possible to directly use this
  356. type to create integer generators using a wide range of formatting options.
  357. [table
  358. [[Expression] [Semantics]]
  359. [
  360. [``int_generator<
  361. Num, Radix, force_sign
  362. >()``]
  363. [Generate the integer of type `Num` provided by a
  364. mandatory attribute using the specified `Radix`
  365. (possible values are `2`, `8`, `10`, and `16`, the
  366. default value is `10`). If `force_sign` is `false`
  367. (the default), a sign is only printed for negative
  368. literals. If `force_sign` is `true`, all numbers will
  369. be printed using a sign, i.e. `'-'` for negative
  370. numbers, `'+'` for positive numbers, and `' '` for
  371. zeros. This generator never fails (unless the
  372. underlying output stream reports an error).]]
  373. [
  374. [``int_generator<
  375. Num, Radix, force_sign
  376. >()(num)``]
  377. [Generate the integer of type `Num` provided by the
  378. immediate literal value the generator is initialized
  379. from, using the specified `Radix` (possible values are
  380. `2`, `8`, `10`, and `16`, the default value is `10`).
  381. If `force_sign` is `false` (the default), a sign is
  382. only printed for negative literals. If `force_sign` is
  383. `true`, all numbers will be printed using a sign, i.e.
  384. `'-'` for negative numbers, `'+'` for positive numbers,
  385. and `' '` for zeros. If this generator has an
  386. associated attribute it succeeds only if the
  387. attribute is equal to the immediate literal (unless
  388. the underlying output stream reports an error).
  389. Otherwise this generator fails and does not generate
  390. any output.]]
  391. ]
  392. [heading Additional Requirements]
  393. The following lists enumerate the requirements which must be met in order to
  394. use a certain type `Num` to instantiate and use a
  395. `int_generator<Num, Radix, force_sign>`.
  396. If `boost::is_integral<Num>::value` is `true` the type `Num` must have defined:
  397. * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
  398. * numeric operators for: `+`, `-`, `/`, `*`, `%`, and unary `-`
  399. If `boost::is_integral<Num>::value` is `false` the type `Num` must have defined:
  400. * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
  401. * numeric operators for: `+`, `-`, `/`, `*`, `%`, and unary `-`
  402. * helper functions implementing the interface and the semantics of: `std::fmod`,
  403. `std::fabs`, `std::pow`, `std::lround`, `std::ltrunc`, `std::floor`, and
  404. `std::ceil`. These need to be defined in a way so that they will be found
  405. using argument dependent lookup (ADL).
  406. [heading Attributes]
  407. [table
  408. [[Expression] [Attribute]]
  409. [[`lit(num)`] [__unused__]]
  410. [[`short_`] [`short`, attribute is mandatory (otherwise compilation
  411. will fail)]]
  412. [[`short_(num)`] [`short`, attribute is optional, if it is supplied, the
  413. generator compares the attribute with `num` and
  414. succeeds only if both are equal, failing otherwise.]]
  415. [[`int_`] [`int`, attribute is mandatory (otherwise compilation
  416. will fail)]]
  417. [[`int_(num)`] [`int`, attribute is optional, if it is supplied, the
  418. generator compares the attribute with `num` and
  419. succeeds only if both are equal, failing otherwise.]]
  420. [[`long_`] [`long`, attribute is mandatory (otherwise compilation
  421. will fail)]]
  422. [[`long_(num)`] [`long`, attribute is optional, if it is supplied, the
  423. generator compares the attribute with `num` and
  424. succeeds only if both are equal, failing otherwise.]]
  425. [[`long_long`] [`long long`, attribute is mandatory (otherwise compilation
  426. will fail)]]
  427. [[`long_long(num)`] [`long long`, attribute is optional, if it is supplied, the
  428. generator compares the attribute with `num` and
  429. succeeds only if both are equal, failing otherwise.]]
  430. [
  431. [``int_generator<
  432. Num, Radix, force_sign
  433. >()``] [`Num`, attribute is mandatory (otherwise compilation
  434. will fail)]]
  435. [
  436. [``int_generator<
  437. Num, Radix, force_sign
  438. >()(num)``] [`Num`, attribute is optional, if it is supplied, the
  439. generator compares the attribute with `num` and
  440. succeeds only if both are equal, failing otherwise.]]
  441. ]
  442. [note In addition to their usual attribute of type `Num` all listed generators
  443. accept an instance of a `boost::optional<Num>` as well. If the
  444. `boost::optional<>` is initialized (holds a value) the generators behave
  445. as if their attribute was an instance of `Num` and emit the value stored
  446. in the `boost::optional<>`. Otherwise the generators will fail.]
  447. [heading Complexity]
  448. [:O(N), where `N` is the number of digits needed to represent the generated
  449. integer number]
  450. [heading Example]
  451. [note The test harness for the example(s) below is presented in the
  452. __karma_basics_examples__ section.]
  453. Some includes:
  454. [reference_karma_includes]
  455. Some using declarations:
  456. [reference_karma_using_declarations_int]
  457. Basic usage of an `int_` generator:
  458. [reference_karma_int]
  459. [endsect]
  460. [/////////////////////////////////////////////////////////////////////////////]
  461. [section:real_number Real Number Generators (`float_`, `double_`, etc.)]
  462. [heading Description]
  463. The `real_generator` can generate real numbers of arbitrary length and size
  464. limited by its template parameter, `Num`. The numeric base type `Num` can be
  465. a user defined numeric type such as fixed_point (fixed point reals) and
  466. bignum (unlimited precision numbers) if the type follows certain
  467. expression requirements (for more information about the requirements, see
  468. [link spirit.karma.reference.numeric.real_number.additional_requirements below]).
  469. [heading Header]
  470. // forwards to <boost/spirit/home/karma/numeric/real.hpp>
  471. #include <boost/spirit/include/karma_real.hpp>
  472. Also, see __include_structure__.
  473. [heading Namespace]
  474. [table
  475. [[Name]]
  476. [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
  477. [[`boost::spirit::float_ // alias: boost::spirit::karma::float_`]]
  478. [[`boost::spirit::double_ // alias: boost::spirit::karma::double_`]]
  479. [[`boost::spirit::long_double // alias: boost::spirit::karma::long_double`]]
  480. ]
  481. [note `lit` is reused by the [karma_string String Generators], the
  482. __karma_char__, and the Numeric Generators. In
  483. general, a char generator is created when you pass in a
  484. character, a string generator is created when you pass in a string, and a
  485. numeric generator is created when you use a numeric literal.]
  486. [heading Synopsis]
  487. template <typename Num, typename RealPolicies>
  488. struct real_generator;
  489. [heading Template parameters]
  490. [table
  491. [[Parameter] [Description] [Default]]
  492. [[`Num`] [The type of the real number to generate.] [`double`]]
  493. [[`RealPolicies`] [The policies to use while
  494. converting the real number.] [`real_policies<Num>`]]
  495. ]
  496. For more information about the type `RealPolicies` see
  497. [link spirit.karma.reference.numeric.real_number.real_number_formatting_policies below].
  498. [heading Model of]
  499. [:__primitive_generator_concept__]
  500. [variablelist Notation
  501. [[`num`] [Numeric literal, any real number value, or
  502. a __karma_lazy_argument__ that evaluates to a real
  503. number value of type `Num`]]
  504. [[`Num`] [Type of `num`: any real number type]]
  505. ]
  506. [heading Expression Semantics]
  507. Semantics of an expression is defined only where it differs from, or is
  508. not defined in __primitive_generator_concept__.
  509. [table
  510. [[Expression] [Semantics]]
  511. [[`lit(num)`] [Generate the real number literal `num` using the
  512. default formatting (no trailing zeros, `fixed`
  513. representation for numbers `fabs(n) <= 1e5 && fabs(n) > 1e-3`,
  514. scientific representation otherwise, 3 fractional digits,
  515. sign is only printed for negative literals). This
  516. generator never fails (unless the underlying output
  517. stream reports an error).]]
  518. [
  519. [``float_
  520. double_
  521. long_double``] [Generate the real number provided by a
  522. mandatory attribute using the default formatting (no
  523. trailing zeros, `fixed` representation for numbers
  524. `fabs(n) <= 1e5 && fabs(n) > 1e-3`, scientific
  525. representation otherwise, 3 fractional digits,
  526. sign is only printed for negative literals). This
  527. generator never fails (unless the underlying output
  528. stream reports an error).]]
  529. [
  530. [``float_(num)
  531. double_(num)
  532. long_double(num)``] [Generate the real point number provided by the
  533. immediate literal value the generator is initialized
  534. from using the default formatting (no trailing zeros,
  535. `fixed` representation for numbers
  536. `fabs(n) <= 1e5 && fabs(n) > 1e-3`, scientific
  537. representation otherwise, 3 fractional digits, sign is
  538. only printed for negative literals). If this generator
  539. has an associated attribute it succeeds only if
  540. the attribute is equal to the immediate literal (unless
  541. the underlying output stream reports an error).
  542. Otherwise this generator fails and does not generate
  543. any output.]]
  544. ]
  545. All generators listed in the table above (except `lit(num)`) are predefined
  546. specializations of the `real_generator<Num, RealPolicies>` basic real
  547. number generator type described below. It is possible to directly use this
  548. type to create real number generators using a wide range of formatting
  549. options.
  550. [table
  551. [[Expression] [Semantics]]
  552. [
  553. [``real_generator<
  554. Num, RealPolicies
  555. >()``]
  556. [Generate the real number of type `Num`
  557. provided by a mandatory attribute using the specified
  558. `RealPolicies`. This generator never fails
  559. (unless the underlying output stream reports an
  560. error).]]
  561. [
  562. [``real_generator<
  563. Num, RealPolicies
  564. >()(num)``]
  565. [Generate the real number of type `Num` provided by the
  566. immediate literal value the generator is initialized
  567. from using the specified `RealPolicies`.
  568. If this generator has an associated attribute it
  569. succeeds only if the attribute is equal to the
  570. immediate literal (unless the underlying output stream
  571. reports an error). Otherwise this generator fails and
  572. does not generate any output.]]
  573. ]
  574. [heading Additional Requirements]
  575. The following list enumerates the requirements which must be met in order to
  576. use a certain type `Num` to instantiate a `real_generator<Num, Policies>`.
  577. In order to be usable as the first template parameter for `real_generator<>`
  578. the type `Num` must have defined:
  579. * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
  580. * numeric operators for: `+`, `-`, `/`, `*`, and `%`
  581. * functions implementing the interface and the semantics of: `std::fmod`,
  582. `std::pow`, `std::log10`, `std::lround`, `std::ltrunc`, `std::modf`,
  583. `std::floor`, and `std::ceil`. These need to be defined in a way so that they
  584. will be found using argument dependent lookup (ADL).
  585. * a valid specialization of the type `std::numeric_limits<Num>` allowing for
  586. numeric property inspection.
  587. [heading Attributes]
  588. [table
  589. [[Expression] [Attribute]]
  590. [[`lit(num)`] [__unused__]]
  591. [[`float_`] [`float`, attribute is mandatory (otherwise compilation
  592. will fail)]]
  593. [[`float_(num)`] [`float_`, attribute is optional, if it is supplied, the
  594. generator compares the attribute with `num` and
  595. succeeds only if both are equal, failing otherwise.]]
  596. [[`double_`] [`double`, attribute is mandatory (otherwise compilation
  597. will fail)]]
  598. [[`double_(num)`] [`double`, attribute is optional, if it is supplied, the
  599. generator compares the attribute with `num` and
  600. succeeds only if both are equal, failing otherwise.]]
  601. [[`long_double`] [`long double`, attribute is mandatory (otherwise
  602. compilation will fail)]]
  603. [[`long_double(num)`][`long double`, attribute is optional, if it is supplied,
  604. the generator compares the attribute with `num` and
  605. succeeds only if both are equal, failing otherwise.]]
  606. [
  607. [``real_generator<
  608. Num, Policies
  609. >()``] [`Num`, attribute is mandatory (otherwise compilation
  610. will fail)]]
  611. [
  612. [``real_generator<
  613. Num, Policies
  614. >()(num)``] [`Num`, attribute is optional, if it is supplied, the
  615. generator compares the attribute with `num` and
  616. succeeds only if both are equal, failing otherwise.]]
  617. ]
  618. [note In addition to their usual attribute of type `Num` all listed generators
  619. accept an instance of a `boost::optional<Num>` as well. If the
  620. `boost::optional<>` is initialized (holds a value) the generators behave
  621. as if their attribute was an instance of `Num` and emit the value stored
  622. in the `boost::optional<>`. Otherwise the generators will fail.]
  623. [heading Real Number Formatting Policies]
  624. If special formatting of a real number is needed, overload
  625. the policy class `real_policies<Num>` and use it as a template
  626. parameter to the `real_generator<>` real number generator. For instance:
  627. // define a new real number formatting policy
  628. template <typename Num>
  629. struct scientific_policy : real_policies<Num>
  630. {
  631. // we want the numbers always to be in scientific format
  632. static int floatfield(Num n) { return fmtflags::scientific; }
  633. };
  634. // define a new generator type based on the new policy
  635. typedef real_generator<double, scientific_policy<double> > science_type;
  636. science_type const scientific = science_type();
  637. // use the new generator
  638. generate(sink, science_type(), 1.0); // will output: 1.0e00
  639. generate(sink, scientific, 0.1); // will output: 1.0e-01
  640. The template parameter `Num` should be the type to be formatted using the
  641. overloaded policy type. At the same time `Num` will be used as the attribute
  642. type of the created real number generator.
  643. [heading Real Number Formatting Policy Expression Semantics]
  644. A real number formatting policy should expose the following variables and
  645. functions:
  646. [table
  647. [[Expression][Description]]
  648. [ [``
  649. template <typename Inserter
  650. , typename OutputIterator
  651. , typename Policies>
  652. bool call (OutputIterator& sink, Num n
  653. , Policies const& p);
  654. ``]
  655. [This is the main function used to generate the output for a real
  656. number. It is called by the real generator in order to perform the
  657. conversion. In theory all of the work can be implemented here, but the
  658. easiest way is to use existing functionality provided by the type specified
  659. by the template parameter `Inserter`. The default implementation of this
  660. functions is:
  661. ``
  662. template <typename Inserter, typename OutputIterator
  663. , typename Policies>
  664. static bool
  665. call (OutputIterator& sink, Num n, Policies const& p)
  666. {
  667. return Inserter::call_n(sink, n, p);
  668. }
  669. ``
  670. `sink` is the output iterator to use for generation
  671. `n` is the real number to convert
  672. `p` is the instance of the policy type used to instantiate this real
  673. number generator.
  674. ]]
  675. [ [``
  676. bool force_sign(Num n);
  677. ``]
  678. [The default behavior is to not to require generating a sign. If the function
  679. `force_sign()` returns true, then all generated numbers will have a
  680. sign (`'+'` or `'-'`, zeros will have a space instead of a sign).
  681. `n` is the real number to output. This can be used to
  682. adjust the required behavior depending on the value of this number.]]
  683. [ [``
  684. bool trailing_zeros(Num n);
  685. ``]
  686. [Return whether trailing zero digits have to be emitted in the fractional
  687. part of the output. If set, this flag instructs the real number
  688. generator to emit trailing zeros up to the required precision digits (as
  689. returned by the `precision()` function).
  690. `n` is the real number to output. This can be used to
  691. adjust the required behavior depending on the value of this number.]]
  692. [ [``
  693. int floatfield(Num n);
  694. ``]
  695. [Decide, which representation type to use in the generated output.
  696. By default all numbers having an absolute value of zero or in between
  697. `0.001` and `100000` will be generated using the fixed format, all others
  698. will be generated using the scientific representation.
  699. The `trailing_zeros()` can be used to force the output of trailing zeros
  700. in the fractional part up to the number of digits returned by the
  701. `precision()` member function. The default is not to generate the trailing
  702. zeros.
  703. `n` is the real number to output. This can be used to
  704. adjust the formatting flags depending on the value of
  705. this number.
  706. The return value has to be either `fmtflags::scientific` (generate real
  707. number values in scientific notation) or `fmtflags::fixed` (generate
  708. real number values in fixed-point notation).
  709. ]]
  710. [ [``
  711. unsigned precision(Num n);
  712. ``]
  713. [Return the maximum number of decimal digits to generate in the
  714. fractional part of the output.
  715. `n` is the real number to output. This can be used to
  716. adjust the required precision depending on the value of this number. If
  717. the trailing zeros flag is specified the fractional part of the output will
  718. be 'filled' with zeros, if appropriate.
  719. *Note:* If the trailing_zeros flag is not in effect additional semantics
  720. apply. See the description for the `fraction_part()` function below.
  721. Moreover, this precision will be limited to the value of
  722. `std::numeric_limits<T>::digits10 + 1`.]]
  723. [ [``
  724. template <typename OutputIterator>
  725. bool integer_part(OutputIterator& sink
  726. , Num n, bool sign, bool force_sign);
  727. ``]
  728. [This function is called to generate the integer part of the real
  729. number.
  730. `sink` is the output iterator to use for generation
  731. `n` is the absolute value of the integer part of the real
  732. number to convert (always non-negative)
  733. `sign` is the sign of the overall real number to convert.
  734. `force_sign` is a flag indicating whether a sign has to be generated even for
  735. non-negative numbers (this is the same as has been returned
  736. from the function `force_sign()` described above)
  737. The return value defines the outcome of the whole generator. If it is
  738. `false`, no further output is generated, immediately returning `false` from
  739. the calling `real_generator` as well. If it is `true`, normal output
  740. generation continues.]]
  741. [ [``
  742. template <typename OutputIterator>
  743. bool dot(OutputIterator& sink, Num n,
  744. unsigned precision);
  745. ``]
  746. [This function is called to generate the decimal point.
  747. `sink` is the output iterator to use for generation
  748. `n` is the fractional part of the real number to convert. Note
  749. that this number is scaled such, that it represents the number of units
  750. which correspond to the value returned from the `precision()` function
  751. earlier. I.e. a fractional part of `0.01234` is represented as `1234`
  752. when the function `precision()` returned `5`.
  753. `precision` is the number of digits to emit as returned by the function
  754. `precision()` described above
  755. This is given to allow to decide, whether a decimal point has to be
  756. generated at all.
  757. *Note:* If the `trailing_zeros` flag is not in effect additional comments
  758. apply. See the description for the `fraction_part()` function below.
  759. The return value defines the outcome of the whole generator. If it is
  760. `false`, no further output is generated, immediately returning `false` from
  761. the calling `real_generator` as well. If it is `true`, normal output
  762. generation continues.]]
  763. [ [``
  764. template <typename OutputIterator>
  765. bool fraction_part(OutputIterator& sink, Num n
  766. , unsigned adjprec, unsigned precision);
  767. ``]
  768. [This function is called to generate the fractional part of the number.
  769. `sink` is the output iterator to use for generation
  770. `n` is the fractional part of the real number to convert. Note
  771. that this number is scaled such, that it represents the number of units
  772. which correspond to the value returned from the `precision()` function
  773. earlier. I.e. a fractional part of `0.01234` is represented as `1234`
  774. when the function `precision()` returned `5`.
  775. `adjprec` is the corrected number of digits to emit (see note below)
  776. `precision` is the number of digits to emit as returned by the function
  777. `precision()` described above
  778. *Note:* If `trailing_zeros()` returns `false` the `adjprec`
  779. parameter will have been corrected from the value the `precision()`
  780. function returned earlier (defining the maximal number of fractional
  781. digits) in the sense, that it takes into account trailing zeros. I.e. a
  782. real number `0.0123` and a value of `5` returned from
  783. `precision()` will result in:
  784. `trailing_zeros()` returned `false`: `n` will be `123`, and `adjprec`
  785. will be `4` (as we need to print `0123`)
  786. `trailing_zeros()` returned `true`: `n` will be `1230`, and `adjprec`
  787. will be `5` (as we need to print `01230`)
  788. The missing preceding zeros in the fractional part have to be supplied
  789. by the implementation of this policy function.
  790. The return value defines the outcome of the whole generator. If it is
  791. `false`, no further output is generated, immediately returning `false` from
  792. the calling `real_generator` as well. If it is `true`, normal output
  793. generation continues.]]
  794. [ [``
  795. template <typename CharEncoding,
  796. typename Tag, typename OutputIterator>
  797. bool exponent(
  798. OutputIterator& sink, long n);
  799. ``]
  800. [This function is called to generate the exponential part of the number
  801. (this is called only if the `floatfield()` function returned the
  802. `fmtflags::scientific` flag).
  803. `sink` is the output iterator to use for generation
  804. `n` is the (signed) exponential part of the real number to convert.
  805. The template parameters `CharEncoding` and `Tag` are either of the type
  806. `unused_type` or describe the character class and conversion to be
  807. applied to any output possibly influenced by either the `lower[]` or
  808. `upper[]` directives.
  809. The return value defines the outcome of the whole generator. If it is
  810. `false`, no further output is generated, immediately returning `false` from
  811. the calling `real_generator` as well. If it is `true`, normal output
  812. generation continues.]]
  813. [ [``
  814. template <typename CharEncoding
  815. , typename Tag, typename OutputIterator>
  816. bool nan (OutputIterator& sink, Num n
  817. , bool force_sign);
  818. ``]
  819. [This function is called whenever the number to print is a non-normal
  820. real number of type `NaN`.
  821. `sink` is the output iterator to use for generation
  822. `n` is the (signed) real number to convert
  823. `force_sign` is a flag indicating whether a sign has to be generated even for
  824. non-negative numbers (this is the same as has been returned from
  825. the function `force_sign()` described above)
  826. The template parameters `CharEncoding` and `Tag` are either of the type
  827. `unused_type` or describe the character class and conversion to be
  828. applied to any output possibly influenced by either the `lower[]` or
  829. `upper[]` directives.
  830. The return value defines the outcome of the whole generator. If it is
  831. `false`, no further output is generated, immediately returning `false` from
  832. the calling `real_generator` as well. If it is `true`, normal output
  833. generation continues.]]
  834. [ [``
  835. template <typename CharEncoding
  836. , typename Tag, typename OutputIterator>
  837. bool inf (OutputIterator& sink, Num n
  838. , bool force_sign);
  839. ``]
  840. [This function is called whenever the number to print is a non-normal
  841. real number of type `Inf`.
  842. `sink` is the output iterator to use for generation
  843. `n` is the (signed) real number to convert
  844. `force_sign` is a flag indicating whether a sign has to be generated even for
  845. non-negative numbers (this is the same as has been returned from
  846. the function `force_sign()` described above)
  847. The template parameters `CharEncoding` and `Tag` are either of the type
  848. `unused_type` or describe the character class and conversion to be
  849. applied to any output possibly influenced by either the `lower[]` or
  850. `upper[]` directives.
  851. The return value defines the outcome of the whole generator. If it is
  852. `false`, no further output is generated, immediately returning `false` from
  853. the calling `real_generator` as well. If it is `true`, normal output
  854. generation continues.]]
  855. ]
  856. [tip The easiest way to implement a proper real number formatting policy is
  857. to derive a new type from the type `real_policies<>` while overriding
  858. the aspects of the formatting which need to be changed.]
  859. [heading Complexity]
  860. [:O(N), where `N` is the number of digits needed to represent the generated
  861. real number.]
  862. [heading Example]
  863. [note The test harness for the example(s) below is presented in the
  864. __karma_basics_examples__ section.]
  865. Some includes:
  866. [reference_karma_includes]
  867. Some using declarations:
  868. [reference_karma_using_declarations_real]
  869. Basic usage of an `double_` generator:
  870. [reference_karma_real]
  871. [endsect]
  872. [/////////////////////////////////////////////////////////////////////////////]
  873. [section:boolean Boolean Generators (`bool_`)]
  874. [heading Description]
  875. As you might expect, the `bool_generator` can generate output from boolean
  876. values. The `bool_generator` generator can be used to generate output from
  877. ordinary primitive C/C++ `bool` values or user defined boolean types if
  878. the type follows certain expression requirements (for more information about
  879. the requirements, see
  880. [link spirit.karma.reference.numeric.boolean.additional_requirements below])).
  881. The `bool_generator` is a template class. Template parameters fine tune its
  882. behavior.
  883. [heading Header]
  884. // forwards to <boost/spirit/home/karma/numeric/bool.hpp>
  885. #include <boost/spirit/include/karma_bool.hpp>
  886. Also, see __include_structure__.
  887. [heading Namespace]
  888. [table
  889. [[Name]]
  890. [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
  891. [[`boost::spirit::bool_ // alias: boost::spirit::karma::bool_`]]
  892. [[`boost::spirit::true_ // alias: boost::spirit::karma::true_`]]
  893. [[`boost::spirit::false_ // alias: boost::spirit::karma::false_`]]
  894. ]
  895. [note `lit` is reused by the [karma_string String Generators], the
  896. __karma_char__, and the Numeric Generators. In
  897. general, a char generator is created when you pass in a
  898. character, a string generator is created when you pass in a string, and a
  899. numeric generator is created when you use a numeric (boolean) literal.]
  900. [heading Synopsis]
  901. template <
  902. typename B
  903. , unsigned Policies>
  904. struct bool_generator;
  905. [heading Template parameters]
  906. [table
  907. [[Parameter] [Description] [Default]]
  908. [[`B`] [The boolean base type of the
  909. boolean generator.] [`bool`]]
  910. [[`Policies`] [The policies to use while
  911. converting the boolean.] [`bool_policies<B>`]]
  912. ]
  913. [heading Model of]
  914. [:__primitive_generator_concept__]
  915. [variablelist Notation
  916. [[`b`] [Boolean literal, or a __karma_lazy_argument__ that
  917. evaluates to a boolean value of type `B`]]
  918. [[`B`] [Type of `b`: any type usable as a boolean, or in case
  919. of a __karma_lazy_argument__, its return value]]
  920. ]
  921. [heading Expression Semantics]
  922. Semantics of an expression is defined only where it differs from, or is
  923. not defined in __primitive_generator_concept__.
  924. [table
  925. [[Expression] [Semantics]]
  926. [[`lit(b)`] [Generate the boolean literal `b` using the default
  927. formatting (`false` is generated as `"false"`, and
  928. `true` is generated as `"true"`). This generator never
  929. fails (unless the underlying output stream reports an error).]]
  930. [[`bool_`] [Generate the boolean value provided by a mandatory
  931. attribute using the default formatting (`false` is
  932. generated as `"false"`, and `true` is generated as
  933. `"true"`). This generator never fails (unless the
  934. underlying output stream reports an error).]]
  935. [[`bool_(b)`] [Generate the boolean value provided by the
  936. immediate literal value the generator is initialized
  937. from using the default formatting (`false` is
  938. generated as `"false"`, and `true` is generated as
  939. `"true"`). If this generator has an associated
  940. attribute it succeeds only if the attribute
  941. is equal to the immediate literal (unless the
  942. underlying output stream reports an error). Otherwise
  943. this generator fails and does not generate any output.]]
  944. [[`true_`] [Generate `"true"`. If this generator has an associated
  945. attribute it succeeds only if the attribute
  946. is `true` as well (unless the underlying output stream
  947. reports an error).]]
  948. [[`false_`] [Generate `"false"`. If this generator has an associated
  949. attribute it succeeds only if the attribute
  950. is `false` as well (unless the underlying output stream
  951. reports an error).]]
  952. ]
  953. All generators listed in the table above (except `lit(num)`) are predefined
  954. specializations of the `bool_generator<B, Policies>` basic boolean generator
  955. type described below. It is possible to directly use this type to create
  956. boolean generators using a wide range of formatting options.
  957. [table
  958. [[Expression] [Semantics]]
  959. [
  960. [``bool_generator<
  961. B, Policies
  962. >()``] [Generate the boolean of type `B` provided
  963. by a mandatory attribute using the specified `Policies`
  964. This generator never fails (unless the underlying
  965. output stream reports an error).]]
  966. [
  967. [``bool_generator<
  968. B, Policies
  969. >()(b)``] [Generate the boolean of type `B` provided
  970. by the immediate literal value the generator is
  971. initialized from, using the specified `Policies`. If
  972. this generator has an associated attribute it succeeds
  973. only if the attribute is equal to the
  974. immediate literal (unless the underlying output
  975. stream reports an error). Otherwise this generator
  976. fails and does not generate any output.]]
  977. ]
  978. [note All boolean generators properly respect the [karma_upperlower `upper`]
  979. and [karma_upperlower `lower`] directives.]
  980. [heading Additional Requirements]
  981. The following lists enumerate the requirements which must be met in order to
  982. use a certain type `B` to instantiate and use a `bool_generator<B, Policies>`.
  983. The type `B`:
  984. * must be (safely) convertible to `bool`
  985. [heading Attributes]
  986. [table
  987. [[Expression] [Attribute]]
  988. [[`bool_(b)`] [__unused__]]
  989. [[`bool_`] [`bool`, attribute is mandatory (otherwise
  990. compilation will fail)]]
  991. [[`bool_(b)`] [`bool`, attribute is optional, if it is
  992. supplied, the generator compares the attribute with
  993. `b` and succeeds only if both are equal, failing
  994. otherwise.]]
  995. [
  996. [``bool_generator<
  997. B, Policies
  998. >()``] [`B`, attribute is mandatory (otherwise compilation
  999. will fail)]]
  1000. [
  1001. [``bool_generator<
  1002. B, Policies
  1003. >()(b)``] [`B`, attribute is optional, if it is supplied, the
  1004. generator compares the attribute with `b` and
  1005. succeeds only if both are equal, failing otherwise.]]
  1006. ]
  1007. [note In addition to their usual attribute of type `B` all listed generators
  1008. accept an instance of a `boost::optional<B>` as well. If the
  1009. `boost::optional<>` is initialized (holds a value) the generators behave
  1010. as if their attribute was an instance of `B` and emit the value stored
  1011. in the `boost::optional<>`. Otherwise the generators will fail.]
  1012. [heading Boolean Formatting Policies]
  1013. If special formatting of a boolean is needed, overload
  1014. the policy class `bool_policies<B>` and use it as a template
  1015. parameter to the `bool_generator<>` boolean generator. For instance:
  1016. struct special_bool_policy : karma::bool_policies<>
  1017. {
  1018. template <typename CharEncoding, typename Tag
  1019. , typename OutputIterator>
  1020. static bool generate_false(OutputIterator& sink, bool b)
  1021. {
  1022. // we want to spell the names of false as eurt (true backwards)
  1023. return string_inserter<CharEncoding, Tag>::call(sink, "eurt");
  1024. }
  1025. };
  1026. typedef karma::bool_generator<special_bool_policy> backwards_bool_type;
  1027. backwards_bool_type const backwards_bool;
  1028. karma::generate(sink, backwards_bool, true); // will output: true
  1029. karma::generate(sink, backwards_bool(false)); // will output: uert
  1030. The template parameter `B` should be the type to be formatted using the
  1031. overloaded policy type. At the same time `B` will be used as the attribute
  1032. type of the created real number generator. The default for `B` is `bool`.
  1033. [heading Boolean Formatting Policy Expression Semantics]
  1034. A boolean formatting policy should expose the following:
  1035. [table
  1036. [[Expression][Description]]
  1037. [ [``
  1038. template <typename Inserter
  1039. , typename OutputIterator
  1040. , typename Policies>
  1041. bool call (OutputIterator& sink, Num n
  1042. , Policies const& p);
  1043. ``]
  1044. [This is the main function used to generate the output for a boolean.
  1045. It is called by the boolean generator in order to perform the
  1046. conversion. In theory all of the work can be implemented here, but the
  1047. easiest way is to use existing functionality provided by the type specified
  1048. by the template parameter `Inserter`. The default implementation of this
  1049. functions is:
  1050. ``
  1051. template <typename Inserter, typename OutputIterator
  1052. , typename Policies>
  1053. static bool
  1054. call (OutputIterator& sink, B b, Policies const& p)
  1055. {
  1056. return Inserter::call_n(sink, b, p);
  1057. }
  1058. ``
  1059. `sink` is the output iterator to use for generation
  1060. `b` is the boolean to convert
  1061. `p` is the instance of the policy type used to instantiate this real
  1062. number generator.
  1063. ]]
  1064. [ [``
  1065. template <typename CharEncoding,
  1066. typename Tag, typename OutputIterator>
  1067. bool generate_false(
  1068. OutputIterator& sink, B b);
  1069. ``]
  1070. [This function is called to generate the boolean if it is `false`.
  1071. `sink` is the output iterator to use for generation
  1072. `b` is the boolean to convert (the value is `false`).
  1073. The template parameters `CharEncoding` and `Tag` are either of the type
  1074. `unused_type` or describe the character class and conversion to be
  1075. applied to any output possibly influenced by either the `lower[]` or
  1076. `upper[]` directives.
  1077. The return value defines the outcome of the whole generator. ]]
  1078. [ [``
  1079. template <typename CharEncoding,
  1080. typename Tag, typename OutputIterator>
  1081. bool generate_true(
  1082. OutputIterator& sink, B b);
  1083. ``]
  1084. [This function is called to generate the boolean if it is `true`.
  1085. `sink` is the output iterator to use for generation
  1086. `b` is the boolean to convert (the value is `true`).
  1087. The template parameters `CharEncoding` and `Tag` are either of the type
  1088. `unused_type` or describe the character class and conversion to be
  1089. applied to any output possibly influenced by either the `lower[]` or
  1090. `upper[]` directives.
  1091. The return value defines the outcome of the whole generator. ]]
  1092. ]
  1093. [heading Complexity]
  1094. [:O(N), where `N` is the number of characters needed to represent the generated
  1095. boolean.]
  1096. [heading Example]
  1097. [note The test harness for the example(s) below is presented in the
  1098. __karma_basics_examples__ section.]
  1099. Some includes:
  1100. [reference_karma_includes]
  1101. Some using declarations:
  1102. [reference_karma_using_declarations_bool]
  1103. Basic usage of an `bool_` generator:
  1104. [reference_karma_bool]
  1105. [endsect]
  1106. [endsect]