Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Karma Confix Generator

Description

The Spirit.Karma confix generator is a primitive generator component allowing to embed any generated ouput inside an opening (a prefix) and a closing (a suffix). A simple example is a C comment: /* This is a C comment */ which can be generated using the confix generator as: confix("/*", "*/")["This is a C comment"]. The general syntax for using the confix is:

confix(prefix, suffix)[subject]

which results in generating the sequence equivalent to

prefix << subject << suffix

Using the confix component instead of the explicit sequence has the advantage of being able to encapsulate the prefix and the suffix into a separate generator construct. The following code snippet illustrates the idea:

// Define a metafunction allowing to compute the type of the confix()
// construct
namespace traits
{
    template <typename Prefix, typename Suffix = Prefix>
    struct confix_spec
    {
        using namespace boost::spirit;
        typedef typename spirit::result_of::terminal<
            repository::tag::confix(Prefix, Suffix)
        >::type type;
    };
};

// Define a helper function allowing to create a confix() construct from 
// arbitrary prefix and suffix generators
template <typename Prefix, typename Suffix>
typename traits::confix_spec<Prefix, Suffix>::type
confix_spec(Prefix const& prefix, Suffix const& suffix)
{
    using namespace boost::spirit;
    return repository::confix(prefix, suffix);
}

// Define a helper function to  construct a HTML tag from the tag name
inline typename traits::confix_spec<std::string>::type
tag (std::string const& tagname)
{
    return confix_spec("<" + tagname + ">", "</" + tagname + ">");
}

// Define generators for different HTML tags the HTML tag
typedef traits::confix_spec<std::string>::type ol = tag("ol");      // <ol>...</ol> 
typedef traits::confix_spec<std::string>::type li = tag("li");      // <li>...</li> 

Now, for instance, the above definitions allow to generate the HTML 'ol' tag using a simple: ol["Some text"] (which results in <ol>Some text</ol>).

Header

#include <boost/spirit/repository/include/karma_confix.hpp>

Synopsis

confix(prefix, suffix)[subject]

Parameters

Parameter

Description

prefix

The generator construct to use to format the opening (the prefix). The prefix is the part generated before any output as generated by the subject.

suffix

The generator construct to use to format the ending (the suffix). The suffix is the part generated after any output as generated by the subject.

subject

The generator construct to use to format the actual output in between the prefix and suffix parts.

All

Model of

Link to concept

Objects

Objects provided by the library

Notation

xxx

An XXX

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

Expression

Semantics

Return type

Complexity

xxx

Semantics of xxx

XXX

Constant

Example

Real example code. Use Quickbook import mechanism to link to actual working code snippets here.


PrevUpHomeNext