Blame boost/wave/cpp_iteration_context.hpp

Packit 58578d
/*=============================================================================
Packit 58578d
    Boost.Wave: A Standard compliant C++ preprocessor library
Packit 58578d
    Definition of the preprocessor context
Packit 58578d
Packit 58578d
    http://www.boost.org/
Packit 58578d
Packit 58578d
    Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
Packit 58578d
    Software License, Version 1.0. (See accompanying file
Packit 58578d
    LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Packit 58578d
=============================================================================*/
Packit 58578d
Packit 58578d
#if !defined(CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED)
Packit 58578d
#define CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED
Packit 58578d
Packit 58578d
#include <iterator>
Packit 58578d
#include <fstream>
Packit 58578d
#if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
Packit 58578d
#include <sstream>
Packit 58578d
#endif
Packit 58578d
Packit 58578d
#include <boost/wave/wave_config.hpp>
Packit 58578d
#include <boost/wave/cpp_exceptions.hpp>
Packit 58578d
#include <boost/wave/language_support.hpp>
Packit 58578d
#include <boost/wave/util/file_position.hpp>
Packit 58578d
// #include <boost/spirit/include/iterator/classic_multi_pass.hpp> // make_multi_pass
Packit 58578d
Packit 58578d
// this must occur after all of the includes and before any code appears
Packit 58578d
#ifdef BOOST_HAS_ABI_HEADERS
Packit 58578d
#include BOOST_ABI_PREFIX
Packit 58578d
#endif
Packit 58578d
Packit 58578d
///////////////////////////////////////////////////////////////////////////////
Packit 58578d
namespace boost {
Packit 58578d
namespace wave {
Packit 58578d
namespace iteration_context_policies {
Packit 58578d
Packit 58578d
///////////////////////////////////////////////////////////////////////////////
Packit 58578d
//
Packit 58578d
//      The iteration_context_policies templates are policies for the
Packit 58578d
//      boost::wave::iteration_context which allows to control, how a given
Packit 58578d
//      input file is to be represented by a pair of iterators pointing to the
Packit 58578d
//      begin and the end of the resulting input sequence.
Packit 58578d
//
Packit 58578d
///////////////////////////////////////////////////////////////////////////////
Packit 58578d
Packit 58578d
    ///////////////////////////////////////////////////////////////////////////
Packit 58578d
    //
Packit 58578d
    //  load_file_to_string
Packit 58578d
    //
Packit 58578d
    //      Loads a file into a string and returns the iterators pointing to
Packit 58578d
    //      the beginning and the end of the loaded string.
Packit 58578d
    //
Packit 58578d
    ///////////////////////////////////////////////////////////////////////////
Packit 58578d
    struct load_file_to_string
Packit 58578d
    {
Packit 58578d
        template <typename IterContextT>
Packit 58578d
        class inner
Packit 58578d
        {
Packit 58578d
        public:
Packit 58578d
            template <typename PositionT>
Packit 58578d
            static void init_iterators(IterContextT &iter_ctx,
Packit 58578d
                PositionT const &act_pos, language_support language)
Packit 58578d
            {
Packit 58578d
                typedef typename IterContextT::iterator_type iterator_type;
Packit 58578d
Packit 58578d
                // read in the file
Packit 58578d
                std::ifstream instream(iter_ctx.filename.c_str());
Packit 58578d
                if (!instream.is_open()) {
Packit 58578d
                    BOOST_WAVE_THROW_CTX(iter_ctx.ctx, preprocess_exception,
Packit 58578d
                        bad_include_file, iter_ctx.filename.c_str(), act_pos);
Packit 58578d
                    return;
Packit 58578d
                }
Packit 58578d
                instream.unsetf(std::ios::skipws);
Packit 58578d
Packit 58578d
                iter_ctx.instring.assign(
Packit 58578d
                    std::istreambuf_iterator<char>(instream.rdbuf()),
Packit 58578d
                    std::istreambuf_iterator<char>());
Packit 58578d
Packit 58578d
                iter_ctx.first = iterator_type(
Packit 58578d
                    iter_ctx.instring.begin(), iter_ctx.instring.end(),
Packit 58578d
                    PositionT(iter_ctx.filename), language);
Packit 58578d
                iter_ctx.last = iterator_type();
Packit 58578d
            }
Packit 58578d
Packit 58578d
        private:
Packit 58578d
            std::string instring;
Packit 58578d
        };
Packit 58578d
    };
Packit 58578d
Packit 58578d
}   // namespace iteration_context_policies
Packit 58578d
Packit 58578d
///////////////////////////////////////////////////////////////////////////////
Packit 58578d
//  Base class for iteration contexts
Packit 58578d
template <typename ContextT, typename IteratorT>
Packit 58578d
struct base_iteration_context
Packit 58578d
{
Packit 58578d
    enum file_type
Packit 58578d
    {
Packit 58578d
        // this iteration context handles ...
Packit 58578d
        main_file,      // ... the main preprocessed file
Packit 58578d
        system_header,  // ... a header file included used #include  <>
Packit 58578d
        user_header     // ... a header file included using #include ""
Packit 58578d
    };
Packit 58578d
Packit 58578d
    base_iteration_context(ContextT& ctx_,
Packit 58578d
            BOOST_WAVE_STRINGTYPE const &fname, std::size_t if_block_depth = 0)
Packit 58578d
    :   real_filename(fname), real_relative_filename(fname), filename(fname),
Packit 58578d
        line(1), emitted_lines(0), if_block_depth(if_block_depth), ctx(ctx_),
Packit 58578d
        type(main_file)
Packit 58578d
    {}
Packit 58578d
    base_iteration_context(ContextT& ctx_,
Packit 58578d
            IteratorT const &first_, IteratorT const &last_,
Packit 58578d
            BOOST_WAVE_STRINGTYPE const &fname, std::size_t if_block_depth = 0,
Packit 58578d
            file_type type_ = main_file)
Packit 58578d
    :   first(first_), last(last_), real_filename(fname),
Packit 58578d
        real_relative_filename(fname), filename(fname),
Packit 58578d
        line(1), emitted_lines(0), if_block_depth(if_block_depth), ctx(ctx_),
Packit 58578d
        type(type_)
Packit 58578d
    {}
Packit 58578d
Packit 58578d
// the actual input stream
Packit 58578d
    IteratorT first;            // actual input stream position
Packit 58578d
    IteratorT last;             // end of input stream
Packit 58578d
    BOOST_WAVE_STRINGTYPE real_filename;  // real name of the current file
Packit 58578d
    BOOST_WAVE_STRINGTYPE real_relative_filename;  // real relative name of the current file
Packit 58578d
    BOOST_WAVE_STRINGTYPE filename;       // actual processed file
Packit 58578d
    std::size_t line;                     // line counter of underlying stream
Packit 58578d
    std::size_t emitted_lines;            // count of emitted newlines
Packit 58578d
    std::size_t if_block_depth; // depth of #if block recursion
Packit 58578d
    ContextT& ctx;              // corresponding context<> object
Packit 58578d
    file_type type;             // the type of the handled file
Packit 58578d
};
Packit 58578d
Packit 58578d
///////////////////////////////////////////////////////////////////////////////
Packit 58578d
//
Packit 58578d
template <
Packit 58578d
    typename ContextT, typename IteratorT,
Packit 58578d
    typename InputPolicyT = typename ContextT::input_policy_type
Packit 58578d
>
Packit 58578d
struct iteration_context
Packit 58578d
:   public base_iteration_context<ContextT, IteratorT>,
Packit 58578d
    public InputPolicyT::template
Packit 58578d
        inner<iteration_context<ContextT, IteratorT, InputPolicyT> >
Packit 58578d
{
Packit 58578d
    typedef IteratorT iterator_type;
Packit 58578d
    typedef typename IteratorT::token_type::position_type position_type;
Packit 58578d
Packit 58578d
    typedef base_iteration_context<ContextT, IteratorT> base_type;
Packit 58578d
    typedef iteration_context<ContextT, IteratorT, InputPolicyT> self_type;
Packit 58578d
Packit 58578d
    iteration_context(ContextT& ctx, BOOST_WAVE_STRINGTYPE const &fname,
Packit 58578d
            position_type const &act_pos,
Packit 58578d
            boost::wave::language_support language_,
Packit 58578d
            typename base_type::file_type type = base_type::main_file)
Packit 58578d
    :   base_iteration_context<ContextT, IteratorT>(ctx, fname, type)
Packit 58578d
    {
Packit 58578d
        InputPolicyT::template inner<self_type>::init_iterators(
Packit 58578d
            *this, act_pos, language_);
Packit 58578d
    }
Packit 58578d
};
Packit 58578d
Packit 58578d
///////////////////////////////////////////////////////////////////////////////
Packit 58578d
}   // namespace wave
Packit 58578d
}   // namespace boost
Packit 58578d
Packit 58578d
// the suffix header occurs after all of the code
Packit 58578d
#ifdef BOOST_HAS_ABI_HEADERS
Packit 58578d
#include BOOST_ABI_SUFFIX
Packit 58578d
#endif
Packit 58578d
Packit 58578d
#endif // !defined(CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED)