Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / C++

Wave: a Standard conformant C++ preprocessor library

Rate me:
Please Sign up or sign in to vote.
4.96/5 (58 votes)
10 Jan 200413 min read 395.5K   4.4K   81  
Describes a free and fully Standard conformant C++ preprocessor library
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>The Input Policy</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="theme/style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="100%" border="0" cellspacing="2" background="theme/bkd2.gif">
  <tr> 
    <td width="21"> <h1></h1></td>
    <td width="885"> <font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="6">The 
      Input Policy</font></b></font></td>
    <td width="96"><a href="http://spirit.sf.net"><img src="theme/wave.gif" width="93" height="68" align="right" border="0"></a></td>
  </tr>
</table>
<br>
<table border="0">
  <tr> 
    <td width="10"></td>
    <td width="30"><a href="index.html"><img src="theme/u_arr.gif" border="0"></a></td>
    <td width="30"><a href="class_reference_context.html"><img src="theme/l_arr.gif" width="20" height="19" border="0"></a></td>
    <td width="20"><a href="class_reference_tracepolicy.html"><img src="theme/r_arr.gif" border="0"></a></td>
  </tr>
</table>

<blockquote> 
  <p><a href="class_reference_inputpolicy.html#introduction">Introduction</a><br>
    <a href="class_reference_inputpolicy.html#header_synopsis">Header 'wave/cpp_iteration_context.hpp' 
    synopsis</a><br>
    <a href="class_reference_inputpolicy.html#template_parameters">Template parameters</a><br>
    <a href="class_reference_inputpolicy.html#member_functions">Member functions</a></p>
</blockquote>
<h2><b><a name="introduction"></a>Introduction</b></h2>
<p>The input policy type may be specified as a template parameter to the <tt>wave::context</tt> 
  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 <tt>iteration_context_policies::load_file_to_string</tt> 
  type. </p>
<h2><b><a name="header_synopsis"></a>Header <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/spirit/spirit/wave/wave/cpp_iteration_context.hpp?rev=HEAD&content-type=text/vnd.viewcvs-markup">wave/cpp_iteration_context.hpp</a> 
  synopsis</b></h2>
<p>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 <tt>Wave</tt> library.</p>
<pre><span class="keyword">namespace</span> wave {
<span class="keyword">namespace</span> iteration_context_policies {

    <span class="keyword">struct</span> load_file_to_string {
    
        <span class="keyword">template</span> &lt;<span class="keyword">typename</span> IterContextT&gt;
        <span class="keyword">class</span> inner {
        
        <span class="keyword">public</span>:
            <span class="comment">// expose the begin and end iterators for the</span>
            <span class="comment">// included file</span>
            <span class="keyword">template</span> &lt;typename PositionT&gt;
            <span class="keyword">static</span> 
            <span class="keyword">void</span> <a href="class_reference_inputpolicy.html#init_iterators">init_iterators</a>(IterContextT &iter_ctx, 
                PositionT const &act_pos)
            {
                <span class="keyword">typedef typename</span> IterContextT::iterator_t iterator_t;
                
                <span class="keyword">std::ifstream</span> 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 = <span class="keyword">std::string</span>(
                    <span class="keyword">std::istreambuf_iterator</span><char>(instream.rdbuf()),
                    <span class="keyword">std::istreambuf_iterator</span><char>());

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

        <span class="keyword">private</span>:
            <span class="keyword">std::string</span> instring;
        };
    };

}   <span class="comment">// namespace iteration_context_policies</span><br>}   <span class="comment">// namespace wave </span>  </pre>
<p>As you can see, an <tt>input_policy</tt> for the <tt>wave::context</tt> 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 <tt>inner</tt>.</p>
<h3><a name="template_parameters"></a>Template Parameters</h3>
<p>The <tt>inner</tt> class is instantiated with one template parameter, the iteration 
  context type, from which the policy is a part of. The iterator type <tt>iterator_t</tt> 
  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 
  <tt>iter_ctx.first</tt> and <tt>iter_ctx.last</tt>) has to initialized from 
  an abritrary iterator type, representing the actual input stream.</p>
<h3><a name="member_functions"></a>Member Functions</h3>
<p><a name="init_iterators"></a><b>init_iterators</b></p>
<pre>    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> PositionT&gt;
    <span class="keyword">static void</span> init_iterators(
        IterContextT iter_ctx, 
        PositionT const &act_pos);</pre>
<blockquote> 
  <p>The <tt>init_iterators</tt> function is called, when an <tt>#include</tt> 
    directive was found in the input token stream. The main rationale for this 
    function is to initialize the pair of iterators <tt>iter_ctx.first</tt> and 
    <tt>iter_ctx.last</tt>, which are to be used to access the input stream corresponding 
    to the include file to be inserted from inside the preprocessing engine.<br>
  </p>
</blockquote>
<table border="0">
  <tr> 
    <td width="10"></td>
    <td width="30"><a href="index.html"><img src="theme/u_arr.gif" border="0"></a></td>
    <td width="30"><a href="class_reference_context.html"><img src="theme/l_arr.gif" width="20" height="19" border="0"></a></td>
    <td width="20"><a href="class_reference_tracepolicy.html"><img src="theme/r_arr.gif" border="0"></a></td>
  </tr>
</table>
<hr size="1">
<p class="copyright">Copyright &copy; 2003 Hartmut Kaiser<br>
  <br>
  <font size="2">Permission to copy, use, modify, sell and distribute this document 
  is granted provided this copyright notice appears in all copies. This document 
  is provided &quot;as is&quot; without express or implied warranty, and with 
  no claim as to its suitability for any purpose. </font> </p>
<span class="updated">Last updated: 
  <!-- #BeginDate format:fcAm1m -->Monday, April 7, 2003  9:46<!-- #EndDate -->
  </span></body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Actively involved in Boost and the development of the Spirit parser construction framework.

Comments and Discussions