]> git.lyx.org Git - features.git/blob - boost/boost/mpl/string.hpp
boost: add eol property
[features.git] / boost / boost / mpl / string.hpp
1
2 #ifndef BOOST_MPL_STRING_HPP_INCLUDED
3 #define BOOST_MPL_STRING_HPP_INCLUDED
4
5 // Copyright Eric Niebler 2009
6 //
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // See http://www.boost.org/libs/mpl for documentation.
12
13 // $Id: string.hpp 49239 2009-04-01 09:10:26Z eric_niebler $
14 // $Date: 2009-04-01 02:10:26 -0700 (Wed, 1 Apr 2009) $
15 // $Revision: 49239 $
16 //
17 // Thanks to:
18 //   Dmitry Goncharov for porting this to the Sun compiler
19
20 #include <boost/config.hpp>
21 #include <boost/detail/workaround.hpp>
22 #include <boost/detail/endian.hpp>
23 #include <boost/mpl/limits/string.hpp>
24 #include <boost/mpl/if.hpp>
25 #include <boost/mpl/char.hpp>
26 #include <boost/mpl/copy.hpp>
27 #include <boost/mpl/size.hpp>
28 #include <boost/mpl/empty.hpp>
29 #include <boost/mpl/assert.hpp>
30 #include <boost/mpl/size_t.hpp>
31 #include <boost/mpl/begin_end.hpp>
32 #include <boost/mpl/joint_view.hpp>
33 #include <boost/mpl/insert_range.hpp>
34 #include <boost/mpl/back_inserter.hpp>
35 #include <boost/mpl/front_inserter.hpp>
36 #include <boost/mpl/iterator_range.hpp>
37 #include <boost/preprocessor/arithmetic/dec.hpp>
38 #include <boost/preprocessor/arithmetic/add.hpp>
39 #include <boost/preprocessor/arithmetic/div.hpp>
40 #include <boost/preprocessor/punctuation/comma_if.hpp>
41 #include <boost/preprocessor/repetition/repeat.hpp>
42 #include <boost/preprocessor/repetition/enum_params.hpp>
43 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
44 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
45 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
46 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
47
48 #include <iterator> // for bidirectional_iterator_tag
49 #include <climits>
50
51 namespace boost { namespace mpl
52 {
53     #define BOOST_MPL_STRING_MAX_PARAMS                                                             \
54       BOOST_PP_DIV(BOOST_PP_ADD(BOOST_MPL_LIMIT_STRING_SIZE, 3), 4)
55
56     // Low-level bit-twiddling is done by macros. Any implementation-defined behavior of
57     // multi-character literals should be localized to these macros.
58
59     #define BOOST_MPL_MULTICHAR_LENGTH(c)                                                           \
60       (std::size_t)((c<CHAR_MIN) ? 4 : ((c>0xffffff)+(c>0xffff)+(c>0xff)+1))
61
62     #if defined(BOOST_LITTLE_ENDIAN) && defined(__SUNPRO_CC)
63
64         #define BOOST_MPL_MULTICHAR_AT(c,i)                                                         \
65           (char)(0xff&((unsigned)(c)>>(8*(std::size_t)(i))))
66
67         #define BOOST_MPL_MULTICHAR_PUSH_BACK(c,i)                                                  \
68           ((((unsigned char)(i))<<(BOOST_MPL_MULTICHAR_LENGTH(c)*8))|(unsigned)(c))
69
70         #define BOOST_MPL_MULTICHAR_PUSH_FRONT(c,i)                                                 \
71           (((unsigned)(c)<<8)|(unsigned char)(i))
72
73         #define BOOST_MPL_MULTICHAR_POP_BACK(c)                                                     \
74           (((1<<((BOOST_MPL_MULTICHAR_LENGTH(c)-1)*8))-1)&(unsigned)(c))
75
76         #define BOOST_MPL_MULTICHAR_POP_FRONT(c)                                                    \
77           ((unsigned)(c)>>8)
78
79     #else
80
81         #define BOOST_MPL_MULTICHAR_AT(c,i)                                                         \
82           (char)(0xff&((unsigned)(c)>>(8*(BOOST_MPL_MULTICHAR_LENGTH(c)-(std::size_t)(i)-1))))
83
84         #define BOOST_MPL_MULTICHAR_PUSH_BACK(c,i)                                                  \
85           (((unsigned)(c)<<8)|(unsigned char)(i))
86
87         #define BOOST_MPL_MULTICHAR_PUSH_FRONT(c,i)                                                 \
88           ((((unsigned char)(i))<<(BOOST_MPL_MULTICHAR_LENGTH(c)*8))|(unsigned)(c))
89
90         #define BOOST_MPL_MULTICHAR_POP_BACK(c)                                                     \
91           ((unsigned)(c)>>8)
92
93         #define BOOST_MPL_MULTICHAR_POP_FRONT(c)                                                    \
94           (((1<<((BOOST_MPL_MULTICHAR_LENGTH(c)-1)*8))-1)&(unsigned)(c))
95
96     #endif
97
98     struct string_tag;
99     struct string_iterator_tag;
100
101     template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_MPL_STRING_MAX_PARAMS, int C, 0)>
102     struct string;
103
104     template<typename Sequence, int I, int J>
105     struct string_iterator;
106
107     template<typename Sequence>
108     struct sequence_tag;
109
110     template<typename Tag>
111     struct size_impl;
112
113     template<>
114     struct size_impl<mpl::string_tag>
115     {
116         template<typename Sequence>
117         struct apply;
118
119         #define M0(z, n, data)                                                                      \
120         + BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C,n))
121
122         #define M1(z, n, data)                                                                      \
123         template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)>                                               \
124         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)> >                                 \
125           : mpl::size_t<(0 BOOST_PP_REPEAT_ ## z(n, M0, ~))>                                        \
126         {};
127
128         BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_MPL_STRING_MAX_PARAMS), M1, ~)
129         #undef M0
130         #undef M1
131     };
132
133     template<>
134     struct size_impl<mpl::string_tag>::apply<mpl::string<> >
135       : mpl::size_t<0>
136     {};
137
138     template<typename Tag>
139     struct begin_impl;
140
141     template<>
142     struct begin_impl<mpl::string_tag>
143     {
144         template<typename Sequence>
145         struct apply
146         {
147             typedef mpl::string_iterator<Sequence, 0, 0> type;
148         };
149     };
150
151     template<typename Tag>
152     struct end_impl;
153
154     template<>
155     struct end_impl<mpl::string_tag>
156     {
157         template<typename Sequence>
158         struct apply;
159
160         #define M0(z,n,data)                                                                        \
161         template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)>                                               \
162         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)> >                                 \
163         {                                                                                           \
164             typedef mpl::string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, n, 0> type;  \
165         };
166
167         BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_MPL_STRING_MAX_PARAMS), M0, ~)
168         #undef M0
169     };
170
171     template<>
172     struct end_impl<mpl::string_tag>::apply<mpl::string<> >
173     {
174         typedef mpl::string_iterator<mpl::string<>, 0, 0> type;
175     };
176
177     template<typename Tag>
178     struct push_back_impl;
179
180     template<>
181     struct push_back_impl<mpl::string_tag>
182     {
183         template<typename Sequence, typename Value, bool B = (4==BOOST_MPL_MULTICHAR_LENGTH(Sequence::back_))>
184         struct apply
185         {
186             BOOST_MPL_ASSERT_MSG(
187                 (BOOST_MPL_LIMIT_STRING_SIZE != mpl::size<Sequence>::type::value)
188               , PUSH_BACK_FAILED_MPL_STRING_IS_FULL
189               , (Sequence)
190             );
191             // If the above assertion didn't fire, then the string is sparse.
192             // Repack the string and retry the push_back
193             typedef
194                 typename mpl::push_back<
195                     typename mpl::copy<
196                         Sequence
197                       , mpl::back_inserter<mpl::string<> >
198                     >::type
199                   , Value
200                 >::type
201             type;
202         };
203
204         template<typename Value>
205         struct apply<mpl::string<>, Value, false>
206         {
207             typedef mpl::string<(char)Value::value> type;
208         };
209
210         #define M0(z,n,data)                                                                        \
211         template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C), typename Value>                               \
212         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, Value, false>                    \
213         {                                                                                           \
214             typedef                                                                                 \
215                 mpl::string<                                                                        \
216                     BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_DEC(n), C)                                   \
217                     BOOST_PP_COMMA_IF(BOOST_PP_DEC(n))                                              \
218                     ((unsigned)BOOST_PP_CAT(C,BOOST_PP_DEC(n))>0xffffff)                            \
219                     ?BOOST_PP_CAT(C,BOOST_PP_DEC(n))                                                \
220                     :BOOST_MPL_MULTICHAR_PUSH_BACK(BOOST_PP_CAT(C,BOOST_PP_DEC(n)), Value::value)   \
221                   , ((unsigned)BOOST_PP_CAT(C,BOOST_PP_DEC(n))>0xffffff)                            \
222                     ?(char)Value::value                                                             \
223                     :0                                                                              \
224                 >                                                                                   \
225             type;                                                                                   \
226         };
227
228         BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
229         #undef M0
230
231         template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), typename Value>
232         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, Value, false>
233         {
234             typedef
235                 mpl::string<
236                     BOOST_PP_ENUM_PARAMS(BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS), C)
237                   , BOOST_MPL_MULTICHAR_PUSH_BACK(BOOST_PP_CAT(C,BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS)), Value::value)
238                 >
239             type;
240         };
241     };
242
243     template<typename Tag>
244     struct pop_back_impl;
245
246     template<>
247     struct pop_back_impl<mpl::string_tag>
248     {
249         template<typename Sequence>
250         struct apply;
251
252         #define M0(z,n,data)                                                                        \
253         template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)>                                               \
254         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)> >                                 \
255         {                                                                                           \
256             BOOST_MPL_ASSERT_MSG((C0 != 0), POP_BACK_FAILED_MPL_STRING_IS_EMPTY, (mpl::string<>));  \
257             typedef                                                                                 \
258                 mpl::string<                                                                        \
259                     BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_DEC(n), C)                                   \
260                     BOOST_PP_COMMA_IF(BOOST_PP_DEC(n))                                              \
261                     BOOST_MPL_MULTICHAR_POP_BACK(BOOST_PP_CAT(C,BOOST_PP_DEC(n)))                   \
262                 >                                                                                   \
263             type;                                                                                   \
264         };
265
266         BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_MPL_STRING_MAX_PARAMS), M0, ~)
267         #undef M0
268     };
269
270     template<typename Tag>
271     struct push_front_impl;
272
273     template<>
274     struct push_front_impl<mpl::string_tag>
275     {
276         template<typename Sequence, typename Value, bool B = (4==BOOST_MPL_MULTICHAR_LENGTH(Sequence::front_))>
277         struct apply
278         {
279             BOOST_MPL_ASSERT_MSG(
280                 (BOOST_MPL_LIMIT_STRING_SIZE != mpl::size<Sequence>::type::value)
281               , PUSH_FRONT_FAILED_MPL_STRING_IS_FULL
282               , (Sequence)
283             );
284             // If the above assertion didn't fire, then the string is sparse.
285             // Repack the string and retry the push_front.
286             typedef
287                 typename mpl::push_front<
288                     typename mpl::reverse_copy<
289                         Sequence
290                       , mpl::front_inserter<string<> >
291                     >::type
292                   , Value
293                 >::type
294             type;
295         };
296
297         #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
298         template<typename Value>
299         struct apply<mpl::string<>, Value, false>
300         {
301             typedef mpl::string<(char)Value::value> type;
302         };
303         #endif
304
305         #define M0(z,n,data)                                                                        \
306         template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C), typename Value>                               \
307         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, Value, true>                     \
308         {                                                                                           \
309             typedef                                                                                 \
310                 mpl::string<                                                                        \
311                     (char)Value::value                                                              \
312                     BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, C)                                        \
313                 >                                                                                   \
314             type;                                                                                   \
315         };
316
317         BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
318         #undef M0
319
320         template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), typename Value>
321         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, Value, false>
322         {
323             typedef
324                 mpl::string<
325                     BOOST_MPL_MULTICHAR_PUSH_FRONT(C0, Value::value)
326                   , BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)
327                 >
328             type0;
329
330             #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
331             typedef
332                 typename mpl::if_<
333                     mpl::empty<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> >
334                   , mpl::string<(char)Value::value>
335                   , type0
336                 >::type
337             type;
338             #else
339             typedef type0 type;
340             #endif
341         };
342     };
343
344     template<typename Tag>
345     struct pop_front_impl;
346
347     template<>
348     struct pop_front_impl<mpl::string_tag>
349     {
350         template<typename Sequence, bool B = (1==BOOST_MPL_MULTICHAR_LENGTH(Sequence::front_))>
351         struct apply;
352
353         #define M0(z,n,data)                                                                        \
354         template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)>                                               \
355         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, true>                            \
356         {                                                                                           \
357             BOOST_MPL_ASSERT_MSG((C0 != 0), POP_FRONT_FAILED_MPL_STRING_IS_EMPTY, (mpl::string<>)); \
358             typedef                                                                                 \
359                 mpl::string<BOOST_PP_ENUM_SHIFTED_PARAMS_Z(z, n, C)>                                \
360             type;                                                                                   \
361         };
362
363         BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
364         #undef M0
365
366         template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)>
367         struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, false>
368         {
369             typedef
370                 mpl::string<
371                     BOOST_MPL_MULTICHAR_POP_FRONT(C0)
372                   , BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)
373                 >
374             type;
375         };
376     };
377
378     template<typename Tag>
379     struct insert_range_impl;
380
381     template<>
382     struct insert_range_impl<mpl::string_tag>
383     {
384         template<typename Sequence, typename Pos, typename Range>
385         struct apply
386           : mpl::copy<
387                 mpl::joint_view<
388                     mpl::iterator_range<
389                         mpl::string_iterator<Sequence, 0, 0>
390                       , Pos
391                     >
392                   , mpl::joint_view<
393                         Range
394                       , mpl::iterator_range<
395                             Pos
396                           , typename mpl::end<Sequence>::type
397                         >
398                     >
399                 >
400               , mpl::back_inserter<mpl::string<> >
401             >
402         {};
403     };
404
405     template<typename Tag>
406     struct insert_impl;
407
408     template<>
409     struct insert_impl<mpl::string_tag>
410     {
411         template<typename Sequence, typename Pos, typename Value>
412         struct apply
413           : mpl::insert_range<Sequence, Pos, mpl::string<(char)Value::value> >
414         {};
415     };
416
417     template<typename Tag>
418     struct erase_impl;
419
420     template<>
421     struct erase_impl<mpl::string_tag>
422     {
423         template<typename Sequence, typename First, typename Last>
424         struct apply
425           : mpl::copy<
426                 mpl::joint_view<
427                     mpl::iterator_range<
428                         mpl::string_iterator<Sequence, 0, 0>
429                       , First
430                     >
431                   , mpl::iterator_range<
432                         typename mpl::if_na<Last, typename mpl::next<First>::type>::type
433                       , typename mpl::end<Sequence>::type
434                     >
435                 >
436               , mpl::back_inserter<mpl::string<> >
437             >
438         {};
439     };
440
441     template<typename Tag>
442     struct clear_impl;
443
444     template<>
445     struct clear_impl<mpl::string_tag>
446     {
447         template<typename>
448         struct apply
449         {
450             typedef mpl::string<> type;
451         };
452     };
453
454     #define M0(z, n, data)                                                                            \
455     template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), int J>                         \
456     struct string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, n, J>   \
457     {                                                                                                 \
458         enum { eomc_ = (BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, n)) == J + 1) };                   \
459         typedef mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> string;             \
460         typedef std::bidirectional_iterator_tag category;                                             \
461         typedef                                                                                       \
462             mpl::string_iterator<string, n + eomc_, eomc_ ? 0 : J + 1>                                \
463         next;                                                                                         \
464         typedef                                                                                       \
465             mpl::string_iterator<string, n, J - 1>                                                    \
466         prior;                                                                                        \
467         typedef mpl::char_<BOOST_MPL_MULTICHAR_AT(BOOST_PP_CAT(C, n), J)> type;                       \
468     };                                                                                                \
469     template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)>                                \
470     struct string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, n, 0>   \
471     {                                                                                                 \
472         enum { eomc_ = (BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, n)) == 1) };                       \
473         typedef mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> string;             \
474         typedef std::bidirectional_iterator_tag category;                                             \
475         typedef                                                                                       \
476             mpl::string_iterator<string, n + eomc_, !eomc_>                                           \
477         next;                                                                                         \
478         typedef                                                                                       \
479             mpl::string_iterator<                                                                     \
480                 string                                                                                \
481               , n - 1                                                                                 \
482               , BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, BOOST_PP_DEC(n))) - 1                      \
483             >                                                                                         \
484         prior;                                                                                        \
485         typedef mpl::char_<BOOST_MPL_MULTICHAR_AT(BOOST_PP_CAT(C, n), 0)> type;                       \
486     };
487
488     BOOST_PP_REPEAT(BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
489     #undef M0
490
491     template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)>
492     struct string
493     {
494         /// INTERNAL ONLY
495         enum
496         {
497             front_  = C0
498           , back_   = BOOST_PP_CAT(C, BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS))
499         };
500
501         typedef char        value_type;
502         typedef string      type;
503         typedef string_tag  tag;
504     };
505
506     namespace aux_
507     {
508         template<typename It, typename End>
509         struct next_unless
510           : mpl::next<It>
511         {};
512
513         template<typename End>
514         struct next_unless<End, End>
515         {
516             typedef End type;
517         };
518
519         template<typename It, typename End>
520         struct deref_unless
521           : mpl::deref<It>
522         {};
523
524         template<typename End>
525         struct deref_unless<End, End>
526         {
527             typedef mpl::char_<'\0'> type;
528         };
529     }
530
531     template<typename Sequence>
532     struct c_str
533     {
534         typedef typename mpl::end<Sequence>::type iend;
535         typedef typename mpl::begin<Sequence>::type i0;
536         #define M0(z, n, data)                                                                      \
537         typedef                                                                                     \
538             typename mpl::aux_::next_unless<BOOST_PP_CAT(i, n), iend>::type                         \
539         BOOST_PP_CAT(i, BOOST_PP_INC(n));
540         BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
541         #undef M0
542
543         typedef c_str type;
544         static typename Sequence::value_type const value[BOOST_MPL_LIMIT_STRING_SIZE+1];
545     };
546
547     template<typename Sequence>
548     typename Sequence::value_type const c_str<Sequence>::value[BOOST_MPL_LIMIT_STRING_SIZE+1] =
549     {
550         #define M0(z, n, data)                                                                      \
551         mpl::aux_::deref_unless<BOOST_PP_CAT(i, n), iend>::type::value,
552         BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
553         #undef M0
554         '\0'
555     };
556
557 }} // namespace boost
558
559 #endif // BOOST_MPL_STRING_HPP_INCLUDED