...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The attr_cast<Exposed,
Transformed>()
component invokes the embedded parser 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
.
// forwards to <boost/spirit/home/qi/auxiliary/attr_cast.hpp> #include <boost/spirit/include/qi_attr_cast.hpp>
Also, see Include Structure.
Name |
---|
|
template <Exposed, Transformed> <unspecified> attr_cast(<unspecified>);
Parameter |
Description |
Default |
---|---|---|
|
The type of the attribute supplied to the |
|
|
The type of the attribute expected by the embedded parser
|
|
The attr_cast
is a function
template. It is possible to invoke it using the following schemes:
attr_cast(p) attr_cast<Exposed>(p) attr_cast<Exposed, Transformed>(p)
depending on which of the attribute types can be deduced properly if not explicitly specified.
Notation
p
A parser object.
Semantics of an expression is defined only where it differs from, or
is not defined in UnaryParser
.
Expression |
Semantics |
---|---|
|
Create a component invoking the parser |
|
Create a component invoking the parser |
|
Create a component invoking the parser |
Expression |
Attribute |
---|---|
|
|
|
|
|
|
The complexity of this component is fully defined by the complexity of the embedded parser
p
.
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
Some using declarations:
using boost::spirit::qi::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 { // in this case we just expose the embedded 'int' as the attribute instance // to use, allowing to leave the function 'post()' empty template <> struct transform_attribute<int_data, int, qi::domain> { typedef int& type; static int& pre(int_data& d) { return d.i; } static void post(int_data& val, int const& attr) {} static void fail(int_data&) {} }; }}}
Now we use the attr_cast
pseudo parser to invoke the attribute transformation:
int_data d = { 0 }; test_parser_attr("1", boost::spirit::qi::attr_cast(int_), d); std::cout << d.i << std::endl;