...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 extraction of a container type T
from it's consumed attribute. Usually,
repetitive generators (such as Kleene
(*
), etc) or sequences
exposing a vector<A>
will extract elements from the container supplied as their consumed attribute
by looping through the containers iterators. In some cases, this may
be undesirable. The as<T>
class creates a directive that will
pass an unnamed temporary object of type T
to it's subject, if extracting T
from it's consumed attribute determined at generation-time to be valid.
traits::valid_as<T>()
is called by as<T>
to determine validity; if it returns false, the generator fails. Subsequent
extraction is performed by calling traits::as<T>()
.
Note | |
---|---|
|
// forwards to <boost/spirit/home/karma/directive/as.hpp> #include <boost/spirit/include/karma_as.hpp>
Also, see Include Structure.
Name |
---|
|
|
template <typename T> struct as;
Parameter |
Description |
Default |
---|---|---|
|
A container type. |
none |
Notation
a
A Generator
.
attr
The attribute supplied to the directive.
Semantics of an expression is defined only where it differs from, or
is not defined in UnaryGenerator
.
Expression |
Semantics |
---|---|
|
Extract an instance of |
|
Equivalent to |
|
Equivalent to |
See Compound Attribute Notation.
Expression |
Attribute |
---|---|
|
|
The complexity is defined by the complexity of the subject generator,
a
, and the complexity of the extraction unnamed contianer of typeT
from the 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::karma::as; using boost::spirit::karma::as_string; using boost::spirit::karma::char_; using boost::spirit::karma::double_;
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 extract a utree
symbol node.
typedef as<utf8_symbol_type> as_symbol_type; as_symbol_type const as_symbol = as_symbol_type(); utree ut; ut.push_back("xyz"); ut.push_back(1.23); test_generator_attr("xyz1.23", as_string[*char_] << double_, ut); test_generator_attr("xyz1.23", as<std::string>()[*char_] << double_, ut); ut.clear(); ut.push_back(utf8_symbol_type("xyz")); ut.push_back(1.23); test_generator_attr("xyz1.23", as_symbol[*char_] << double_, ut); test_generator_attr("xyz1.23", as<utf8_symbol_type>()[*char_] << double_, ut);