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

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext
Attribute Transformation Pseudo Generator (attr_cast)
Description

The attr_cast<Exposed, Transformed>() component invokes the embedded generator while supplying an attribute of type Transformed. The supplied attribute gets created from the original attribute (of type Exposed) passed to this component using the customization point traits::transform_attribute.

Header
// forwards to <boost/spirit/home/karma/auxiliary/attr_cast.hpp>
#include <boost/spirit/include/karma_attr_cast.hpp>

Also, see Include Structure.

Namespace

Name

boost::spirit::attr_cast // alias: boost::spirit::karma::attr_cast

Synopsis
template <Exposed, Transformed>
<unspecified> attr_cast(<unspecified>);
Template parameters

Parameter

Description

Default

Exposed

The type of the attribute supplied to the attr_cast.

unused_type

Transformed

The type of the attribute expected by the embedded generator g.

unused_type

The attr_cast is a function template. It is possible to invoke it using the following schemes:

attr_cast(g)
attr_cast<Exposed>(g)
attr_cast<Exposed, Transformed>(g)

depending on which of the attribute types can be deduced properly if not explicitly specified.

Model of

UnaryGenerator

Notation

g

A generator object.

Expression Semantics

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

Expression

Semantics

attr_cast(g)

Create a component invoking the generator g while passing an attribute of the type as normally expected by g. The type of the supplied attribute will be transformed to the type g exposes as its attribute type (by using the attribute customization point traits::transform_attribute). This generator does not fail unless g fails.

attr_cast<Exposed>(g)

Create a component invoking the generator g while passing an attribute of the type as normally expected by g. The supplied attribute is expected to be of the type Exposed, it will be transformed to the type g exposes as its attribute type (using the attribute customization point traits::transform_attribute). This generator does not fail unless g fails.

attr_cast<Exposed, Transformed>(g)

Create a component invoking the generator g while passing an attribute of type Transformed. The supplied attribute is expected to be of the type Exposed, it will be transformed to the type Transformed (using the attribute customization point traits::transform_attribute). This generator does not fail unless g fails.

Attributes

Expression

Attribute

attr_cast(g)

g: A --> attr_cast(g): A

attr_cast<Exposed>(g)

g: A --> attr_cast<Exposed>(g): Exposed

attr_cast<Exposed, Transformed>(g)

g: A --> attr_cast<Exposed, Transformed>(g): Exposed

Complexity

The complexity of this component is fully defined by the complexity of the embedded generator g.

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::int_;

The example references data structure int_data which needs a specialization of the customization point traits::transform_attribute:

// this is just a test structure we want to use in place of an int
struct int_data
{
    int i;
};

// we provide a custom attribute transformation to allow its use as an int
namespace boost { namespace spirit { namespace traits
{
    template <>
    struct transform_attribute<int_data const, int, karma::domain>
    {
        typedef int type;
        static int pre(int_data const& d) { return d.i; }
    };
}}}

Now we use the attr_cast pseudo generator to invoke the attribute transformation:

int_data d = { 1 };
test_generator_attr("1", boost::spirit::karma::attr_cast(int_), d);


PrevUpHomeNext