快速入门

使用预处理Wave高度可配置。您必须定义一些选项来控制它。以下是您可以定义的若干选项

 包含搜索路径,用于定义在何处搜索要通过#include <...>and#include "..."指令
 要预定义的宏以及要取消定义的预定义宏
 是否启用 C++ 标准的若干扩展(例如可变参数和占位符)

您可以通过boost::wave::context对象访问所有这些处理参数。因此,必须实例化此类型的一个对象实例来使用Wave库。(有关 context 模板类的更多信息,请参阅类参考 这里。)要实例化boost::wave::context对象,您必须提供至少两个模板参数:底层输入流的迭代器类型,以及用作预处理引擎标记源的词法分析器迭代器类型。

不要自行实例化主预处理迭代器。请从boost::wave::context对象获取。下面的代码片段取自quick_start示例,展示了Wave.

    // The following preprocesses a given input file.
    // Open the file and read it into a string variable
    std::ifstream instream("input.cpp");
    std::string input(
        std::istreambuf_iterator<char>(instream.rdbuf()),
        std::istreambuf_iterator<char>());

    // The template boost::wave::cpplexer::lex_token<> is the  
    // token type to be used by the Wave library.
    // This token type is one of the central types throughout 
    // the library, because it is a template parameter to some 
    // of the public classes and templates and it is returned 
    // from the iterators.
    // The template boost::wave::cpplexer::lex_iterator<> is
    // the lexer iterator to use as the token source for the
    // preprocessing engine. In this case this is parameterized
    // with the token type.
    typedef boost::wave::cpplexer::lex_iterator<
            boost::wave::cpplexer::lex_token<> >
        lex_iterator_type;
    typedef boost::wave::context<
            std::string::iterator, lex_iterator_type>
        context_type;

    context_type ctx(input.begin(), input.end(), "input.cpp");

    // At this point you may want to set the parameters of the
    // preprocessing as include paths and/or predefined macros.
        ctx.add_include_path("...");
        ctx.add_macro_definition(...);

    // Get the preprocessor iterators and use them to generate 
    // the token sequence.
    context_type::iterator_type first = ctx.begin();
    context_type::iterator_type last = ctx.end();

    // The input stream is preprocessed for you during iteration
// over [first, last)
while (first != last) { std::cout << (*first).get_value(); ++first; }

的最小使用场景boost::wave::context对象的构造函数可以接受一对任意迭代器类型(至少input_iterator类型迭代器),用于输入流,必须提供待处理的数据。第三个参数提供一个文件名,该文件名会在预处理器输出中报告,以指示当前上下文。需要注意的是,只要没有#include#line指令出现,这个文件名才会被使用;一旦出现指令,就会改变当前报告的文件名。

对预处理后标记的迭代相对直接。只需从 context 对象获取起始迭代器和结束迭代器(可能在初始化一些包含搜索路径后),即可完成!对迭代器解引用将返回即时从输入流生成的预处理标记。(欲获取标记类型的更多信息,请查看 这里。)