...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
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.
// forwards to <boost/spirit/home/karma/numeric/bool.hpp> #include <boost/spirit/include/karma_bool.hpp>
Also, see Include Structure.
Name |
---|
|
|
|
|
Note | |
---|---|
|
template < typename B , unsigned Policies> struct bool_generator;
Parameter |
Description |
Default |
---|---|---|
|
The boolean base type of the boolean generator. |
|
|
The policies to use while converting the boolean. |
|
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
Semantics of an expression is defined only where it differs from, or
is not defined in PrimitiveGenerator
.
Expression |
Semantics |
---|---|
|
Generate the boolean literal |
|
Generate the boolean value provided by a mandatory attribute
using the default formatting ( |
|
Generate the boolean value provided by the immediate literal
value the generator is initialized from using the default formatting
( |
|
Generate |
|
Generate |
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 |
bool_generator< B, Policies >()(b)
|
Generate the boolean of type |
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
:
bool
Expression |
Attribute |
---|---|
|
|
|
|
|
|
bool_generator< B, Policies >()
|
|
bool_generator< B, Policies >()(b)
|
|
Note | |
---|---|
In addition to their usual attribute of type |
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
.
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 template <typename Inserter, typename OutputIterator , typename Policies> static bool call (OutputIterator& sink, B b, Policies const& p) { return Inserter::call_n(sink, b, p); }
|
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
The template parameters 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
The template parameters The return value defines the outcome of the whole generator. |
O(N), where
N
is the number of characters needed to represent the generated boolean.
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/phoenix/core.hpp> #include <boost/phoenix/operator.hpp> #include <boost/fusion/include/std_pair.hpp> #include <boost/proto/deep_copy.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);