Unsigned Integer Number Generators (uint_, etc.)

| Home | Libraries | People | FAQ | More

##### Unsigned Integer Number Generators (`uint_`, etc.)

###### Description

The `uint_generator` class is the simplest among the members of the numerics package. The `uint_generator` can generate unsigned integers of arbitrary length and size. The `uint_generator` generator can be used to generate ordinary primitive C/C++ integers or even user defined scalars such as bigints (unlimited precision integers) if the type follows certain expression requirements (for more information about the requirements, see below)). The `uint_generator` is a template class. Template parameters fine tune its behavior.

###### Header

    // forwards to <boost/spirit/home/karma/numeric/uint.hpp>
    #include <boost/spirit/include/karma_uint.hpp>

Also, see Include Structure.

###### Namespace

Name
`boost::spirit::lit // alias: boost::spirit::karma::lit`
`boost::spirit::bin // alias: boost::spirit::karma::bin`
`boost::spirit::oct // alias: boost::spirit::karma::oct`
`boost::spirit::hex // alias: boost::spirit::karma::hex`
`boost::spirit::ushort_ // alias: boost::spirit::karma::ushort_`
`boost::spirit::ulong_ // alias: boost::spirit::karma::ulong_`
`boost::spirit::uint_ // alias: boost::spirit::karma::uint_`
`boost::spirit::ulong_long // alias: boost::spirit::karma::ulong_long`

| Note
The generators `ulong_long` and `ulong_long(num)` are only available on platforms where the preprocessor constant `BOOST_HAS_LONG_LONG` is defined (i.e. on platforms having native support for `unsigned long long` (64 bit) unsigned integer types).

| Note
`lit` is reused by the String Generators, the Character Generators, and the Numeric Generators. In general, a char generator is created when you pass in a character, a string generator is created when you pass in a string, and a numeric generator is created when you use a numeric literal.

###### Synopsis

    template <
        typename Num
      , unsigned Radix>
    struct uint_generator;

###### Template parameters

Parameter | Description | Default
--- | --- | ---
`Num` | The numeric base type of the numeric generator. | `unsigned int`
`Radix` | The radix base. This can be any value in the (inclusive) range from `2` .. `36`. | `10`

###### Model of

> `PrimitiveGenerator`

Notation

`num`

  Numeric literal, any unsigned integer value, or a Lazy Argument that evaluates to an unsigned integer value of type `Num`

`Num`

  Type of `num`: any unsigned integer type, or in case of a Lazy Argument, its return value

`Radix`

  An integer literal specifying the required radix for the output conversion. Valid values are from the (inclusive) range `2` .. `36`.

###### Expression Semantics

Semantics of an expression is defined only where it differs from, or is not defined in `PrimitiveGenerator`.

Expression | Semantics
--- | ---
`lit(num)` | Generate the unsigned integer literal `num` using the default formatting (radix is `10`). This generator never fails (unless the underlying output stream reports an error).
    ushort_
    uint_
    ulong_
    ulong_long
| Generate the unsigned integer provided by a mandatory attribute using the default formatting (radix is `10`). This generator never fails (unless the underlying output stream reports an error).
    ushort_(num)
    uint_(num)
    ulong_(num)
    ulong_long(num)
| Generate the unsigned integer provided by the immediate literal value the generator is initialized from using the default formatting (radix is `10`). If this generator has an associated attribute it succeeds only if the attribute is equal to the immediate literal (unless the underlying output stream reports an error). Otherwise this generator fails and does not generate any output.
    bin
    oct
    hex
| Generate the unsigned integer provided by a mandatory attribute using the default formatting and the corresponding radix (`bin`: radix is `2`, `oct`: radix is `8`, `hex`: radix is `16`). This generator never fails (unless the underlying output stream reports an error).
    bin(num)
    oct(num)
    hex(num)
| Generate the unsigned integer provided by the immediate literal value the generator is initialized from using the default formatting and the corresponding radix (`bin`: radix is `2`, `oct`: radix is `8`, `hex`: radix is `16`). If this generator has an associated attribute it succeeds only if the attribute is equal to the immediate literal (unless the underlying output stream reports an error). Otherwise this generator fails and does not generate any output.

All generators listed in the table above (except `lit(num)`) are predefined specializations of the `uint_generator<Num, Radix>` basic unsigned integer number generator type described below. It is possible to directly use this type to create unsigned integer generators using a wide range of formatting options.

Expression | Semantics
--- | ---
    uint_generator<
        Num, Radix
    >()
| Generate the unsigned integer of type `Num` provided by a mandatory attribute using the specified `Radix` (allowed values are from the (inclusive) range from `2` .. `36`, the default value is `10`).This generator never fails (unless the underlying output stream reports an error).
    uint_generator<
        Num, Radix
    >()(num)
| Generate the unsigned integer of type `Num` provided by the immediate literal value the generator is initialized from, using the specified `Radix` (allowed values are from the (inclusive) range from `2` .. `36`, the default value is `10`). If this generator has an associated attribute it succeeds only if the attribute is equal to the immediate literal (unless the underlying output stream reports an error). Otherwise this generator fails and does not generate any output.

###### Additional Requirements

The following lists enumerate the requirements which must be met in order to use a certain type `Num` to instantiate and use a `uint_generator<Num, Radix>`.

If `boost::is_integral<Num>::value` is `true` the type `Num` must have defined:

- comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
- numeric operators for: `+`, `-`, `/`, `*`, and `%`

If `boost::is_integral<Num>::value` is `false` the type `Num` must have defined:

- comparison operators for: `<`, `<=`, `==`, `!=`, `>`, and `>=`
- numeric operators for: `+`, `-`, `/`, `*`, and `%`
- helper functions implementing the interface and the semantics of: `std::fmod`, `std::pow`, `std::lround`, `std::ltrunc`, `std::floor`, and `std::ceil`. These need to be defined in a way so that they will be found using argument dependent lookup (ADL).

###### Attributes

Expression | Attribute
--- | ---
`lit(num)` | `unused`
`ushort_` | `unsigned short`, attribute is mandatory (otherwise compilation will fail)
`ushort_(num)` | `unsigned short`, attribute is optional, if it is supplied, the generator compares the attribute with `num` and succeeds only if both are equal, failing otherwise.
`uint_` | `unsigned int`, attribute is mandatory (otherwise compilation will fail)
`uint_(num)` | `unsigned int`, attribute is optional, if it is supplied, the generator compares the attribute with `num` and succeeds only if both are equal, failing otherwise.
`ulong_` | `unsigned long`, attribute is mandatory (otherwise compilation will fail)
`ulong_(num)` | `unsigned long`, attribute is optional, if it is supplied, the generator compares the attribute with `num` and succeeds only if both are equal, failing otherwise.
`ulong_long` | `unsigned long long`, attribute is mandatory (otherwise compilation will fail)
`ulong_long(num)` | `unsigned long long`, attribute is optional, if it is supplied, the generator compares the attribute with `num` and succeeds only if both are equal, failing otherwise.
    bin
    oct
    hex
| `unsigned int`, attribute is mandatory (otherwise compilation will fail)
    bin(num)
    oct(num)
    hex(num)
| `unsigned int`, attribute is optional, if it is supplied, the generator compares the attribute with `num` and succeeds only if both are equal, failing otherwise.
    uint_generator<
        Num, Radix
    >()
| `Num`, attribute is mandatory (otherwise compilation will fail)
    uint_generator<
        Num, Radix
    >()(num)
| `Num`, attribute is optional, if it is supplied, the generator compares the attribute with `num` and succeeds only if both are equal, failing otherwise.

| Note
In addition to their usual attribute of type `Num` all listed generators accept an instance of a `boost::optional<Num>` as well. If the `boost::optional<>` is initialized (holds a value) the generators behave as if their attribute was an instance of `Num` and emit the value stored in the `boost::optional<>`. Otherwise the generators will fail.

###### Complexity

> O(N), where `N` is the number of digits needed to represent the generated integer number

###### Example

| Note
The test harness for the example(s) below is presented in the Basics Examples section.

Some includes:

    #include <boost/spirit/include/karma.hpp>
    #include <boost/spirit/include/support_utree.hpp>
    #include <boost/spirit/include/phoenix_core.hpp>
    #include <boost/spirit/include/phoenix_operator.hpp>
    #include <boost/fusion/include/std_pair.hpp>
    #include <iostream>
    #include <string>

Some using declarations:

    using boost::spirit::karma::uint_;
    using boost::spirit::karma::lit;

Basic usage of an `uint` generator:

    test_generator("2", lit(2U));
    test_generator("2", uint_(2));
    test_generator_attr("2", uint_(2), 2);
    test_generator_attr("", uint_(2), 3);    // fails (as 2 != 3)!
    test_generator_attr("2", uint_, 2);