]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef BOOST_METAPARSE_V1_STRING_HPP |
2 | #define BOOST_METAPARSE_V1_STRING_HPP | |
3 | ||
4 | // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012. | |
5 | // Distributed under the Boost Software License, Version 1.0. | |
6 | // (See accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | #include <boost/metaparse/config.hpp> | |
10 | #include <boost/metaparse/v1/fwd/string.hpp> | |
11 | #include <boost/metaparse/v1/string_tag.hpp> | |
12 | #include <boost/metaparse/v1/impl/string_iterator.hpp> | |
13 | #include <boost/metaparse/v1/impl/empty_string.hpp> | |
14 | #include <boost/metaparse/v1/impl/size.hpp> | |
15 | #include <boost/metaparse/v1/impl/pop_front.hpp> | |
16 | #include <boost/metaparse/v1/impl/push_front_c.hpp> | |
17 | #include <boost/metaparse/v1/impl/push_back_c.hpp> | |
18 | #include <boost/metaparse/v1/impl/pop_back.hpp> | |
19 | #include <boost/metaparse/v1/impl/assert_string_length.hpp> | |
20 | #include <boost/metaparse/v1/impl/string_at.hpp> | |
21 | ||
22 | #include <boost/preprocessor/arithmetic/sub.hpp> | |
23 | #include <boost/preprocessor/punctuation/comma_if.hpp> | |
24 | #include <boost/preprocessor/repetition/enum.hpp> | |
25 | #include <boost/preprocessor/repetition/enum_params.hpp> | |
26 | #include <boost/preprocessor/repetition/repeat_from_to.hpp> | |
27 | #include <boost/preprocessor/tuple/eat.hpp> | |
28 | ||
29 | #include <boost/type_traits/is_same.hpp> | |
30 | ||
31 | /* | |
32 | * The string type | |
33 | */ | |
34 | ||
35 | namespace boost | |
36 | { | |
37 | namespace metaparse | |
38 | { | |
39 | namespace v1 | |
40 | { | |
41 | #ifdef BOOST_METAPARSE_VARIADIC_STRING | |
42 | template <char... Cs> | |
43 | struct string | |
44 | { | |
45 | typedef string type; | |
46 | typedef string_tag tag; | |
47 | }; | |
48 | #else | |
49 | template <BOOST_PP_ENUM_PARAMS(BOOST_METAPARSE_LIMIT_STRING_SIZE, int C)> | |
50 | struct string | |
51 | { | |
52 | typedef string type; | |
53 | typedef string_tag tag; | |
54 | }; | |
55 | #endif | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | /* | |
61 | * Boost.MPL overloads | |
62 | */ | |
63 | ||
64 | namespace boost | |
65 | { | |
66 | namespace mpl | |
67 | { | |
68 | // push_back | |
69 | template <class S> | |
70 | struct push_back_impl; | |
71 | ||
72 | template <> | |
73 | struct push_back_impl<boost::metaparse::v1::string_tag> | |
74 | { | |
75 | typedef push_back_impl type; | |
76 | ||
77 | template <class S, class C> | |
78 | struct apply : | |
79 | boost::metaparse::v1::impl::push_back_c< | |
80 | typename S::type, | |
81 | C::type::value | |
82 | > | |
83 | {}; | |
84 | }; | |
85 | ||
86 | // pop_back | |
87 | template <class S> | |
88 | struct pop_back_impl; | |
89 | ||
90 | template <> | |
91 | struct pop_back_impl<boost::metaparse::v1::string_tag> | |
92 | { | |
93 | typedef pop_back_impl type; | |
94 | ||
95 | template <class S> | |
96 | struct apply : boost::metaparse::v1::impl::pop_back<S> {}; | |
97 | }; | |
98 | ||
99 | // push_front | |
100 | template <class S> | |
101 | struct push_front_impl; | |
102 | ||
103 | template <> | |
104 | struct push_front_impl<boost::metaparse::v1::string_tag> | |
105 | { | |
106 | typedef push_front_impl type; | |
107 | ||
108 | template <class S, class C> | |
109 | struct apply : | |
110 | boost::metaparse::v1::impl::push_front_c< | |
111 | typename S::type, | |
112 | C::type::value | |
113 | > | |
114 | {}; | |
115 | }; | |
116 | ||
117 | // pop_front | |
118 | template <class S> | |
119 | struct pop_front_impl; | |
120 | ||
121 | template <> | |
122 | struct pop_front_impl<boost::metaparse::v1::string_tag> | |
123 | { | |
124 | typedef pop_front_impl type; | |
125 | ||
126 | template <class S> | |
127 | struct apply : boost::metaparse::v1::impl::pop_front<S> {}; | |
128 | }; | |
129 | ||
130 | // clear | |
131 | template <class S> | |
132 | struct clear_impl; | |
133 | ||
134 | template <> | |
135 | struct clear_impl<boost::metaparse::v1::string_tag> | |
136 | { | |
137 | typedef clear_impl type; | |
138 | ||
139 | template <class S> | |
140 | struct apply : boost::metaparse::v1::string<> {}; | |
141 | }; | |
142 | ||
143 | // begin | |
144 | template <class S> | |
145 | struct begin_impl; | |
146 | ||
147 | template <> | |
148 | struct begin_impl<boost::metaparse::v1::string_tag> | |
149 | { | |
150 | typedef begin_impl type; | |
151 | ||
152 | template <class S> | |
153 | struct apply : | |
154 | boost::metaparse::v1::impl::string_iterator<typename S::type, 0> | |
155 | {}; | |
156 | }; | |
157 | ||
158 | // end | |
159 | template <class S> | |
160 | struct end_impl; | |
161 | ||
162 | template <> | |
163 | struct end_impl<boost::metaparse::v1::string_tag> | |
164 | { | |
165 | typedef end_impl type; | |
166 | ||
167 | template <class S> | |
168 | struct apply : | |
169 | boost::metaparse::v1::impl::string_iterator< | |
170 | typename S::type, | |
171 | boost::metaparse::v1::impl::size<typename S::type>::type::value | |
172 | > | |
173 | {}; | |
174 | }; | |
175 | ||
176 | // equal_to | |
177 | template <class A, class B> | |
178 | struct equal_to_impl; | |
179 | ||
180 | template <> | |
181 | struct equal_to_impl< | |
182 | boost::metaparse::v1::string_tag, | |
183 | boost::metaparse::v1::string_tag | |
184 | > | |
185 | { | |
186 | typedef equal_to_impl type; | |
187 | ||
188 | template <class A, class B> | |
189 | struct apply : boost::is_same<typename A::type, typename B::type> {}; | |
190 | }; | |
191 | ||
192 | template <class T> | |
193 | struct equal_to_impl<boost::metaparse::v1::string_tag, T> | |
194 | { | |
195 | typedef equal_to_impl type; | |
196 | ||
197 | template <class, class> | |
198 | struct apply : false_ {}; | |
199 | }; | |
200 | ||
201 | template <class T> | |
202 | struct equal_to_impl<T, boost::metaparse::v1::string_tag> : | |
203 | equal_to_impl<boost::metaparse::v1::string_tag, T> | |
204 | {}; | |
205 | ||
206 | // c_str | |
207 | template <class S> | |
208 | struct c_str; | |
209 | ||
210 | #ifdef BOOST_METAPARSE_VARIADIC_STRING | |
211 | template <char... Cs> | |
212 | struct c_str<boost::metaparse::v1::string<Cs...>> | |
213 | { | |
214 | typedef c_str type; | |
215 | #ifdef BOOST_NO_CONSTEXPR_C_STR | |
216 | static const char value[sizeof...(Cs) + 1]; | |
217 | #else | |
218 | static constexpr char value[sizeof...(Cs) + 1] = {Cs..., 0}; | |
219 | #endif | |
220 | }; | |
221 | ||
222 | template <> | |
223 | struct c_str<boost::metaparse::v1::string<>> : | |
224 | boost::metaparse::v1::impl::empty_string<> | |
225 | {}; | |
226 | ||
227 | #ifdef BOOST_NO_CONSTEXPR_C_STR | |
228 | template <char... Cs> | |
229 | const char c_str<boost::metaparse::v1::string<Cs...>>::value[] | |
230 | = {Cs..., 0}; | |
231 | #else | |
232 | template <char... Cs> | |
233 | constexpr char c_str<boost::metaparse::v1::string<Cs...>>::value[]; | |
234 | #endif | |
235 | ||
236 | #else | |
237 | template <BOOST_PP_ENUM_PARAMS(BOOST_METAPARSE_LIMIT_STRING_SIZE, int C)> | |
238 | struct c_str< | |
239 | boost::metaparse::v1::string< | |
240 | BOOST_PP_ENUM_PARAMS(BOOST_METAPARSE_LIMIT_STRING_SIZE, C) | |
241 | > | |
242 | > | |
243 | { | |
244 | typedef c_str type; | |
245 | static BOOST_CONSTEXPR const char | |
246 | value[BOOST_METAPARSE_LIMIT_STRING_SIZE + 1] | |
247 | #if !defined BOOST_NO_CONSTEXPR && !defined BOOST_NO_CXX11_CONSTEXPR | |
248 | = {BOOST_PP_ENUM_PARAMS(BOOST_METAPARSE_LIMIT_STRING_SIZE, C), 0} | |
249 | #endif | |
250 | ; | |
251 | }; | |
252 | ||
253 | template <BOOST_PP_ENUM_PARAMS(BOOST_METAPARSE_LIMIT_STRING_SIZE, int C)> | |
254 | BOOST_CONSTEXPR const char | |
255 | c_str< | |
256 | boost::metaparse::v1::string< | |
257 | BOOST_PP_ENUM_PARAMS(BOOST_METAPARSE_LIMIT_STRING_SIZE, C) | |
258 | > | |
259 | >::value[BOOST_METAPARSE_LIMIT_STRING_SIZE + 1] | |
260 | #if defined BOOST_NO_CONSTEXPR || defined BOOST_NO_CXX11_CONSTEXPR | |
261 | = {BOOST_PP_ENUM_PARAMS(BOOST_METAPARSE_LIMIT_STRING_SIZE, C), 0} | |
262 | #endif | |
263 | ; | |
264 | ||
265 | template <> | |
266 | struct c_str<boost::metaparse::v1::string<> > : | |
267 | boost::metaparse::v1::impl::empty_string<> | |
268 | { | |
269 | typedef c_str type; | |
270 | }; | |
271 | ||
272 | #ifdef BOOST_METAPARSE_DEF | |
273 | # error BOOST_METAPARSE_DEF already defined | |
274 | #endif | |
275 | #define BOOST_METAPARSE_DEF(n) \ | |
276 | = {BOOST_PP_ENUM_PARAMS(n, C) BOOST_PP_COMMA_IF(n) 0} | |
277 | ||
278 | #ifdef BOOST_METAPARSE_STRING_DECLARE | |
279 | # error BOOST_METAPARSE_STRING_DECLARE already defined | |
280 | #endif | |
281 | #ifdef BOOST_METAPARSE_STRING_DEFINE | |
282 | # error BOOST_METAPARSE_STRING_DECLARE already defined | |
283 | #endif | |
284 | #if !defined BOOST_NO_CONSTEXPR && !defined BOOST_NO_CXX11_CONSTEXPR | |
285 | # define BOOST_METAPARSE_STRING_DECLARE(n) BOOST_METAPARSE_DEF(n) | |
286 | # define BOOST_METAPARSE_STRING_DEFINE(n) | |
287 | #else | |
288 | # define BOOST_METAPARSE_STRING_DECLARE(n) | |
289 | # define BOOST_METAPARSE_STRING_DEFINE(n) BOOST_METAPARSE_DEF(n) | |
290 | #endif | |
291 | ||
292 | #ifdef BOOST_METAPARSE_STRING_CASE | |
293 | # error BOOST_METAPARSE_STRING_CASE is already defined | |
294 | #endif | |
295 | #define BOOST_METAPARSE_STRING_CASE(z, n, unused) \ | |
296 | template <BOOST_PP_ENUM_PARAMS(n, int C)> \ | |
297 | struct \ | |
298 | c_str< \ | |
299 | boost::metaparse::v1::string< \ | |
300 | BOOST_PP_ENUM_PARAMS(n, C) BOOST_PP_COMMA_IF(n) \ | |
301 | BOOST_PP_ENUM( \ | |
302 | BOOST_PP_SUB(BOOST_METAPARSE_LIMIT_STRING_SIZE, n), \ | |
303 | BOOST_NO_CHAR BOOST_PP_TUPLE_EAT(3), \ | |
304 | ~ \ | |
305 | ) \ | |
306 | > \ | |
307 | > \ | |
308 | { \ | |
309 | typedef c_str type; \ | |
310 | static BOOST_CONSTEXPR const char value[n + 1] \ | |
311 | BOOST_METAPARSE_STRING_DECLARE(n); \ | |
312 | }; \ | |
313 | \ | |
314 | template <BOOST_PP_ENUM_PARAMS(n, int C)> \ | |
315 | BOOST_CONSTEXPR const char c_str< \ | |
316 | boost::metaparse::v1::string< \ | |
317 | BOOST_PP_ENUM_PARAMS(n, C) BOOST_PP_COMMA_IF(n) \ | |
318 | BOOST_PP_ENUM( \ | |
319 | BOOST_PP_SUB(BOOST_METAPARSE_LIMIT_STRING_SIZE, n), \ | |
320 | BOOST_NO_CHAR BOOST_PP_TUPLE_EAT(3), \ | |
321 | ~ \ | |
322 | ) \ | |
323 | > \ | |
324 | >::value[n + 1] BOOST_METAPARSE_STRING_DEFINE(n); | |
325 | ||
326 | BOOST_PP_REPEAT_FROM_TO( | |
327 | 1, | |
328 | BOOST_METAPARSE_LIMIT_STRING_SIZE, | |
329 | BOOST_METAPARSE_STRING_CASE, | |
330 | ~ | |
331 | ) | |
332 | ||
333 | #undef BOOST_METAPARSE_STRING_CASE | |
334 | #undef BOOST_METAPARSE_STRING_DECLARE | |
335 | #undef BOOST_METAPARSE_STRING_DEFINE | |
336 | #undef BOOST_METAPARSE_DEF | |
337 | #endif | |
338 | } | |
339 | } | |
340 | ||
341 | #include <boost/metaparse/v1/impl/remove_trailing_no_chars.hpp> | |
342 | ||
343 | #if \ | |
344 | defined BOOST_METAPARSE_VARIADIC_STRING \ | |
345 | && !defined BOOST_NO_CONSTEXPR && !defined BOOST_NO_CXX11_CONSTEXPR | |
346 | ||
347 | # include <boost/metaparse/v1/impl/string.hpp> | |
348 | ||
349 | #else | |
350 | ||
351 | # define BOOST_METAPARSE_V1_CONFIG_NO_BOOST_METAPARSE_STRING 1 | |
352 | ||
353 | #endif | |
354 | ||
355 | #endif | |
356 |