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