Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::xpressive::placeholder< T, I, Dummy > Struct Template Reference

For defining a placeholder to stand in for a variable a semantic action. More...

#include <xpressive_fwd.hpp>

Public Types

typedef placeholder< T, I, Dummy > this_type
 
typedef proto::terminal
< detail::action_arg< T,
mpl::int_< I > > >::type 
action_arg_type
 

Detailed Description

template<typename T, int I, typename Dummy>
struct boost::xpressive::placeholder< T, I, Dummy >

For defining a placeholder to stand in for a variable a semantic action.

Use placeholder<> to define a placeholder for use in semantic actions to stand in for real objects. The use of placeholders allows regular expressions with actions to be defined once and reused in many contexts to read and write from objects which were not available when the regex was defined.

Template Parameters
TThe type of the object for which this placeholder stands in.
IAn optional identifier that can be used to distinguish this placeholder from others that may be used in the same semantic action that happen to have the same type.

You can use placeholder<> by creating an object of type placeholder<T> and using that object in a semantic action exactly as you intend an object of type T to be used.

placeholder<int> _i;
placeholder<double> _d;
sregex rex = ( some >> regex >> here )
[ ++_i, _d *= _d ];

Then, when doing a pattern match with either regex_search(), regex_match() or regex_replace(), pass a match_results<> object that contains bindings for the placeholders used in the regex object's semantic actions. You can create the bindings by calling match_results::let as follows:

int i = 0;
double d = 3.14;
what.let(_i = i)
.let(_d = d);
if(regex_match("some string", rex, what))
// i and d mutated here

If a semantic action executes that contains an unbound placeholder, a exception of type regex_error is thrown.

See the discussion for xpressive::let() and the {user_s_guide.semantic_actions_and_user_defined_assertions.referring_to_non_local_variables, "Referring to Non-Local Variables"} section in the Users' Guide for more information.

Example:

// Define a placeholder for a map object:
placeholder<std::map<std::string, int> > _map;
// Match a word and an integer, separated by =>,
// and then stuff the result into a std::map<>
sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
[ _map[s1] = as<int>(s2) ];
// Match one or more word/integer pairs, separated
// by whitespace.
sregex rx = pair >> *(+_s >> pair);
// The string to parse
std::string str("aaa=>1 bbb=>23 ccc=>456");
// Here is the actual map to fill in:
std::map<std::string, int> result;
// Bind the _map placeholder to the actual map
what.let( _map = result );
// Execute the match and fill in result map
if(regex_match(str, what, rx))
{
std::cout << result["aaa"] << '\n';
std::cout << result["bbb"] << '\n';
std::cout << result["ccc"] << '\n';
}

Member Typedef Documentation

template<typename T , int I, typename Dummy >
typedef proto::terminal<detail::action_arg<T, mpl::int_<I> > >::type boost::xpressive::placeholder< T, I, Dummy >::action_arg_type
template<typename T , int I, typename Dummy >
typedef placeholder<T, I, Dummy> boost::xpressive::placeholder< T, I, Dummy >::this_type

The documentation for this struct was generated from the following file: