The Input Policy 輸入策略

Introduction 簡介
Header 'wave/cpp_iteration_context.hpp' synopsis 頭文件'wave/cpp_iteration_context.hpp'概要
Template parameters 模板參數
Member functions 成員函數

Introduction 簡介

The input policy type may be specified as a template parameter to the wave::context object and is used for customizing the way, how an included file is to be represented by a pair of iterators pointing to the beginning and the end of the resulting input sequence. If this template parameter is not given while instantiating the context object, it defaults to the iteration_context_policies::load_file_to_string type.
輸入策略的類型可以指定為 wave::context 對象的一個模板參數,並用於定制一個包含文件如何用一對指向輸入序列的開始與結尾的迭代器來表示。如果在對上下文對像進行初始化時該模板參數未給出,則缺省為 iteration_context_policies::load_file_to_string 類型。

Header wave/iteration_context.hpp synopsis 頭文件概要

The following code listing does not show the required interface only, but for brevity reasons the whole implementation of an input policy, which loads the given file into a string variable and exposes the begin() and end() iterators of this string to the Wave library.
以下代碼並不只是展示所需要的接口,而是一個輸入策略的完整實現,該策略將指定文件導入至一個字符串變量,並將該字符串的 begin() 和 end() 迭代器暴露給 Wave 庫。

namespace boost {
namespace wave {
namespace iteration_context_policies {

struct load_file_to_string {

template <typename IterContext>
class inner {

public:
// expose the begin and end iterators for the // included file template <typename Position>
static
void init_iterators(IterContext&iter_ctx,
Position const &act_pos)
{
typedef typename IterContext::iterator_type iterator_type;

std::ifstream instream(iter_ctx.filename.c_str());
if (!instream.is_open()) {
CPP_THROW(preprocess_exception, bad_include_file,
iter_ctx.filename, act_pos);
}

iter_ctx.instring = std::string(
std::istreambuf_iterator(instream.rdbuf()),
std::istreambuf_iterator());

iter_ctx.first = iterator_type(iter_ctx.instring.begin(),
iter_ctx.instring.end(),
PositionT(iter_ctx.filename));
iter_ctx.last = iterator_type();
}

private:
std::string instring;
};
};

} // namespace iteration_context_policies } // namespace wave } // namespace boost

As you can see, an input_policy for the wave::context object should implement one function only, the init_iterators function. The policy shown is implemented with the help of an embedded class to avoid the need for template template parameters, which aren't implemented by all systems today. This embedded class should have the name inner.
如你所見,一個用於 wave::context 對象的 input_policy 只實現一個函數 init_iterators。以上所示策略的實現利用了一個內嵌類,來避免使用模板模板參數,因為迄今為止還不是所有的系統都實現了模板模板參數。這個內嵌類的名字為 inner.

Template Parameters 模板參數

The inner class is instantiated with one template parameter, the iteration context type, from which the policy is a part of. The iterator type iterator_type which is used to access the underlying input stream has to be derived through a typedef as shown. The iterator pair to initialize (which is accessible as iter_ctx.first and iter_ctx.last) has to initialized from an abritrary iterator type, representing the actual input stream.
inner 類以一個模板參數初始化,該參數為以本策略作為其一部分的迭代上下文類型。用於訪問底層輸入流的迭代器類型 iterator_type 必須通過一個以上所示的 typedef 來生成。初始化的迭代器對(它們可以通過 iter_ctx.firstiter_ctx.last 來訪問)必須從一個任意的迭代器類型來初始化,用來表示實際的輸入流。

Member Functions 成員函數

init_iterators

    template <typename Position>
static void init_iterators(
IterContext iter_ctx,
Position const &act_pos);

directive was found in the input token stream. The main rationale for this function is to initialize the pair of iterators iter_ctx.first and iter_ctx.last, which are to be used to access the input stream corresponding to the include file to be inserted from inside the preprocessing engine.
指令在輸入單詞流中找到。該函數的主要原理是,初始化一對迭代器 iter_ctx.firstiter_ctx.last,它們被用於訪問與被插入到預處理引擎中的頭文件相對應的輸入流。