1 [/==============================================================================
2 Copyright (C) 2001-2011 Hartmut Kaiser
3 Copyright (C) 2001-2011 Joel de Guzman
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ===============================================================================/]
9 [section:numeric Numeric Generators]
11 The library includes a couple of predefined objects for generating booleans,
12 signed and unsigned integers, and real numbers. These generators are fully
13 parametric. Most of the important aspects of numeric generation can be
14 finely adjusted to suit. This includes the radix base, the exponent, the
15 fraction etc. Policies control the real number generators' behavior. There are
16 some predefined policies covering the most common real number formats but the
17 user can supply her own when needed.
19 The numeric parsers are fine tuned (employing loop unrolling and
20 extensive template metaprogramming) with exceptional performance that
21 rivals the low level C functions such as `ltoa`, `ssprintf`, and `_gcvt`.
22 Benchmarks reveal up to 2X speed over the C counterparts (see here:
23 __sec_karma_numeric_performance__). This goes to show that you can write
24 extremely tight generic C++ code that rivals, if not surpasses C.
26 [heading Module Header]
28 // forwards to <boost/spirit/home/karma/numeric.hpp>
29 #include <boost/spirit/include/karma_numeric.hpp>
31 Also, see __include_structure__.
33 [/////////////////////////////////////////////////////////////////////////////]
34 [section:unsigned_int Unsigned Integer Number Generators (`uint_`, etc.)]
38 The `uint_generator` class is the simplest among the members of the
39 numerics package. The `uint_generator` can generate unsigned integers of
40 arbitrary length and size. The `uint_generator` generator can be used to
41 generate ordinary primitive C/C++ integers or even user defined scalars such as
42 bigints (unlimited precision integers) if the type follows
43 certain expression requirements (for more information about the requirements, see
44 [link spirit.karma.reference.numeric.unsigned_int.additional_requirements below])).
45 The `uint_generator` is a template class. Template parameters fine tune its behavior.
49 // forwards to <boost/spirit/home/karma/numeric/uint.hpp>
50 #include <boost/spirit/include/karma_uint.hpp>
52 Also, see __include_structure__.
58 [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
59 [[`boost::spirit::bin // alias: boost::spirit::karma::bin`]]
60 [[`boost::spirit::oct // alias: boost::spirit::karma::oct`]]
61 [[`boost::spirit::hex // alias: boost::spirit::karma::hex`]]
62 [[`boost::spirit::ushort_ // alias: boost::spirit::karma::ushort_`]]
63 [[`boost::spirit::ulong_ // alias: boost::spirit::karma::ulong_`]]
64 [[`boost::spirit::uint_ // alias: boost::spirit::karma::uint_`]]
65 [[`boost::spirit::ulong_long // alias: boost::spirit::karma::ulong_long`]]
68 [note The generators `ulong_long` and `ulong_long(num)` are only available on
69 platforms where the preprocessor constant `BOOST_HAS_LONG_LONG` is
70 defined (i.e. on platforms having native support for `unsigned long long`
71 (64 bit) unsigned integer types).]
73 [note `lit` is reused by the [karma_string String Generators], the
74 __karma_char__, and the Numeric Generators. In
75 general, a char generator is created when you pass in a
76 character, a string generator is created when you pass in a string, and a
77 numeric generator is created when you use a numeric literal.]
84 struct uint_generator;
86 [heading Template parameters]
89 [[Parameter] [Description] [Default]]
90 [[`Num`] [The numeric base type of the
91 numeric generator.] [`unsigned int`]]
92 [[`Radix`] [The radix base. This can be
93 any value in the (inclusive) range from `2` .. `36`.] [`10`]]
98 [:__primitive_generator_concept__]
100 [variablelist Notation
101 [[`num`] [Numeric literal, any unsigned integer value, or
102 a __karma_lazy_argument__ that evaluates to an
103 unsigned integer value of type `Num`]]
104 [[`Num`] [Type of `num`: any unsigned integer type, or in case
105 of a __karma_lazy_argument__, its return value]]
106 [[`Radix`] [An integer literal specifying the required radix for
107 the output conversion. Valid values are from the
108 (inclusive) range `2` .. `36`.]]
111 [heading Expression Semantics]
113 Semantics of an expression is defined only where it differs from, or is
114 not defined in __primitive_generator_concept__.
117 [[Expression] [Semantics]]
118 [[`lit(num)`] [Generate the unsigned integer literal `num` using the
119 default formatting (radix is `10`). This generator never
120 fails (unless the underlying output stream reports
126 ulong_long``] [Generate the unsigned integer provided by a mandatory
127 attribute using the default formatting (radix is `10`).
128 This generator never fails (unless the underlying
129 output stream reports an error).]]
134 ulong_long(num)``] [Generate the unsigned integer provided by the
135 immediate literal value the generator is initialized
136 from using the default formatting (radix is `10`). If
137 this generator has an associated attribute it succeeds
138 only if the attribute is equal to the
139 immediate literal (unless the underlying output
140 stream reports an error). Otherwise this generator
141 fails and does not generate any output.]]
145 hex``] [Generate the unsigned integer provided by a mandatory
146 attribute using the default formatting and the
147 corresponding radix (`bin`: radix is `2`, `oct`: radix is `8`,
148 `hex`: radix is `16`). This generator never fails (unless
149 the underlying output stream reports an error).]]
153 hex(num)``] [Generate the unsigned integer provided by the
154 immediate literal value the generator is initialized
155 from using the default formatting and the
156 corresponding radix (`bin`: radix is `2`, `oct`:
157 radix is `8`, `hex`: radix is `16`). If
158 this generator has an associated attribute it succeeds
159 only if the attribute is equal to the
160 immediate literal (unless the underlying output
161 stream reports an error). Otherwise this generator
162 fails and does not generate any output.]]
165 All generators listed in the table above (except `lit(num)`) are predefined
166 specializations of the `uint_generator<Num, Radix>` basic unsigned integer
167 number generator type described below. It is possible to directly use this
168 type to create unsigned integer generators using a wide range of formatting
172 [[Expression] [Semantics]]
177 [Generate the unsigned integer of type `Num` provided
178 by a mandatory attribute using the specified `Radix`
179 (allowed values are from the (inclusive) range from
181 default value is `10`).This generator never fails
182 (unless the underlying output stream reports an
188 [Generate the unsigned integer of type `Num` provided
189 by the immediate literal value the generator is
190 initialized from, using the specified `Radix`
191 (allowed values are from the (inclusive) range from
193 default value is `10`). If this generator has an
194 associated attribute it succeeds only if the
195 attribute is equal to the immediate literal (unless
196 the underlying output stream reports an error).
197 Otherwise this generator fails and does not generate
201 [heading Additional Requirements]
203 The following lists enumerate the requirements which must be met in order to
204 use a certain type `Num` to instantiate and use a
205 `uint_generator<Num, Radix>`.
207 If `boost::is_integral<Num>::value` is `true` the type `Num` must have defined:
209 * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
210 * numeric operators for: `+`, `-`, `/`, `*`, and `%`
212 If `boost::is_integral<Num>::value` is `false` the type `Num` must have defined:
214 * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
215 * numeric operators for: `+`, `-`, `/`, `*`, and `%`
216 * helper functions implementing the interface and the semantics of: `std::fmod`,
217 `std::pow`, `std::lround`, `std::ltrunc`, `std::floor`, and `std::ceil`.
218 These need to be defined in a way so that they will be found using argument
219 dependent lookup (ADL).
224 [[Expression] [Attribute]]
225 [[`lit(num)`] [__unused__]]
226 [[`ushort_`] [`unsigned short`, attribute is mandatory (otherwise
227 compilation will fail)]]
228 [[`ushort_(num)`] [`unsigned short`, attribute is optional, if it is
229 supplied, the generator compares the attribute with
230 `num` and succeeds only if both are equal, failing
233 [[`uint_`] [`unsigned int`, attribute is mandatory (otherwise
234 compilation will fail)]]
235 [[`uint_(num)`] [`unsigned int`, attribute is optional, if it is
236 supplied, the generator compares the attribute with
237 `num` and succeeds only if both are equal, failing
240 [[`ulong_`] [`unsigned long`, attribute is mandatory (otherwise
241 compilation will fail)]]
242 [[`ulong_(num)`] [`unsigned long`, attribute is optional, if it is
243 supplied, the generator compares the attribute with
244 `num` and succeeds only if both are equal, failing
247 [[`ulong_long`] [`unsigned long long`, attribute is mandatory
248 (otherwise compilation will fail)]]
249 [[`ulong_long(num)`][`unsigned long long`, attribute is optional, if it is
250 supplied, the generator compares the attribute with
251 `num` and succeeds only if both are equal, failing
257 hex``] [`unsigned int`, attribute is mandatory
258 (otherwise compilation will fail)]]
262 hex(num)``] [`unsigned int`, attribute is optional, if it is
263 supplied, the generator compares the attribute with
264 `num` and succeeds only if both are equal, failing
270 >()``] [`Num`, attribute is mandatory (otherwise compilation
275 >()(num)``] [`Num`, attribute is optional, if it is supplied, the
276 generator compares the attribute with `num` and
277 succeeds only if both are equal, failing otherwise.]]
280 [note In addition to their usual attribute of type `Num` all listed generators
281 accept an instance of a `boost::optional<Num>` as well. If the
282 `boost::optional<>` is initialized (holds a value) the generators behave
283 as if their attribute was an instance of `Num` and emit the value stored
284 in the `boost::optional<>`. Otherwise the generators will fail.]
288 [:O(N), where `N` is the number of digits needed to represent the generated
293 [note The test harness for the example(s) below is presented in the
294 __karma_basics_examples__ section.]
298 [reference_karma_includes]
300 Some using declarations:
302 [reference_karma_using_declarations_uint]
304 Basic usage of an `uint` generator:
306 [reference_karma_uint]
310 [/////////////////////////////////////////////////////////////////////////////]
311 [section:signed_int Signed Integer Number Generators (`int_`, etc.)]
313 [heading Description]
315 The `int_generator` can generate signed integers of arbitrary length and size.
316 This is almost the same as the `uint_generator`. The only difference is the
317 additional task of generating the `'+'` or `'-'` sign preceding the number.
318 The class interface is the same as that of the `uint_generator`.
320 The `int_generator` generator can be used to emit ordinary primitive C/C++
321 integers or even user defined scalars such as bigints (unlimited
322 precision integers) if the type follows certain expression
323 requirements (for more information about the requirements, see
324 [link spirit.karma.reference.numeric.signed_int.additional_requirements below]).
328 // forwards to <boost/spirit/home/karma/numeric/int.hpp>
329 #include <boost/spirit/include/karma_int.hpp>
331 Also, see __include_structure__.
337 [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
338 [[`boost::spirit::short_ // alias: boost::spirit::karma::short_`]]
339 [[`boost::spirit::int_ // alias: boost::spirit::karma::int_`]]
340 [[`boost::spirit::long_ // alias: boost::spirit::karma::long_`]]
341 [[`boost::spirit::long_long // alias: boost::spirit::karma::long_long`]]
344 [note The generators `long_long` and `long_long(num)` are only available on
345 platforms where the preprocessor constant `BOOST_HAS_LONG_LONG` is
346 defined (i.e. on platforms having native support for `long long`
347 (64 bit) integer types).]
349 [note `lit` is reused by the [karma_string String Generators], the
350 __karma_char__, and the Numeric Generators. In
351 general, a char generator is created when you pass in a
352 character, a string generator is created when you pass in a string, and a
353 numeric generator is created when you use a numeric literal.]
361 struct int_generator;
363 [heading Template parameters]
366 [[Parameter] [Description] [Default]]
367 [[`T`] [The numeric base type of the
368 numeric parser.] [`int`]]
369 [[`Radix`] [The radix base. This can be
370 either 2: binary, 8: octal,
371 10: decimal and 16: hexadecimal.] [`10`]]
372 [[`force_sign`] [If `true`, all numbers will
373 have a sign (space for zero)] [`false`]]
378 [:__primitive_generator_concept__]
380 [variablelist Notation
381 [[`num`] [Numeric literal, any signed integer value, or
382 a __karma_lazy_argument__ that evaluates to a signed
383 integer value of type `Num`]]
384 [[`Num`] [Type of `num`: any signed integer type]]
385 [[`Radix`] [A constant integer literal specifying the required
386 radix for the output conversion. Valid values are `2`,
387 `8`, `10`, and `16`.]]
388 [[`force_sign`] [A constant boolean literal specifying whether the
389 generated number should always have a sign (`'+'` for
390 positive numbers, `'-'` for negative numbers and a
394 [heading Expression Semantics]
396 Semantics of an expression is defined only where it differs from, or is
397 not defined in __primitive_generator_concept__.
400 [[Expression] [Semantics]]
401 [[`lit(num)`] [Generate the integer literal `num` using the default
402 formatting (radix is `10`, sign is only printed for
403 negative literals). This generator never fails (unless
404 the underlying output stream reports an error).]]
409 long_long``] [Generate the integer provided by a mandatory attribute
410 using the default formatting (radix is `10`, sign is
411 only printed for negative literals). This generator
412 never fails (unless the underlying output stream
418 long_long(num)``] [Generate the integer provided by the immediate literal
419 value the generator is initialized from using the
420 default formatting (radix is `10`, sign is only printed
421 for negative literals). If this generator has an
422 associated attribute it succeeds only if the
423 attribute is equal to the immediate literal (unless
424 the underlying output stream reports an error).
425 Otherwise this generator fails and does not generate
429 All generators listed in the table above (except `lit(num)`) are predefined
430 specializations of the `int_generator<Num, Radix, force_sign>` basic integer
431 number generator type described below. It is possible to directly use this
432 type to create integer generators using a wide range of formatting options.
435 [[Expression] [Semantics]]
438 Num, Radix, force_sign
440 [Generate the integer of type `Num` provided by a
441 mandatory attribute using the specified `Radix`
442 (possible values are `2`, `8`, `10`, and `16`, the
443 default value is `10`). If `force_sign` is `false`
444 (the default), a sign is only printed for negative
445 literals. If `force_sign` is `true`, all numbers will
446 be printed using a sign, i.e. `'-'` for negative
447 numbers, `'+'` for positive numbers, and `' '` for
448 zeros. This generator never fails (unless the
449 underlying output stream reports an error).]]
452 Num, Radix, force_sign
454 [Generate the integer of type `Num` provided by the
455 immediate literal value the generator is initialized
456 from, using the specified `Radix` (possible values are
457 `2`, `8`, `10`, and `16`, the default value is `10`).
458 If `force_sign` is `false` (the default), a sign is
459 only printed for negative literals. If `force_sign` is
460 `true`, all numbers will be printed using a sign, i.e.
461 `'-'` for negative numbers, `'+'` for positive numbers,
462 and `' '` for zeros. If this generator has an
463 associated attribute it succeeds only if the
464 attribute is equal to the immediate literal (unless
465 the underlying output stream reports an error).
466 Otherwise this generator fails and does not generate
470 [heading Additional Requirements]
472 The following lists enumerate the requirements which must be met in order to
473 use a certain type `Num` to instantiate and use a
474 `int_generator<Num, Radix, force_sign>`.
476 If `boost::is_integral<Num>::value` is `true` the type `Num` must have defined:
478 * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
479 * numeric operators for: `+`, `-`, `/`, `*`, `%`, and unary `-`
481 If `boost::is_integral<Num>::value` is `false` the type `Num` must have defined:
483 * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
484 * numeric operators for: `+`, `-`, `/`, `*`, `%`, and unary `-`
485 * helper functions implementing the interface and the semantics of: `std::fmod`,
486 `std::fabs`, `std::pow`, `std::lround`, `std::ltrunc`, `std::floor`, and
487 `std::ceil`. These need to be defined in a way so that they will be found
488 using argument dependent lookup (ADL).
493 [[Expression] [Attribute]]
494 [[`lit(num)`] [__unused__]]
495 [[`short_`] [`short`, attribute is mandatory (otherwise compilation
497 [[`short_(num)`] [`short`, attribute is optional, if it is supplied, the
498 generator compares the attribute with `num` and
499 succeeds only if both are equal, failing otherwise.]]
501 [[`int_`] [`int`, attribute is mandatory (otherwise compilation
503 [[`int_(num)`] [`int`, attribute is optional, if it is supplied, the
504 generator compares the attribute with `num` and
505 succeeds only if both are equal, failing otherwise.]]
507 [[`long_`] [`long`, attribute is mandatory (otherwise compilation
509 [[`long_(num)`] [`long`, attribute is optional, if it is supplied, the
510 generator compares the attribute with `num` and
511 succeeds only if both are equal, failing otherwise.]]
513 [[`long_long`] [`long long`, attribute is mandatory (otherwise compilation
515 [[`long_long(num)`] [`long long`, attribute is optional, if it is supplied, the
516 generator compares the attribute with `num` and
517 succeeds only if both are equal, failing otherwise.]]
521 Num, Radix, force_sign
522 >()``] [`Num`, attribute is mandatory (otherwise compilation
526 Num, Radix, force_sign
527 >()(num)``] [`Num`, attribute is optional, if it is supplied, the
528 generator compares the attribute with `num` and
529 succeeds only if both are equal, failing otherwise.]]
532 [note In addition to their usual attribute of type `Num` all listed generators
533 accept an instance of a `boost::optional<Num>` as well. If the
534 `boost::optional<>` is initialized (holds a value) the generators behave
535 as if their attribute was an instance of `Num` and emit the value stored
536 in the `boost::optional<>`. Otherwise the generators will fail.]
540 [:O(N), where `N` is the number of digits needed to represent the generated
545 [note The test harness for the example(s) below is presented in the
546 __karma_basics_examples__ section.]
550 [reference_karma_includes]
552 Some using declarations:
554 [reference_karma_using_declarations_int]
556 Basic usage of an `int_` generator:
558 [reference_karma_int]
562 [/////////////////////////////////////////////////////////////////////////////]
563 [section:real_number Real Number Generators (`float_`, `double_`, etc.)]
565 [heading Description]
567 The `real_generator` can generate real numbers of arbitrary length and size
568 limited by its template parameter, `Num`. The numeric base type `Num` can be
569 a user defined numeric type such as fixed_point (fixed point reals) and
570 bignum (unlimited precision numbers) if the type follows certain
571 expression requirements (for more information about the requirements, see
572 [link spirit.karma.reference.numeric.real_number.additional_requirements below]).
576 // forwards to <boost/spirit/home/karma/numeric/real.hpp>
577 #include <boost/spirit/include/karma_real.hpp>
579 Also, see __include_structure__.
585 [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
586 [[`boost::spirit::float_ // alias: boost::spirit::karma::float_`]]
587 [[`boost::spirit::double_ // alias: boost::spirit::karma::double_`]]
588 [[`boost::spirit::long_double // alias: boost::spirit::karma::long_double`]]
591 [note `lit` is reused by the [karma_string String Generators], the
592 __karma_char__, and the Numeric Generators. In
593 general, a char generator is created when you pass in a
594 character, a string generator is created when you pass in a string, and a
595 numeric generator is created when you use a numeric literal.]
599 template <typename Num, typename RealPolicies>
600 struct real_generator;
602 [heading Template parameters]
605 [[Parameter] [Description] [Default]]
606 [[`Num`] [The type of the real number to generate.] [`double`]]
607 [[`RealPolicies`] [The policies to use while
608 converting the real number.] [`real_policies<Num>`]]
611 For more information about the type `RealPolicies` see
612 [link spirit.karma.reference.numeric.real_number.real_number_formatting_policies below].
616 [:__primitive_generator_concept__]
618 [variablelist Notation
619 [[`num`] [Numeric literal, any real number value, or
620 a __karma_lazy_argument__ that evaluates to a real
621 number value of type `Num`]]
622 [[`Num`] [Type of `num`: any real number type]]
625 [heading Expression Semantics]
627 Semantics of an expression is defined only where it differs from, or is
628 not defined in __primitive_generator_concept__.
631 [[Expression] [Semantics]]
632 [[`lit(num)`] [Generate the real number literal `num` using the
633 default formatting (no trailing zeros, `fixed`
634 representation for numbers `fabs(n) <= 1e5 && fabs(n) > 1e-3`,
635 scientific representation otherwise, 3 fractional digits,
636 sign is only printed for negative literals). This
637 generator never fails (unless the underlying output
638 stream reports an error).]]
642 long_double``] [Generate the real number provided by a
643 mandatory attribute using the default formatting (no
644 trailing zeros, `fixed` representation for numbers
645 `fabs(n) <= 1e5 && fabs(n) > 1e-3`, scientific
646 representation otherwise, 3 fractional digits,
647 sign is only printed for negative literals). This
648 generator never fails (unless the underlying output
649 stream reports an error).]]
653 long_double(num)``] [Generate the real point number provided by the
654 immediate literal value the generator is initialized
655 from using the default formatting (no trailing zeros,
656 `fixed` representation for numbers
657 `fabs(n) <= 1e5 && fabs(n) > 1e-3`, scientific
658 representation otherwise, 3 fractional digits, sign is
659 only printed for negative literals). If this generator
660 has an associated attribute it succeeds only if
661 the attribute is equal to the immediate literal (unless
662 the underlying output stream reports an error).
663 Otherwise this generator fails and does not generate
667 All generators listed in the table above (except `lit(num)`) are predefined
668 specializations of the `real_generator<Num, RealPolicies>` basic real
669 number generator type described below. It is possible to directly use this
670 type to create real number generators using a wide range of formatting
674 [[Expression] [Semantics]]
679 [Generate the real number of type `Num`
680 provided by a mandatory attribute using the specified
681 `RealPolicies`. This generator never fails
682 (unless the underlying output stream reports an
688 [Generate the real number of type `Num` provided by the
689 immediate literal value the generator is initialized
690 from using the specified `RealPolicies`.
691 If this generator has an associated attribute it
692 succeeds only if the attribute is equal to the
693 immediate literal (unless the underlying output stream
694 reports an error). Otherwise this generator fails and
695 does not generate any output.]]
698 [heading Additional Requirements]
700 The following list enumerates the requirements which must be met in order to
701 use a certain type `Num` to instantiate a `real_generator<Num, Policies>`.
703 In order to be usable as the first template parameter for `real_generator<>`
704 the type `Num` must have defined:
706 * comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
707 * numeric operators for: `+`, `-`, `/`, `*`, and `%`
708 * functions implementing the interface and the semantics of: `std::fmod`,
709 `std::pow`, `std::log10`, `std::lround`, `std::ltrunc`, `std::modf`,
710 `std::floor`, and `std::ceil`. These need to be defined in a way so that they
711 will be found using argument dependent lookup (ADL).
712 * a valid specialization of the type `std::numeric_limits<Num>` allowing for
713 numeric property inspection.
719 [[Expression] [Attribute]]
720 [[`lit(num)`] [__unused__]]
722 [[`float_`] [`float`, attribute is mandatory (otherwise compilation
724 [[`float_(num)`] [`float_`, attribute is optional, if it is supplied, the
725 generator compares the attribute with `num` and
726 succeeds only if both are equal, failing otherwise.]]
728 [[`double_`] [`double`, attribute is mandatory (otherwise compilation
730 [[`double_(num)`] [`double`, attribute is optional, if it is supplied, the
731 generator compares the attribute with `num` and
732 succeeds only if both are equal, failing otherwise.]]
734 [[`long_double`] [`long double`, attribute is mandatory (otherwise
735 compilation will fail)]]
736 [[`long_double(num)`][`long double`, attribute is optional, if it is supplied,
737 the generator compares the attribute with `num` and
738 succeeds only if both are equal, failing otherwise.]]
742 >()``] [`Num`, attribute is mandatory (otherwise compilation
747 >()(num)``] [`Num`, attribute is optional, if it is supplied, the
748 generator compares the attribute with `num` and
749 succeeds only if both are equal, failing otherwise.]]
752 [note In addition to their usual attribute of type `Num` all listed generators
753 accept an instance of a `boost::optional<Num>` as well. If the
754 `boost::optional<>` is initialized (holds a value) the generators behave
755 as if their attribute was an instance of `Num` and emit the value stored
756 in the `boost::optional<>`. Otherwise the generators will fail.]
758 [heading Real Number Formatting Policies]
760 If special formatting of a real number is needed, overload
761 the policy class `real_policies<Num>` and use it as a template
762 parameter to the `real_generator<>` real number generator. For instance:
764 // define a new real number formatting policy
765 template <typename Num>
766 struct scientific_policy : real_policies<Num>
768 // we want the numbers always to be in scientific format
769 static int floatfield(Num n) { return fmtflags::scientific; }
772 // define a new generator type based on the new policy
773 typedef real_generator<double, scientific_policy<double> > science_type;
774 science_type const scientific = science_type();
776 // use the new generator
777 generate(sink, science_type(), 1.0); // will output: 1.0e00
778 generate(sink, scientific, 0.1); // will output: 1.0e-01
780 The template parameter `Num` should be the type to be formatted using the
781 overloaded policy type. At the same time `Num` will be used as the attribute
782 type of the created real number generator.
785 [heading Real Number Formatting Policy Expression Semantics]
787 A real number formatting policy should expose the following variables and
791 [[Expression][Description]]
793 template <typename Inserter
794 , typename OutputIterator
796 bool call (OutputIterator& sink, Num n
797 , Policies const& p);
799 [This is the main function used to generate the output for a real
800 number. It is called by the real generator in order to perform the
801 conversion. In theory all of the work can be implemented here, but the
802 easiest way is to use existing functionality provided by the type specified
803 by the template parameter `Inserter`. The default implementation of this
806 template <typename Inserter, typename OutputIterator
809 call (OutputIterator& sink, Num n, Policies const& p)
811 return Inserter::call_n(sink, n, p);
814 `sink` is the output iterator to use for generation
816 `n` is the real number to convert
818 `p` is the instance of the policy type used to instantiate this real
822 bool force_sign(Num n);
824 [The default behavior is to not to require generating a sign. If the function
825 `force_sign()` returns true, then all generated numbers will have a
826 sign (`'+'` or `'-'`, zeros will have a space instead of a sign).
828 `n` is the real number to output. This can be used to
829 adjust the required behavior depending on the value of this number.]]
831 bool trailing_zeros(Num n);
833 [Return whether trailing zero digits have to be emitted in the fractional
834 part of the output. If set, this flag instructs the real number
835 generator to emit trailing zeros up to the required precision digits (as
836 returned by the `precision()` function).
838 `n` is the real number to output. This can be used to
839 adjust the required behavior depending on the value of this number.]]
841 int floatfield(Num n);
843 [Decide, which representation type to use in the generated output.
845 By default all numbers having an absolute value of zero or in between
846 `0.001` and `100000` will be generated using the fixed format, all others
847 will be generated using the scientific representation.
849 The `trailing_zeros()` can be used to force the output of trailing zeros
850 in the fractional part up to the number of digits returned by the
851 `precision()` member function. The default is not to generate the trailing
854 `n` is the real number to output. This can be used to
855 adjust the formatting flags depending on the value of
858 The return value has to be either `fmtflags::scientific` (generate real
859 number values in scientific notation) or `fmtflags::fixed` (generate
860 real number values in fixed-point notation).
863 unsigned precision(Num n);
865 [Return the maximum number of decimal digits to generate in the
866 fractional part of the output.
868 `n` is the real number to output. This can be used to
869 adjust the required precision depending on the value of this number. If
870 the trailing zeros flag is specified the fractional part of the output will
871 be 'filled' with zeros, if appropriate.
873 *Note:* If the trailing_zeros flag is not in effect additional semantics
874 apply. See the description for the `fraction_part()` function below.
875 Moreover, this precision will be limited to the value of
876 `std::numeric_limits<T>::digits10 + 1`.]]
878 template <typename OutputIterator>
879 bool integer_part(OutputIterator& sink
880 , Num n, bool sign, bool force_sign);
882 [This function is called to generate the integer part of the real
885 `sink` is the output iterator to use for generation
887 `n` is the absolute value of the integer part of the real
888 number to convert (always non-negative)
890 `sign` is the sign of the overall real number to convert.
892 `force_sign` is a flag indicating whether a sign has to be generated even for
893 non-negative numbers (this is the same as has been returned
894 from the function `force_sign()` described above)
896 The return value defines the outcome of the whole generator. If it is
897 `false`, no further output is generated, immediately returning `false` from
898 the calling `real_generator` as well. If it is `true`, normal output
899 generation continues.]]
901 template <typename OutputIterator>
902 bool dot(OutputIterator& sink, Num n,
905 [This function is called to generate the decimal point.
907 `sink` is the output iterator to use for generation
909 `n` is the fractional part of the real number to convert. Note
910 that this number is scaled such, that it represents the number of units
911 which correspond to the value returned from the `precision()` function
912 earlier. I.e. a fractional part of `0.01234` is represented as `1234`
913 when the function `precision()` returned `5`.
915 `precision` is the number of digits to emit as returned by the function
916 `precision()` described above
918 This is given to allow to decide, whether a decimal point has to be
921 *Note:* If the `trailing_zeros` flag is not in effect additional comments
922 apply. See the description for the `fraction_part()` function below.
924 The return value defines the outcome of the whole generator. If it is
925 `false`, no further output is generated, immediately returning `false` from
926 the calling `real_generator` as well. If it is `true`, normal output
927 generation continues.]]
929 template <typename OutputIterator>
930 bool fraction_part(OutputIterator& sink, Num n
931 , unsigned adjprec, unsigned precision);
933 [This function is called to generate the fractional part of the number.
935 `sink` is the output iterator to use for generation
937 `n` is the fractional part of the real number to convert. Note
938 that this number is scaled such, that it represents the number of units
939 which correspond to the value returned from the `precision()` function
940 earlier. I.e. a fractional part of `0.01234` is represented as `1234`
941 when the function `precision()` returned `5`.
943 `adjprec` is the corrected number of digits to emit (see note below)
945 `precision` is the number of digits to emit as returned by the function
946 `precision()` described above
948 *Note:* If `trailing_zeros()` returns `false` the `adjprec`
949 parameter will have been corrected from the value the `precision()`
950 function returned earlier (defining the maximal number of fractional
951 digits) in the sense, that it takes into account trailing zeros. I.e. a
952 real number `0.0123` and a value of `5` returned from
953 `precision()` will result in:
955 `trailing_zeros()` returned `false`: `n` will be `123`, and `adjprec`
956 will be `4` (as we need to print `0123`)
958 `trailing_zeros()` returned `true`: `n` will be `1230`, and `adjprec`
959 will be `5` (as we need to print `01230`)
961 The missing preceding zeros in the fractional part have to be supplied
962 by the implementation of this policy function.
964 The return value defines the outcome of the whole generator. If it is
965 `false`, no further output is generated, immediately returning `false` from
966 the calling `real_generator` as well. If it is `true`, normal output
967 generation continues.]]
969 template <typename CharEncoding,
970 typename Tag, typename OutputIterator>
972 OutputIterator& sink, long n);
974 [This function is called to generate the exponential part of the number
975 (this is called only if the `floatfield()` function returned the
976 `fmtflags::scientific` flag).
978 `sink` is the output iterator to use for generation
980 `n` is the (signed) exponential part of the real number to convert.
982 The template parameters `CharEncoding` and `Tag` are either of the type
983 `unused_type` or describe the character class and conversion to be
984 applied to any output possibly influenced by either the `lower[]` or
985 `upper[]` directives.
987 The return value defines the outcome of the whole generator. If it is
988 `false`, no further output is generated, immediately returning `false` from
989 the calling `real_generator` as well. If it is `true`, normal output
990 generation continues.]]
992 template <typename CharEncoding
993 , typename Tag, typename OutputIterator>
994 bool nan (OutputIterator& sink, Num n
997 [This function is called whenever the number to print is a non-normal
998 real number of type `NaN`.
1000 `sink` is the output iterator to use for generation
1002 `n` is the (signed) real number to convert
1004 `force_sign` is a flag indicating whether a sign has to be generated even for
1005 non-negative numbers (this is the same as has been returned from
1006 the function `force_sign()` described above)
1008 The template parameters `CharEncoding` and `Tag` are either of the type
1009 `unused_type` or describe the character class and conversion to be
1010 applied to any output possibly influenced by either the `lower[]` or
1011 `upper[]` directives.
1013 The return value defines the outcome of the whole generator. If it is
1014 `false`, no further output is generated, immediately returning `false` from
1015 the calling `real_generator` as well. If it is `true`, normal output
1016 generation continues.]]
1018 template <typename CharEncoding
1019 , typename Tag, typename OutputIterator>
1020 bool inf (OutputIterator& sink, Num n
1023 [This function is called whenever the number to print is a non-normal
1024 real number of type `Inf`.
1026 `sink` is the output iterator to use for generation
1028 `n` is the (signed) real number to convert
1030 `force_sign` is a flag indicating whether a sign has to be generated even for
1031 non-negative numbers (this is the same as has been returned from
1032 the function `force_sign()` described above)
1034 The template parameters `CharEncoding` and `Tag` are either of the type
1035 `unused_type` or describe the character class and conversion to be
1036 applied to any output possibly influenced by either the `lower[]` or
1037 `upper[]` directives.
1039 The return value defines the outcome of the whole generator. If it is
1040 `false`, no further output is generated, immediately returning `false` from
1041 the calling `real_generator` as well. If it is `true`, normal output
1042 generation continues.]]
1045 [tip The easiest way to implement a proper real number formatting policy is
1046 to derive a new type from the type `real_policies<>` while overriding
1047 the aspects of the formatting which need to be changed.]
1050 [heading Complexity]
1052 [:O(N), where `N` is the number of digits needed to represent the generated
1057 [note The test harness for the example(s) below is presented in the
1058 __karma_basics_examples__ section.]
1062 [reference_karma_includes]
1064 Some using declarations:
1066 [reference_karma_using_declarations_real]
1068 Basic usage of an `double_` generator:
1070 [reference_karma_real]
1074 [/////////////////////////////////////////////////////////////////////////////]
1075 [section:boolean Boolean Generators (`bool_`)]
1077 [heading Description]
1079 As you might expect, the `bool_generator` can generate output from boolean
1080 values. The `bool_generator` generator can be used to generate output from
1081 ordinary primitive C/C++ `bool` values or user defined boolean types if
1082 the type follows certain expression requirements (for more information about
1083 the requirements, see
1084 [link spirit.karma.reference.numeric.boolean.additional_requirements below])).
1085 The `bool_generator` is a template class. Template parameters fine tune its
1090 // forwards to <boost/spirit/home/karma/numeric/bool.hpp>
1091 #include <boost/spirit/include/karma_bool.hpp>
1093 Also, see __include_structure__.
1099 [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
1100 [[`boost::spirit::bool_ // alias: boost::spirit::karma::bool_`]]
1101 [[`boost::spirit::true_ // alias: boost::spirit::karma::true_`]]
1102 [[`boost::spirit::false_ // alias: boost::spirit::karma::false_`]]
1105 [note `lit` is reused by the [karma_string String Generators], the
1106 __karma_char__, and the Numeric Generators. In
1107 general, a char generator is created when you pass in a
1108 character, a string generator is created when you pass in a string, and a
1109 numeric generator is created when you use a numeric (boolean) literal.]
1115 , unsigned Policies>
1116 struct bool_generator;
1118 [heading Template parameters]
1121 [[Parameter] [Description] [Default]]
1122 [[`B`] [The boolean base type of the
1123 boolean generator.] [`bool`]]
1124 [[`Policies`] [The policies to use while
1125 converting the boolean.] [`bool_policies<B>`]]
1130 [:__primitive_generator_concept__]
1132 [variablelist Notation
1133 [[`b`] [Boolean literal, or a __karma_lazy_argument__ that
1134 evaluates to a boolean value of type `B`]]
1135 [[`B`] [Type of `b`: any type usable as a boolean, or in case
1136 of a __karma_lazy_argument__, its return value]]
1139 [heading Expression Semantics]
1141 Semantics of an expression is defined only where it differs from, or is
1142 not defined in __primitive_generator_concept__.
1145 [[Expression] [Semantics]]
1146 [[`lit(b)`] [Generate the boolean literal `b` using the default
1147 formatting (`false` is generated as `"false"`, and
1148 `true` is generated as `"true"`). This generator never
1149 fails (unless the underlying output stream reports an error).]]
1150 [[`bool_`] [Generate the boolean value provided by a mandatory
1151 attribute using the default formatting (`false` is
1152 generated as `"false"`, and `true` is generated as
1153 `"true"`). This generator never fails (unless the
1154 underlying output stream reports an error).]]
1155 [[`bool_(b)`] [Generate the boolean value provided by the
1156 immediate literal value the generator is initialized
1157 from using the default formatting (`false` is
1158 generated as `"false"`, and `true` is generated as
1159 `"true"`). If this generator has an associated
1160 attribute it succeeds only if the attribute
1161 is equal to the immediate literal (unless the
1162 underlying output stream reports an error). Otherwise
1163 this generator fails and does not generate any output.]]
1164 [[`true_`] [Generate `"true"`. If this generator has an associated
1165 attribute it succeeds only if the attribute
1166 is `true` as well (unless the underlying output stream
1167 reports an error).]]
1168 [[`false_`] [Generate `"false"`. If this generator has an associated
1169 attribute it succeeds only if the attribute
1170 is `false` as well (unless the underlying output stream
1171 reports an error).]]
1174 All generators listed in the table above (except `lit(num)`) are predefined
1175 specializations of the `bool_generator<B, Policies>` basic boolean generator
1176 type described below. It is possible to directly use this type to create
1177 boolean generators using a wide range of formatting options.
1180 [[Expression] [Semantics]]
1184 >()``] [Generate the boolean of type `B` provided
1185 by a mandatory attribute using the specified `Policies`
1186 This generator never fails (unless the underlying
1187 output stream reports an error).]]
1191 >()(b)``] [Generate the boolean of type `B` provided
1192 by the immediate literal value the generator is
1193 initialized from, using the specified `Policies`. If
1194 this generator has an associated attribute it succeeds
1195 only if the attribute is equal to the
1196 immediate literal (unless the underlying output
1197 stream reports an error). Otherwise this generator
1198 fails and does not generate any output.]]
1201 [note All boolean generators properly respect the [karma_upperlower `upper`]
1202 and [karma_upperlower `lower`] directives.]
1204 [heading Additional Requirements]
1206 The following lists enumerate the requirements which must be met in order to
1207 use a certain type `B` to instantiate and use a `bool_generator<B, Policies>`.
1211 * must be (safely) convertible to `bool`
1213 [heading Attributes]
1216 [[Expression] [Attribute]]
1217 [[`bool_(b)`] [__unused__]]
1218 [[`bool_`] [`bool`, attribute is mandatory (otherwise
1219 compilation will fail)]]
1220 [[`bool_(b)`] [`bool`, attribute is optional, if it is
1221 supplied, the generator compares the attribute with
1222 `b` and succeeds only if both are equal, failing
1228 >()``] [`B`, attribute is mandatory (otherwise compilation
1233 >()(b)``] [`B`, attribute is optional, if it is supplied, the
1234 generator compares the attribute with `b` and
1235 succeeds only if both are equal, failing otherwise.]]
1238 [note In addition to their usual attribute of type `B` all listed generators
1239 accept an instance of a `boost::optional<B>` as well. If the
1240 `boost::optional<>` is initialized (holds a value) the generators behave
1241 as if their attribute was an instance of `B` and emit the value stored
1242 in the `boost::optional<>`. Otherwise the generators will fail.]
1244 [heading Boolean Formatting Policies]
1246 If special formatting of a boolean is needed, overload
1247 the policy class `bool_policies<B>` and use it as a template
1248 parameter to the `bool_generator<>` boolean generator. For instance:
1250 struct special_bool_policy : karma::bool_policies<>
1252 template <typename CharEncoding, typename Tag
1253 , typename OutputIterator>
1254 static bool generate_false(OutputIterator& sink, bool b)
1256 // we want to spell the names of false as eurt (true backwards)
1257 return string_inserter<CharEncoding, Tag>::call(sink, "eurt");
1261 typedef karma::bool_generator<special_bool_policy> backwards_bool_type;
1262 backwards_bool_type const backwards_bool;
1264 karma::generate(sink, backwards_bool, true); // will output: true
1265 karma::generate(sink, backwards_bool(false)); // will output: uert
1267 The template parameter `B` should be the type to be formatted using the
1268 overloaded policy type. At the same time `B` will be used as the attribute
1269 type of the created real number generator. The default for `B` is `bool`.
1272 [heading Boolean Formatting Policy Expression Semantics]
1274 A boolean formatting policy should expose the following:
1277 [[Expression][Description]]
1279 template <typename Inserter
1280 , typename OutputIterator
1281 , typename Policies>
1282 bool call (OutputIterator& sink, Num n
1283 , Policies const& p);
1285 [This is the main function used to generate the output for a boolean.
1286 It is called by the boolean generator in order to perform the
1287 conversion. In theory all of the work can be implemented here, but the
1288 easiest way is to use existing functionality provided by the type specified
1289 by the template parameter `Inserter`. The default implementation of this
1292 template <typename Inserter, typename OutputIterator
1293 , typename Policies>
1295 call (OutputIterator& sink, B b, Policies const& p)
1297 return Inserter::call_n(sink, b, p);
1300 `sink` is the output iterator to use for generation
1302 `b` is the boolean to convert
1304 `p` is the instance of the policy type used to instantiate this real
1308 template <typename CharEncoding,
1309 typename Tag, typename OutputIterator>
1310 bool generate_false(
1311 OutputIterator& sink, B b);
1313 [This function is called to generate the boolean if it is `false`.
1315 `sink` is the output iterator to use for generation
1317 `b` is the boolean to convert (the value is `false`).
1319 The template parameters `CharEncoding` and `Tag` are either of the type
1320 `unused_type` or describe the character class and conversion to be
1321 applied to any output possibly influenced by either the `lower[]` or
1322 `upper[]` directives.
1324 The return value defines the outcome of the whole generator. ]]
1326 template <typename CharEncoding,
1327 typename Tag, typename OutputIterator>
1329 OutputIterator& sink, B b);
1331 [This function is called to generate the boolean if it is `true`.
1333 `sink` is the output iterator to use for generation
1335 `b` is the boolean to convert (the value is `true`).
1337 The template parameters `CharEncoding` and `Tag` are either of the type
1338 `unused_type` or describe the character class and conversion to be
1339 applied to any output possibly influenced by either the `lower[]` or
1340 `upper[]` directives.
1342 The return value defines the outcome of the whole generator. ]]
1345 [heading Complexity]
1347 [:O(N), where `N` is the number of characters needed to represent the generated
1352 [note The test harness for the example(s) below is presented in the
1353 __karma_basics_examples__ section.]
1357 [reference_karma_includes]
1359 Some using declarations:
1361 [reference_karma_using_declarations_bool]
1363 Basic usage of an `bool_` generator:
1365 [reference_karma_bool]