Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Wave: a Standard conformant C++ preprocessor library

, 10 Jan 2004
Describes a free and fully Standard conformant C++ preprocessor library
wave_preprocessor_demo.zip
wave.exe
wave_preprocessor_demo1.zip
wave.exe
wave_preprocessor_src.zip
wave
doc
theme
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
uc.gif
u_arr.gif
wave.gif
test
boost-build.jam
cpp_tokens
Jamfile.v2
list_includes
project-root.jam
wave
Jamfile
Jamfile.v2
runtests.sh
test_files
wave
cpplexer
re2clex
cpp.re
test
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
slex
test
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
grammars
idllexer
re2clex
idl.re
util
wave_preprocessor_src1.zip
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
u_arr.gif
uc.gif
wave.gif
boost-build.jam
Jamfile.v2
project-root.jam
Jamfile
Jamfile.v2
runtests.sh
cpp.re
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
/*=============================================================================
    Wave: A Standard compliant C++ preprocessor

    Definition of the preprocessor context
    
    Copyright (c) 2001-2004 Hartmut Kaiser
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to 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)

    See Copyright.txt for full acknowledgements.
=============================================================================*/

#if !defined(CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED)
#define CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED

#include <iterator>
#if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
#include <sstream>
#endif

#include "wave/cpp_exceptions.hpp"
#include "wave/language_support.hpp"
#include "wave/util/file_position.hpp"

///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace iteration_context_policies {

///////////////////////////////////////////////////////////////////////////////
//
//      The iteration_context_policies templates are policies for the 
//      wave::iteration_context which allows to control, how a given input file 
//      is to be represented by a pair of iterators pointing to the begin and 
//      the end of the resulting input sequence.
//
///////////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////
    //
    //  load_file_to_string
    //
    //      Loads a file into a string and returns the iterators pointing to 
    //      the beginning and the end of the loaded string.
    //
    ///////////////////////////////////////////////////////////////////////////
    struct load_file_to_string {
    
        template <typename IterContextT>
        class inner {
        
        public:
            template <typename PositionT>
            static 
            void init_iterators(IterContextT &iter_ctx, 
                PositionT const &act_pos)
            {
                typedef typename IterContextT::iterator_t iterator_t;
                
                std::ifstream instream(iter_ctx.filename.c_str());
                if (!instream.is_open()) {
                    CPP_THROW(preprocess_exception, bad_include_file, 
                        iter_ctx.filename, act_pos);
                }
                instream.unsetf(std::ios::skipws);
                
#if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
            // this is known to be very slow for large files on some systems
                std::copy (istream_iterator<char>(instream),
                    istream_iterator<char>(), 
                    std::inserter(iter_ctx.instring, iter_ctx.instring.end()));
#else
                iter_ctx.instring = std::string(
                    std::istreambuf_iterator<char>(instream.rdbuf()),
                    std::istreambuf_iterator<char>());
#endif // defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)

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

        private:
            std::string instring;
        };
    };
    
///////////////////////////////////////////////////////////////////////////////
//
//  load_file
//
//      The load_file policy opens a given file and returns the wrapped
//      istreambuf_iterators.
//
///////////////////////////////////////////////////////////////////////////////
    struct load_file {
            
        template <typename IterContextT>
        class inner {

        public:
            ~inner() { if (instream.is_open()) instream.close(); }
            
            template <typename PositionT>
            static 
            void init_iterators(IterContextT &iter_ctx, 
                PositionT const &act_pos)
            {
                typedef typename IterContextT::iterator_t iterator_t;
                
                iter_ctx.instream.open(iter_ctx.filename.c_str());
                if (!iter_ctx.instream.is_open()) {
                    CPP_THROW(preprocess_exception, bad_include_file, 
                        iter_ctx.filename, act_pos);
                }
                iter_ctx.instream.unsetf(std::ios::skipws);

                using boost::spirit::make_multi_pass;
                iter_ctx.first = iterator_t(
                    make_multi_pass(std::istreambuf_iterator<char>(
                        iter_ctx.instream.rdbuf())),
                    make_multi_pass(std::istreambuf_iterator<char>()),
                    PositionT(iter_ctx.filename), iter_ctx.language);
                iter_ctx.last = iterator_t();
            }

        private:
            std::ifstream instream;
        };
    };
    
}   // namespace iterattion_context_policies

///////////////////////////////////////////////////////////////////////////////
//  
template <typename IteratorT>
struct base_iteration_context {

public:
    base_iteration_context(WAVE_STRINGTYPE const &fname)   
    :   real_filename(fname), filename(fname), line(1), emitted_lines(1)
    {}
    base_iteration_context(IteratorT const &first_, IteratorT const &last_, 
            WAVE_STRINGTYPE const &fname)
    :   first(first_), last(last_), real_filename(fname), filename(fname), 
        line(1), emitted_lines(1)
    {}

// the actual input stream
    IteratorT first;            // actual input stream position 
    IteratorT last;             // end of input stream
    WAVE_STRINGTYPE real_filename;    // real name of the current file
    WAVE_STRINGTYPE filename;   // actual processed file
    int line;                   // line counter of underlying stream
    int emitted_lines;          // count of emitted newlines
};

///////////////////////////////////////////////////////////////////////////////
//  
template <
    typename IteratorT, 
    typename InputPolicyT = 
        iteration_context_policies::load_file_to_string 
>
struct iteration_context
:   public base_iteration_context<IteratorT>,
    public InputPolicyT::template 
        inner<iteration_context<IteratorT, InputPolicyT> >
{
    typedef IteratorT iterator_t;
    typedef typename IteratorT::token_t::position_t position_t;
    
    typedef iteration_context<IteratorT, InputPolicyT> self_t;
    
    iteration_context(WAVE_STRINGTYPE const &fname, position_t const &act_pos, 
            wave::language_support language_) 
    :   base_iteration_context<IteratorT>(fname), 
        language(language_)
    {
        InputPolicyT::template inner<self_t>::init_iterators(*this, act_pos);
    }
    
    wave::language_support language;
};

///////////////////////////////////////////////////////////////////////////////
}   // namespace wave

#endif // !defined(CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED)

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

Share

About the Author

Hartmut Kaiser

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 11 Jan 2004
Article Copyright 2003 by Hartmut Kaiser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid