class_reference_inptpolcy.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>The Input Policy</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <link href="theme/style.css" rel="stylesheet" type="text/css">
  7. </head>
  8. <body>
  9. <table width="100%" border="0" cellspacing="2" background="theme/bkd2.gif">
  10. <tr>
  11. <td width="21"> <h1></h1></td>
  12. <td width="885"> <font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="6">The
  13. Input Policy</font></b></font></td>
  14. <td width="96"><a href="http://www.boost.org"><img src="theme/wave.gif" width="93" height="68" align="right" border="0"></a></td>
  15. </tr>
  16. </table>
  17. <br>
  18. <table border="0">
  19. <tr>
  20. <td width="10"></td>
  21. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  22. <td width="30"><a href="class_reference_context.html"><img src="theme/l_arr.gif" width="20" height="19" border="0"></a></td>
  23. <td width="30"><a href="class_reference_ctxpolicy.html"><img src="theme/r_arr.gif" border="0"></a></td>
  24. </tr>
  25. </table>
  26. <blockquote>
  27. <p><a href="class_reference_inptpolcy.html#introduction">Introduction</a><br>
  28. <a href="class_reference_inptpolcy.html#header_synopsis">Header 'wave/cpp_iteration_context.hpp'
  29. synopsis</a><br>
  30. <a href="class_reference_inptpolcy.html#template_parameters">Template parameters</a><br>
  31. <a href="class_reference_inptpolcy.html#member_functions">Member functions</a></p>
  32. </blockquote>
  33. <h2><b><a name="introduction"></a>Introduction</b></h2>
  34. <p>The input policy type may be specified as a template parameter to the <tt>wave::context</tt>
  35. object and is used for customizing the way, how an included file is to be represented
  36. by a pair of iterators pointing to the beginning and the end of the resulting
  37. input sequence. If this template parameter is not given while instantiating
  38. the context object, it defaults to the <tt>iteration_context_policies::load_file_to_string</tt>
  39. type. </p>
  40. <h2><b><a name="header_synopsis"></a>Header <a href="http://svn.boost.org/trac/boost/browser/trunk/boost/wave/util/iteration_context.hpp">wave/iteration_context.hpp</a>
  41. synopsis</b></h2>
  42. <p>The following code listing does not show the required interface only, but for
  43. brevity reasons the whole implementation of an input policy, which loads the
  44. given file into a string variable and exposes the begin() and end() iterators
  45. of this string to the <tt>Wave</tt> library.</p>
  46. <pre><span class="keyword">namespace</span> boost {
  47. <span class="keyword">namespace</span> wave {
  48. <span class="keyword">namespace</span> iteration_context_policies {
  49. <span class="keyword">struct</span> load_file_to_string {
  50. <span class="keyword">template</span> &lt;<span class="keyword">typename</span> IterContext&gt;
  51. <span class="keyword">class</span> inner {
  52. <span class="keyword">public</span>:
  53. <span class="comment">// expose the begin and end iterators for the</span>
  54. <span class="comment">// included file</span>
  55. <span class="keyword">template</span> &lt;typename Position&gt;
  56. <span class="keyword">static</span>
  57. <span class="keyword">void</span> <a href="class_reference_inptpolcy.html#init_iterators">init_iterators</a>(IterContext&iter_ctx,
  58. Position const &act_pos)
  59. {
  60. <span class="keyword">typedef typename</span> IterContext::iterator_type iterator_type;
  61. <span class="keyword">std::ifstream</span> instream(iter_ctx.filename.c_str());
  62. if (!instream.is_open()) {
  63. CPP_THROW(preprocess_exception, bad_include_file,
  64. iter_ctx.filename, act_pos);
  65. }
  66. iter_ctx.instring = <span class="keyword">std::string</span>(
  67. <span class="keyword">std::istreambuf_iterator</span><char>&lt;char&gt;(instream.rdbuf()),
  68. <span class="keyword">std::istreambuf_iterator</span><char>&lt;char&gt;());
  69. iter_ctx.first = iterator_type(iter_ctx.instring.begin(),
  70. iter_ctx.instring.end(),
  71. PositionT(iter_ctx.filename));
  72. iter_ctx.last = iterator_type();
  73. }
  74. <span class="keyword">private</span>:
  75. <span class="keyword">std::string</span> instring;
  76. };
  77. };
  78. } <span class="comment">// namespace iteration_context_policies</span>
  79. } <span class="comment">// namespace wave </span>
  80. } <span class="comment">// namespace boost </span> </pre>
  81. <p>As you can see, an <tt>input_policy</tt> for the <tt>wave::context</tt> object
  82. should implement one function only, the init_iterators function. The policy
  83. shown is implemented with the help of an embedded class to avoid the need for
  84. template template parameters, which aren't implemented by all systems today.
  85. This embedded class should have the name <tt>inner</tt>.</p>
  86. <h3><a name="template_parameters"></a>Template Parameters</h3>
  87. <p>The <tt>inner</tt> class is instantiated with one template parameter, the iteration
  88. context type, from which the policy is a part of. The iterator type <tt>iterator_type</tt>
  89. which is used to access the underlying input stream has to be derived through
  90. a typedef as shown. The iterator pair to initialize (which is accessible as
  91. <tt>iter_ctx.first</tt> and <tt>iter_ctx.last</tt>) has to initialized from
  92. an abritrary iterator type, representing the actual input stream.</p>
  93. <h3><a name="member_functions"></a>Member Functions</h3>
  94. <p><a name="init_iterators"></a><b>init_iterators</b></p>
  95. <pre> <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Position&gt;
  96. <span class="keyword">static void</span> init_iterators(
  97. IterContext iter_ctx,
  98. Position const &act_pos);</pre>
  99. <p>directive was found in the input token stream. The main rationale for this
  100. function is to initialize the pair of iterators <tt>iter_ctx.first</tt> and
  101. <tt>iter_ctx.last</tt>, which are to be used to access the input stream corresponding
  102. to the include file to be inserted from inside the preprocessing engine.</p>
  103. </blockquote>
  104. <table border="0">
  105. <tr>
  106. <td width="10"></td>
  107. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  108. <td width="30"><a href="class_reference_context.html"><img src="theme/l_arr.gif" width="20" height="19" border="0"></a></td>
  109. <td width="30"><a href="class_reference_ctxpolicy.html"><img src="theme/r_arr.gif" border="0"></a></td>
  110. </tr>
  111. </table>
  112. <hr size="1">
  113. <p class="copyright">Copyright &copy; 2003-2011 Hartmut Kaiser<br>
  114. <br>
  115. <font size="2">Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) </font> </p>
  116. <span class="updated"></span>
  117. <p class="copyright"><span class="updated">Last updated:
  118. <!-- #BeginDate format:fcAm1m -->Sunday, October 12, 2008 20:14<!-- #EndDate -->
  119. </span></p>
  120. </body>
  121. </html>