...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The as<T>
class forces the atomic assignment of it's subject's synthesized attribute.
Usually, repetitive parsers (such as Kleene,
etc) or sequences exposing a vector<A>
will assign elements to the container
supplied as their synthesized attribute by calling traits::push_back_container
repeatedly.
In some cases, this may be undesirable. The as<T>
class creates a directive that will
pass a temporary object of type T
to it's subject. If the subject parser passes, the temporary object will
be assigned to the directive's supplied attribute with a single call
to traits::assign_to
. If the subject parser
fails, the directive's attribute is not mutated.
Note | |
---|---|
|
Note | |
---|---|
The |
Caution | |
---|---|
The |
// forwards to <boost/spirit/home/qi/directive/as.hpp> #include <boost/spirit/include/qi_as.hpp>
Also, see Include Structure.
Name |
---|
|
|
|
template <typename T> struct as;
Parameter |
Description |
Default |
---|---|---|
|
A container type. [none |
Notation
a
A Parser
.
t
A container of type T
.
attr
The attribute supplied to the directive.
Semantics of an expression is defined only where it differs from, or
is not defined in UnaryParser
.
Expression |
Semantics |
---|---|
|
Create a temporary object of |
|
Equivalent to |
|
Equivalent to |
See Compound Attribute Notation.
Expression |
Attribute |
---|---|
|
|
The complexity is defined by the complexity of the subject parser,
a
, and the complexity of the assignment of the containert
to the supplied attributeattr
.
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
Some using declarations:
using boost::spirit::utree; using boost::spirit::utree_type; using boost::spirit::utf8_symbol_type; using boost::spirit::qi::as; using boost::spirit::qi::as_string; using boost::spirit::qi::char_;
Simple usage of as<T>
,
as_string
and as_wstring
:
To properly handle string concatenation with utree
, we make use of as_string[]
.
We also use as<T>
to explicitly create a utree
symbol node.
utree ut; typedef as<utf8_symbol_type> as_symbol_type; as_symbol_type const as_symbol = as_symbol_type(); test_parser_attr("foo", as_string[*char_], ut); std::cout << ut << std::endl; // will output >"foo"< BOOST_ASSERT(ut.which() == utree_type::string_type); ut.clear(); test_parser_attr("foo", as<std::string>()[*char_], ut); std::cout << ut << std::endl; // will output >"foo"< BOOST_ASSERT(ut.which() == utree_type::string_type); ut.clear(); test_parser_attr("foo", as_symbol[*char_], ut); std::cout << ut << std::endl; // will output >foo< BOOST_ASSERT(ut.which() == utree_type::symbol_type); ut.clear(); test_parser_attr("foo", as<utf8_symbol_type>()[*char_], ut); std::cout << ut << std::endl; // will output >foo< BOOST_ASSERT(ut.which() == utree_type::symbol_type);