1 /*=============================================================================
2     Boost.Wave: A Standard compliant C++ preprocessor library
3     Definition of the preprocessor context
4 
5     http://www.boost.org/
6 
7     Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
8     Software License, Version 1.0. (See accompanying file
9     LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
11 
12 #if !defined(CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED)
13 #define CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED
14 
15 #include <iterator>
16 #include <fstream>
17 #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
18 #include <sstream>
19 #endif
20 
21 #include <boost/wave/wave_config.hpp>
22 #include <boost/wave/cpp_exceptions.hpp>
23 #include <boost/wave/language_support.hpp>
24 #include <boost/wave/util/file_position.hpp>
25 // #include <boost/spirit/include/iterator/classic_multi_pass.hpp> // make_multi_pass
26 
27 // this must occur after all of the includes and before any code appears
28 #ifdef BOOST_HAS_ABI_HEADERS
29 #include BOOST_ABI_PREFIX
30 #endif
31 
32 ///////////////////////////////////////////////////////////////////////////////
33 namespace boost {
34 namespace wave {
35 namespace iteration_context_policies {
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 //      The iteration_context_policies templates are policies for the
40 //      boost::wave::iteration_context which allows to control, how a given
41 //      input file is to be represented by a pair of iterators pointing to the
42 //      begin and the end of the resulting input sequence.
43 //
44 ///////////////////////////////////////////////////////////////////////////////
45 
46     ///////////////////////////////////////////////////////////////////////////
47     //
48     //  load_file_to_string
49     //
50     //      Loads a file into a string and returns the iterators pointing to
51     //      the beginning and the end of the loaded string.
52     //
53     ///////////////////////////////////////////////////////////////////////////
54     struct load_file_to_string
55     {
56         template <typename IterContextT>
57         class inner
58         {
59         public:
60             template <typename PositionT>
init_iterators(IterContextT & iter_ctx,PositionT const & act_pos,language_support language)61             static void init_iterators(IterContextT &iter_ctx,
62                 PositionT const &act_pos, language_support language)
63             {
64                 typedef typename IterContextT::iterator_type iterator_type;
65 
66                 // read in the file
67                 std::ifstream instream(iter_ctx.filename.c_str());
68                 if (!instream.is_open()) {
69                     BOOST_WAVE_THROW_CTX(iter_ctx.ctx, preprocess_exception,
70                         bad_include_file, iter_ctx.filename.c_str(), act_pos);
71                     return;
72                 }
73                 instream.unsetf(std::ios::skipws);
74 
75                 iter_ctx.instring.assign(
76                     std::istreambuf_iterator<char>(instream.rdbuf()),
77                     std::istreambuf_iterator<char>());
78 
79                 iter_ctx.first = iterator_type(
80                     iter_ctx.instring.begin(), iter_ctx.instring.end(),
81                     PositionT(iter_ctx.filename), language);
82                 iter_ctx.last = iterator_type();
83             }
84 
85         private:
86             std::string instring;
87         };
88     };
89 
90 }   // namespace iteration_context_policies
91 
92 ///////////////////////////////////////////////////////////////////////////////
93 //  Base class for iteration contexts
94 template <typename ContextT, typename IteratorT>
95 struct base_iteration_context
96 {
97     enum file_type
98     {
99         // this iteration context handles ...
100         main_file,      // ... the main preprocessed file
101         system_header,  // ... a header file included used #include  <>
102         user_header     // ... a header file included using #include ""
103     };
104 
base_iteration_contextboost::wave::base_iteration_context105     base_iteration_context(ContextT& ctx_,
106             BOOST_WAVE_STRINGTYPE const &fname, std::size_t if_block_depth = 0)
107     :   real_filename(fname), real_relative_filename(fname), filename(fname),
108         line(1), emitted_lines(0), if_block_depth(if_block_depth), ctx(ctx_),
109         type(main_file)
110     {}
base_iteration_contextboost::wave::base_iteration_context111     base_iteration_context(ContextT& ctx_,
112             IteratorT const &first_, IteratorT const &last_,
113             BOOST_WAVE_STRINGTYPE const &fname, std::size_t if_block_depth = 0,
114             file_type type_ = main_file)
115     :   first(first_), last(last_), real_filename(fname),
116         real_relative_filename(fname), filename(fname),
117         line(1), emitted_lines(0), if_block_depth(if_block_depth), ctx(ctx_),
118         type(type_)
119     {}
120 
121 // the actual input stream
122     IteratorT first;            // actual input stream position
123     IteratorT last;             // end of input stream
124     BOOST_WAVE_STRINGTYPE real_filename;  // real name of the current file
125     BOOST_WAVE_STRINGTYPE real_relative_filename;  // real relative name of the current file
126     BOOST_WAVE_STRINGTYPE filename;       // actual processed file
127     std::size_t line;                     // line counter of underlying stream
128     std::size_t emitted_lines;            // count of emitted newlines
129     std::size_t if_block_depth; // depth of #if block recursion
130     ContextT& ctx;              // corresponding context<> object
131     file_type type;             // the type of the handled file
132 };
133 
134 ///////////////////////////////////////////////////////////////////////////////
135 //
136 template <
137     typename ContextT, typename IteratorT,
138     typename InputPolicyT = typename ContextT::input_policy_type
139 >
140 struct iteration_context
141 :   public base_iteration_context<ContextT, IteratorT>,
142     public InputPolicyT::template
143         inner<iteration_context<ContextT, IteratorT, InputPolicyT> >
144 {
145     typedef IteratorT iterator_type;
146     typedef typename IteratorT::token_type::position_type position_type;
147 
148     typedef base_iteration_context<ContextT, IteratorT> base_type;
149     typedef iteration_context<ContextT, IteratorT, InputPolicyT> self_type;
150 
iteration_contextboost::wave::iteration_context151     iteration_context(ContextT& ctx, BOOST_WAVE_STRINGTYPE const &fname,
152             position_type const &act_pos,
153             boost::wave::language_support language_,
154             typename base_type::file_type type = base_type::main_file)
155     :   base_iteration_context<ContextT, IteratorT>(ctx, fname, type)
156     {
157         InputPolicyT::template inner<self_type>::init_iterators(
158             *this, act_pos, language_);
159     }
160 };
161 
162 ///////////////////////////////////////////////////////////////////////////////
163 }   // namespace wave
164 }   // namespace boost
165 
166 // the suffix header occurs after all of the code
167 #ifdef BOOST_HAS_ABI_HEADERS
168 #include BOOST_ABI_SUFFIX
169 #endif
170 
171 #endif // !defined(CPP_ITERATION_CONTEXT_HPP_00312288_9DDB_4668_AFE5_25D3994FD095_INCLUDED)
172