Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext
Boolean Generators (bool_)
Description

As you might expect, the bool_generator can generate output from boolean values. The bool_generator generator can be used to generate output from ordinary primitive C/C++ bool values or user defined boolean types if the type follows certain expression requirements (for more information about the requirements, see below)). The bool_generator is a template class. Template parameters fine tune its behavior.

Header
// forwards to <boost/spirit/home/karma/numeric/bool.hpp>
#include <boost/spirit/include/karma_bool.hpp>

Also, see Include Structure.

Namespace

Name

boost::spirit::lit // alias: boost::spirit::karma::lit

boost::spirit::bool_ // alias: boost::spirit::karma::bool_

boost::spirit::true_ // alias: boost::spirit::karma::true_

boost::spirit::false_ // alias: boost::spirit::karma::false_

[Note] 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 (boolean) literal.

Synopsis
template <
    typename B
  , unsigned Policies>
struct bool_generator;
Template parameters

Parameter

Description

Default

B

The boolean base type of the boolean generator.

bool

Policies

The policies to use while converting the boolean.

bool_policies<B>

Model of

PrimitiveGenerator

Notation

b

Boolean literal, or a Lazy Argument that evaluates to a boolean value of type B

B

Type of b: any type usable as a boolean, or in case of a Lazy Argument, its return value

Expression Semantics

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

Expression

Semantics

lit(b)

Generate the boolean literal b using the default formatting (false is generated as "false", and true is generated as "true"). This generator never fails (unless the underlying output stream reports an error).

bool_

Generate the boolean value provided by a mandatory attribute using the default formatting (false is generated as "false", and true is generated as "true"). This generator never fails (unless the underlying output stream reports an error).

bool_(b)

Generate the boolean value provided by the immediate literal value the generator is initialized from using the default formatting (false is generated as "false", and true is generated as "true"). 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.

true_

Generate "true". If this generator has an associated attribute it succeeds only if the attribute is true as well (unless the underlying output stream reports an error).

false_

Generate "false". If this generator has an associated attribute it succeeds only if the attribute is false as well (unless the underlying output stream reports an error).

All generators listed in the table above (except lit(num)) are predefined specializations of the bool_generator<B, Policies> basic boolean generator type described below. It is possible to directly use this type to create boolean generators using a wide range of formatting options.

Expression

Semantics

bool_generator<
    B, Policies
>()

Generate the boolean of type B provided by a mandatory attribute using the specified Policies This generator never fails (unless the underlying output stream reports an error).

bool_generator<
    B, Policies
>()(b)

Generate the boolean of type B provided by the immediate literal value the generator is initialized from, using the specified Policies. 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.

[Note] Note

All boolean generators properly respect the upper and lower directives.

Additional Requirements

The following lists enumerate the requirements which must be met in order to use a certain type B to instantiate and use a bool_generator<B, Policies>.

The type B:

Attributes

Expression

Attribute

bool_(b)

unused

bool_

bool, attribute is mandatory (otherwise compilation will fail)

bool_(b)

bool, attribute is optional, if it is supplied, the generator compares the attribute with b and succeeds only if both are equal, failing otherwise.

bool_generator<
    B, Policies
>()

B, attribute is mandatory (otherwise compilation will fail)

bool_generator<
    B, Policies
>()(b)

B, attribute is optional, if it is supplied, the generator compares the attribute with b and succeeds only if both are equal, failing otherwise.

[Note] Note

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

Boolean Formatting Policies

If special formatting of a boolean is needed, overload the policy class bool_policies<B> and use it as a template parameter to the bool_generator<> boolean generator. For instance:

struct special_bool_policy : karma::bool_policies<>
{
    template <typename CharEncoding, typename Tag
      , typename OutputIterator>
    static bool generate_false(OutputIterator& sink, bool b)
    {
        // we want to spell the names of false as eurt (true backwards)
        return string_inserter<CharEncoding, Tag>::call(sink, "eurt");
    }
};

typedef karma::bool_generator<special_bool_policy> backwards_bool_type;
backwards_bool_type const backwards_bool;

karma::generate(sink, backwards_bool, true);    // will output: true
karma::generate(sink, backwards_bool(false));   // will output: uert

The template parameter B should be the type to be formatted using the overloaded policy type. At the same time B will be used as the attribute type of the created real number generator. The default for B is bool.

Boolean Formatting Policy Expression Semantics

A boolean formatting policy should expose the following:

Expression

Description

template <typename Inserter
  , typename OutputIterator
  , typename Policies>
bool call (OutputIterator& sink, Num n
  , Policies const& p);

This is the main function used to generate the output for a boolean. It is called by the boolean generator in order to perform the conversion. In theory all of the work can be implemented here, but the easiest way is to use existing functionality provided by the type specified by the template parameter Inserter. The default implementation of this functions is:

template <typename Inserter, typename OutputIterator
  , typename Policies>
static bool
call (OutputIterator& sink, B b, Policies const& p)
{
    return Inserter::call_n(sink, b, p);
}

sink is the output iterator to use for generation

b is the boolean to convert

p is the instance of the policy type used to instantiate this real number generator.

template <typename CharEncoding,
    typename Tag, typename OutputIterator>
bool generate_false(
    OutputIterator& sink, B b);

This function is called to generate the boolean if it is false.

sink is the output iterator to use for generation

b is the boolean to convert (the value is false).

The template parameters CharEncoding and Tag are either of the type unused_type or describe the character class and conversion to be applied to any output possibly influenced by either the lower[] or upper[] directives.

The return value defines the outcome of the whole generator.

template <typename CharEncoding,
    typename Tag, typename OutputIterator>
bool generate_true(
    OutputIterator& sink, B b);

This function is called to generate the boolean if it is true.

sink is the output iterator to use for generation

b is the boolean to convert (the value is true).

The template parameters CharEncoding and Tag are either of the type unused_type or describe the character class and conversion to be applied to any output possibly influenced by either the lower[] or upper[] directives.

The return value defines the outcome of the whole generator.

Complexity

O(N), where N is the number of characters needed to represent the generated boolean.

Example
[Note] 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::bool_;
using boost::spirit::karma::lit;

Basic usage of an bool_ generator:

test_generator("true", lit(true));
test_generator("false", bool_(false));
test_generator_attr("true", bool_(true), true);
test_generator_attr("", bool_(true), false);    // fails (as true != false)!
test_generator_attr("false", bool_, false);


PrevUpHomeNext