Boost logo

Boost :

From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2007-03-10 18:01:54


> I'm using boost::wave to implement a C/C++ preprocessor for
> the Synopsis
> ( project. I have just been asked
> for support of the '-include' command line option, which, for
> typical compilers, means to preload an include file before
> parsing the actual input.
> I wonder how to implement that with wave. The wave::context<>
> constructor expects two input iterators (begin and end) which
> it will use during parsing.
> Relying only on this would mean I have to write my own
> iterators (or wrapper
> container) that is able to switch from one input source (the
> one specified by
> '-include') to the next (the actual input file).
> However, I believe there is a lot of value in wave::context<>
> being able to do that internally (either, by allowing to
> reassign a new pair of iterators, or by providing a copy
> constructor for the context that creates a new context from a
> new pair of iterators, but inherits the state of an existing context).
> Does anything like this exist ? Are there plans to support
> such use cases ?

Wave does already support that! The iterator returned from the context has a
member function force_include():

    context_type::iterator_type it = ctx.begin();
    for (each of the files to be pre-included)
        it.force_include(filename, last_name);

Note, you have to call force_include() in reverse order if compared to the
sequence of files to pre-include, i.e. the file to be pre-included first has
to be supplied to force_include() last. The additional flag (second
parameter) has to be true when calling force_include() for the last time
(for the 'first' file). Obviously you need to call force_include() _before_
you dereference the iterator for the first time.

BTW, if you would like to see an example how to implement this, the wave
driver applet has a command line option --forceinclude/-F allowing to
specify one or several files to be pre-included before the actual
preprocessing begins:

    vector<string>::const_reverse_iterator rend = force.rend();
    for (vector<string>::const_reverse_iterator cit = force.rbegin();
         cit != rend; /**/)
        string filename(*cit);
        first.force_include(filename.c_str(), ++cit == rend);

where 'force' is a vector containing the filenames to pre-include and
'first' is the iterator returned from ctx.begin().

Regards Hartmut

Boost list run by bdawes at, gregod at, cpdaniel at, john at