Ticket #2905: string.patch

File string.patch, 34.9 KB (added by Eric Niebler, 14 years ago)

patch that adds mpl::char_ and mpl::string as well as tests and docs

  • boost/mpl/char_fwd.hpp

     
     1
     2#ifndef BOOST_MPL_CHAR_FWD_HPP_INCLUDED
     3#define BOOST_MPL_CHAR_FWD_HPP_INCLUDED
     4
     5// Copyright Eric Niebler 2008
     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// $Source$
     14// $Date: 2008-06-14 08:41:37 -0700 (Sat, 16 Jun 2008) $
     15// $Revision: 24874 $
     16
     17#include <boost/mpl/aux_/adl_barrier.hpp>
     18#include <boost/mpl/aux_/nttp_decl.hpp>
     19
     20BOOST_MPL_AUX_ADL_BARRIER_NAMESPACE_OPEN
     21
     22template< BOOST_MPL_AUX_NTTP_DECL(char, N) > struct char_;
     23
     24BOOST_MPL_AUX_ADL_BARRIER_NAMESPACE_CLOSE
     25BOOST_MPL_AUX_ADL_BARRIER_DECL(char_)
     26
     27#endif // BOOST_MPL_CHAR_FWD_HPP_INCLUDED
  • 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#include <boost/mpl/char.hpp>
     18#include <boost/mpl/long.hpp>
     19#include <boost/mpl/back.hpp>
     20#include <boost/mpl/copy.hpp>
     21#include <boost/mpl/assert.hpp>
     22#include <boost/mpl/size_t.hpp>
     23#include <boost/mpl/joint_view.hpp>
     24#include <boost/mpl/insert_range.hpp>
     25#include <boost/mpl/back_inserter.hpp>
     26#include <boost/mpl/iterator_range.hpp>
     27#include <boost/preprocessor/arithmetic/dec.hpp>
     28#include <boost/preprocessor/arithmetic/add.hpp>
     29#include <boost/preprocessor/arithmetic/div.hpp>
     30#include <boost/preprocessor/repetition/enum.hpp>
     31#include <boost/preprocessor/punctuation/comma_if.hpp>
     32#include <boost/preprocessor/repetition/enum_params.hpp>
     33#include <boost/preprocessor/repetition/repeat_from_to.hpp>
     34#include <boost/preprocessor/repetition/enum_shifted_params.hpp>
     35#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
     36#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
     37
     38namespace boost { namespace mpl
     39{
     40    #ifndef BOOST_MPL_STRING_MAX_LENGTH
     41    # define BOOST_MPL_STRING_MAX_LENGTH 32
     42    #endif
     43   
     44    #define BOOST_MPL_STRING_MAX_PARAMS BOOST_PP_DIV(BOOST_PP_ADD(BOOST_MPL_STRING_MAX_LENGTH, 3), 4)
     45
     46    #define BOOST_MPL_MULTICHAR_LENGTH(c)   (std::size_t)((c>0xffffff)+(c>0xffff)+(c>0xff)+1)
     47    #define BOOST_MPL_MULTICHAR_AT(c,i)     (char)(0xff&(c>>(8*(BOOST_MPL_MULTICHAR_LENGTH(c)-(std::size_t)(i)-1))))
     48
     49    struct string_tag;
     50    struct string_iterator_tag;
     51
     52    template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C, 0)>
     53    struct string;
     54
     55    template<typename Sequence, long N>
     56    struct string_iterator;
     57
     58    template<typename Sequence>
     59    struct sequence_tag;
     60
     61    template<typename Tag>
     62    struct size_impl;
     63
     64    template<>
     65    struct size_impl<string_tag>
     66    {
     67        template<typename Sequence>
     68        struct apply
     69          : mpl::size_t<Sequence::size>
     70        {};
     71    };
     72
     73    template<typename Tag>
     74    struct at_impl;
     75
     76    template<>
     77    struct at_impl<string_tag>
     78    {
     79        template<typename Sequence, typename N>
     80        struct apply
     81          : Sequence::template at<N::value>
     82        {};
     83    };
     84
     85    template<typename Tag>
     86    struct begin_impl;
     87
     88    template<>
     89    struct begin_impl<string_tag>
     90    {
     91        template<typename Sequence>
     92        struct apply
     93        {
     94            typedef string_iterator<Sequence, 0> type;
     95        };
     96    };
     97
     98    template<typename Tag>
     99    struct end_impl;
     100
     101    template<>
     102    struct end_impl<string_tag>
     103    {
     104        template<typename Sequence>
     105        struct apply
     106        {
     107            typedef string_iterator<Sequence, Sequence::size> type;
     108        };
     109    };
     110
     111    template<typename Tag>
     112    struct push_back_impl;
     113
     114    template<>
     115    struct push_back_impl<string_tag>
     116    {
     117        template<typename Sequence, typename Value, bool B = (4==BOOST_MPL_MULTICHAR_LENGTH(Sequence::back_))>
     118        struct apply
     119        {
     120            BOOST_MPL_ASSERT_MSG(false, PUSH_BACK_FAILED_MPL_STRING_IS_FULL, (Sequence));
     121            typedef void type;
     122        };
     123
     124        template<typename Value>
     125        struct apply<string<>, Value, false>
     126        {
     127            typedef string<(char)Value::value> type;
     128        };
     129
     130        #define M0(z,n,data)                                                                        \
     131        template<BOOST_PP_ENUM_PARAMS_Z(z, n, unsigned int C), typename Value>                      \
     132        struct apply<string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, Value, false>                         \
     133        {                                                                                           \
     134            typedef string<                                                                         \
     135                BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_DEC(n), C)                                       \
     136                BOOST_PP_COMMA_IF(BOOST_PP_DEC(n))                                                  \
     137                (BOOST_PP_CAT(C,BOOST_PP_DEC(n))>0xffffff)                                          \
     138                ?BOOST_PP_CAT(C,BOOST_PP_DEC(n))                                                    \
     139                :(BOOST_PP_CAT(C,BOOST_PP_DEC(n))<<8)|(unsigned char)Value::value                   \
     140              , (BOOST_PP_CAT(C,BOOST_PP_DEC(n))>0xffffff)                                          \
     141                ?(char)Value::value                                                                 \
     142                :0                                                                                  \
     143            > type;                                                                                 \
     144        };
     145
     146        BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS), M0, ~)
     147        #undef M0
     148
     149        template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C), typename Value>
     150        struct apply<string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, Value, false>
     151        {
     152            typedef string<
     153                BOOST_PP_ENUM_PARAMS(BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS), C)
     154              , (BOOST_PP_CAT(C,BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS))<<8)|(unsigned char)Value::value
     155            > type;
     156        };
     157    };
     158
     159    template<typename Tag>
     160    struct push_front_impl;
     161
     162    template<>
     163    struct push_front_impl<string_tag>
     164    {
     165        template<typename Sequence, typename Value, bool B = (4==BOOST_MPL_MULTICHAR_LENGTH(Sequence::front_))>
     166        struct apply
     167        {
     168            BOOST_MPL_ASSERT_MSG(false, PUSH_FRONT_FAILED_MPL_STRING_IS_FULL, (Sequence));
     169            typedef void type;
     170        };
     171
     172        template<typename Value>
     173        struct apply<string<>, Value, false>
     174        {
     175            typedef string<(char)Value::value> type;
     176        };
     177
     178        #define M0(z,n,data)                                                                        \
     179        template<BOOST_PP_ENUM_PARAMS_Z(z, n, unsigned int C), typename Value>                      \
     180        struct apply<string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, Value, true>                          \
     181        {                                                                                           \
     182            typedef string<                                                                         \
     183                (char)Value::value                                                                  \
     184                BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, C)                                            \
     185            > type;                                                                                 \
     186        };
     187
     188        BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS), M0, ~)
     189        #undef M0
     190
     191        template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C), typename Value>
     192        struct apply<string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, Value, false>
     193        {
     194            typedef string<
     195                ((((unsigned char)Value::value)<<(BOOST_MPL_MULTICHAR_LENGTH(C0)*8))|C0)
     196              , BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)
     197            > type;
     198        };
     199    };
     200
     201    template<typename Tag>
     202    struct insert_range_impl;
     203
     204    template<>
     205    struct insert_range_impl<string_tag>
     206    {
     207        template<typename Sequence, typename Pos, typename Range>
     208        struct apply
     209          : copy<
     210                joint_view<
     211                    iterator_range<
     212                        string_iterator<Sequence, 0>
     213                      , Pos
     214                    >
     215                  , joint_view<
     216                        Range
     217                      , iterator_range<
     218                            Pos
     219                          , string_iterator<Sequence, Sequence::size>
     220                        >
     221                    >
     222                >
     223              , back_inserter<string<> >
     224            >
     225        {};
     226    };
     227
     228    template<typename Tag>
     229    struct insert_impl;
     230
     231    template<>
     232    struct insert_impl<string_tag>
     233    {
     234        template<typename Sequence, typename Pos, typename Value>
     235        struct apply
     236          : insert_range<Sequence, Pos, string<(char)Value::value> >
     237        {};
     238    };
     239
     240    template<typename Tag>
     241    struct erase_impl;
     242
     243    template<>
     244    struct erase_impl<string_tag>
     245    {
     246        template<typename Sequence, typename First, typename Last>
     247        struct apply
     248          : copy<
     249                joint_view<
     250                    iterator_range<
     251                        string_iterator<Sequence, 0>
     252                      , First
     253                    >
     254                  , iterator_range<
     255                        typename if_na<Last, typename next<First>::type>::type
     256                      , string_iterator<Sequence, Sequence::size>
     257                    >
     258                >
     259              , back_inserter<string<> >
     260            >
     261        {};
     262    };
     263
     264    template<typename Tag>
     265    struct clear_impl;
     266
     267    template<>
     268    struct clear_impl<string_tag>
     269    {
     270        template<typename>
     271        struct apply
     272        {
     273            typedef string<> type;
     274        };
     275    };
     276
     277    template<typename Tag>
     278    struct advance_impl;
     279
     280    template<>
     281    struct advance_impl<string_iterator_tag>
     282    {
     283        template<typename Iterator, typename N>
     284        struct apply
     285        {
     286            typedef string_iterator<
     287                typename Iterator::string_type
     288              , Iterator::index + N::value
     289            > type;
     290        };
     291    };
     292
     293    template<typename Tag>
     294    struct distance_impl;
     295
     296    template<>
     297    struct distance_impl<string_iterator_tag>
     298    {
     299        template<typename First, typename Last>
     300        struct apply
     301        {
     302            typedef mpl::long_<Last::index - First::index> type;
     303        };
     304    };
     305
     306    template<typename Sequence, long N>
     307    struct string_iterator
     308      : Sequence::template at<N>
     309    {
     310        typedef string_iterator_tag tag;
     311        typedef std::random_access_iterator_tag category;
     312        typedef Sequence string_type;
     313        static long const index = N;
     314        typedef string_iterator<Sequence, N+1> next;
     315        typedef string_iterator<Sequence, N-1> prior;
     316    };
     317
     318    template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C)>
     319    struct string
     320    {
     321        /// INTERNAL ONLY
     322        static unsigned int const front_ = C0;
     323        /// INTERNAL ONLY
     324        static unsigned int const back_ = BOOST_PP_CAT(C, BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS));
     325        /// INTERNAL ONLY
     326        typedef string<BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> rest_;
     327
     328        typedef string type;
     329
     330        typedef string_tag tag;
     331
     332        static std::size_t const size = BOOST_MPL_MULTICHAR_LENGTH(C0) + rest_::size;
     333
     334        template<long Pos, bool B = (Pos < BOOST_MPL_MULTICHAR_LENGTH(C0))>
     335        struct at
     336          : boost::mpl::char_<BOOST_MPL_MULTICHAR_AT(C0,Pos)>
     337        {};
     338
     339        template<long Pos>
     340        struct at<Pos, false>
     341          : rest_::template at<Pos-BOOST_MPL_MULTICHAR_LENGTH(C0)>
     342        {};
     343
     344        static char const c_str[];
     345    };
     346
     347    template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C)>
     348    char const string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>::c_str[] =
     349    {
     350    #define M0(z, n, data) at<n>::value
     351        BOOST_PP_ENUM(BOOST_MPL_STRING_MAX_LENGTH, M0, ~)
     352    #undef M0
     353      , '\0' // to ensure the string is null-terminated
     354    };
     355
     356    template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C)>
     357    std::size_t const string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>::size;
     358
     359    template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C)>
     360    unsigned int const string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>::front_;
     361
     362    template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, unsigned int C)>
     363    unsigned int const string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>::back_;
     364
     365    template<>
     366    struct string<>
     367    {
     368        /// INTERNAL ONLY
     369        static unsigned int const front_ = 0;
     370        /// INTERNAL ONLY
     371        static unsigned int const back_ = 0;
     372        /// INTERNAL ONLY
     373        typedef string rest_;
     374
     375        typedef string type;
     376
     377        typedef string_tag tag;
     378
     379        static std::size_t const size = 0;
     380
     381        template<unsigned int>
     382        struct at
     383          : boost::mpl::char_<'\0'>
     384        {};
     385
     386        static char const c_str[];
     387    };
     388
     389    char const string<>::c_str[] = {'\0'};
     390    std::size_t const string<>::size;
     391    unsigned int const string<>::front_;
     392    unsigned int const string<>::back_;
     393
     394}} // namespace boost
     395
     396#endif // BOOST_MPL_STRING_HPP_INCLUDED
  • boost/mpl/char.hpp

     
     1
     2#ifndef BOOST_MPL_CHAR_HPP_INCLUDED
     3#define BOOST_MPL_CHAR_HPP_INCLUDED
     4
     5// Copyright Eric Niebler 2008
     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// $Source$
     14// $Date: 2008-06-14 08:41:37 -0700 (Sat, 16 Jun 2008) $
     15// $Revision: 24874 $
     16
     17#include <boost/mpl/char_fwd.hpp>
     18
     19#define AUX_WRAPPER_VALUE_TYPE char
     20#include <boost/mpl/aux_/integral_wrapper.hpp>
     21
     22#endif // BOOST_MPL_CHAR_HPP_INCLUDED
  • libs/mpl/test/string.cpp

     
     1
     2// Copyright Eric Niebler 2009
     3//
     4// Distributed under the Boost Software License, Version 1.0.
     5// (See accompanying file LICENSE_1_0.txt or copy at
     6// http://www.boost.org/LICENSE_1_0.txt)
     7//
     8// See http://www.boost.org/libs/mpl for documentation.
     9
     10// $Id: string.cpp 49240 2009-04-01 09:21:07Z eric_niebler $
     11// $Date: 2009-04-01 02:21:07 -0700 (Wed, 1 Apr 2009) $
     12// $Revision: 49240 $
     13
     14#include <string>
     15#include <cstring>
     16#include <iostream>
     17
     18#include <boost/mpl/string.hpp>
     19
     20#include <boost/mpl/at.hpp>
     21#include <boost/mpl/long.hpp>
     22#include <boost/mpl/back.hpp>
     23#include <boost/mpl/copy.hpp>
     24#include <boost/mpl/size.hpp>
     25#include <boost/mpl/empty.hpp>
     26#include <boost/mpl/front.hpp>
     27#include <boost/mpl/clear.hpp>
     28#include <boost/mpl/erase.hpp>
     29#include <boost/mpl/insert.hpp>
     30#include <boost/mpl/assert.hpp>
     31#include <boost/mpl/size_t.hpp>
     32#include <boost/mpl/for_each.hpp>
     33#include <boost/mpl/vector_c.hpp>
     34#include <boost/mpl/push_back.hpp>
     35#include <boost/mpl/joint_view.hpp>
     36#include <boost/mpl/insert_range.hpp>
     37#include <boost/mpl/back_inserter.hpp>
     38#include <boost/mpl/iterator_range.hpp>
     39#include <boost/type_traits/is_same.hpp>
     40#include <boost/detail/lightweight_test.hpp>
     41namespace mpl = boost::mpl;
     42
     43// Accept a string as a template parameter!
     44template<char const *sz>
     45struct greeting
     46{
     47    std::string say_hello() const
     48    {
     49        return sz;
     50    }
     51};
     52
     53struct push_char
     54{
     55    push_char(std::string &str)
     56      : str_(&str)
     57    {}
     58
     59    void operator()(char ch) const
     60    {
     61        this->str_->push_back(ch);
     62    }
     63
     64    std::string *str_;
     65};
     66
     67void test1()
     68{
     69    BOOST_TEST(0 == std::strcmp(mpl::string<'Hell','o wo','rld!'>::c_str, "Hello world!"));
     70    BOOST_TEST((12 == mpl::size<mpl::string<'Hell','o wo','rld!'> >::type::value));
     71    BOOST_TEST(('w' == mpl::at_c<mpl::string<'Hell','o wo','rld!'>, 6>::type::value));
     72
     73    // test using a string as a template parameter
     74    greeting<mpl::string<'Hell','o wo','rld!'>::c_str> g;
     75    BOOST_TEST("Hello world!" == g.say_hello());
     76
     77    BOOST_TEST(0 == std::strcmp("", mpl::string<>::c_str));
     78
     79    std::string result;
     80    mpl::for_each<mpl::string<'Hell','o wo','rld!'> >(push_char(result));
     81    BOOST_TEST("Hello world!" == result);
     82
     83    BOOST_MPL_ASSERT((mpl::empty<mpl::string<> >));
     84    BOOST_MPL_ASSERT_NOT((mpl::empty<mpl::string<'hi!'> >));
     85
     86    BOOST_TEST(('h' == mpl::front<mpl::string<'hi!'> >::type()));
     87    BOOST_TEST(('!' == mpl::back<mpl::string<'hi!'> >::type()));
     88}
     89
     90// testing push_back
     91void test2()
     92{
     93    typedef mpl::push_back<mpl::string<>, mpl::char_<'a'> >::type t1;
     94    BOOST_TEST(0 == std::strcmp("a", t1::c_str));
     95
     96    typedef mpl::push_back<t1, mpl::char_<'b'> >::type t2;
     97    BOOST_TEST(0 == std::strcmp("ab", t2::c_str));
     98
     99    typedef mpl::push_back<t2, mpl::char_<'c'> >::type t3;
     100    BOOST_TEST(0 == std::strcmp("abc", t3::c_str));
     101    BOOST_MPL_ASSERT((boost::is_same<t3, mpl::string<'abc'> >));
     102
     103    typedef mpl::push_back<t3, mpl::char_<'d'> >::type t4;
     104    BOOST_TEST(0 == std::strcmp("abcd", t4::c_str));
     105
     106    typedef mpl::push_back<t4, mpl::char_<'e'> >::type t5;
     107    BOOST_TEST(0 == std::strcmp("abcde", t5::c_str));
     108    BOOST_MPL_ASSERT((boost::is_same<t5, mpl::string<'abcd','e'> >));
     109
     110    typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaa'> almost_full;
     111    BOOST_TEST(0 == std::strcmp("aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaa", almost_full::c_str));
     112
     113    typedef mpl::push_back<almost_full, mpl::char_<'X'> >::type t6;
     114    BOOST_TEST(0 == std::strcmp("aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaX", t6::c_str));
     115}
     116
     117// testing push_front
     118void test3()
     119{
     120    typedef mpl::push_front<mpl::string<>, mpl::char_<'a'> >::type t1;
     121    BOOST_TEST(0 == std::strcmp("a", t1::c_str));
     122
     123    typedef mpl::push_front<t1, mpl::char_<'b'> >::type t2;
     124    BOOST_TEST(0 == std::strcmp("ba", t2::c_str));
     125
     126    typedef mpl::push_front<t2, mpl::char_<'c'> >::type t3;
     127    BOOST_TEST(0 == std::strcmp("cba", t3::c_str));
     128
     129    typedef mpl::push_front<t3, mpl::char_<'d'> >::type t4;
     130    BOOST_TEST(0 == std::strcmp("dcba", t4::c_str));
     131
     132    typedef mpl::push_front<t4, mpl::char_<'e'> >::type t5;
     133    BOOST_TEST(0 == std::strcmp("edcba", t5::c_str));
     134
     135    typedef mpl::string<'aaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa'> almost_full;
     136    BOOST_TEST(0 == std::strcmp("aaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa", almost_full::c_str));
     137
     138    typedef mpl::push_front<almost_full, mpl::char_<'X'> >::type t6;
     139    BOOST_TEST(0 == std::strcmp("Xaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa", t6::c_str));
     140}
     141
     142void test4()
     143{
     144    // back-inserter with copy
     145    typedef mpl::vector_c<char, 'a','b','c','d','e'> rgc;
     146    typedef mpl::copy<rgc, mpl::back_inserter<mpl::string<> > >::type str;
     147    BOOST_TEST(0 == std::strcmp("abcde", str::c_str));
     148}
     149
     150// test insert_range and erase
     151void test5()
     152{
     153    typedef mpl::string<'Hell','o wo','rld!'> hello;
     154    typedef mpl::advance_c<mpl::begin<hello>::type, 5>::type where;
     155    typedef mpl::string<' cru','el'> cruel;
     156    typedef mpl::insert_range<hello, where, cruel>::type hello_cruel;
     157    BOOST_TEST(0 == std::strcmp("Hello cruel world!", hello_cruel::c_str));
     158
     159    typedef mpl::erase<hello, mpl::begin<hello>::type, where>::type erased1;
     160    BOOST_TEST(0 == std::strcmp(" world!", erased1::c_str));
     161}
     162
     163
     164int main()
     165{
     166    test1();
     167    test2();
     168    test3();
     169    test4();
     170    test5();
     171
     172    return boost::report_errors();
     173}
  • libs/mpl/test/char.cpp

     
     1
     2// Copyright Eric Niebler 2008
     3//
     4// Distributed under the Boost Software License, Version 1.0.
     5// (See accompanying file LICENSE_1_0.txt or copy at
     6// http://www.boost.org/LICENSE_1_0.txt)
     7//
     8// See http://www.boost.org/libs/mpl for documentation.
     9
     10// $Id: char.cpp 49240 2009-04-01 09:21:07Z eric_niebler $
     11// $Date: 2009-04-01 02:21:07 -0700 (Wed, 1 Apr 2009) $
     12// $Revision: 49240 $
     13
     14#include <boost/mpl/char.hpp>
     15#include <boost/preprocessor/repeat.hpp>
     16
     17#include "integral_wrapper_test.hpp"
     18
     19
     20MPL_TEST_CASE()
     21{
     22#   define WRAPPER(T, i) char_<i>
     23    BOOST_PP_REPEAT(10, INTEGRAL_WRAPPER_TEST, char)
     24}
  • libs/mpl/test/Jamfile.v2

     
    4444compile insert.cpp ;
    4545compile insert_range.cpp ;
    4646run int.cpp ;
     47run char.cpp ;
    4748run integral_c.cpp : : : <toolset>vacpp:<cxxflags>-qchars=signed ;
    4849compile is_placeholder.cpp ;
    4950compile is_sequence.cpp ;
     
    9091compile vector.cpp ;
    9192compile vector_c.cpp ;
    9293compile zip_view.cpp ;
     94run string.cpp ;
  • libs/mpl/doc/src/refmanual/char_.rst

     
     1.. Data Types/Numeric//char_ |60
     2
     3.. Copyright Eric Niebler 2009.
     4.. Distributed under the Boost
     5.. Software License, Version 1.0. (See accompanying
     6.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
     7
     8char\_
     9======
     10
     11Synopsis
     12--------
     13
     14.. parsed-literal::
     15   
     16    template<
     17          char N
     18        >
     19    struct char\_
     20    {
     21        // |unspecified|
     22        // ...
     23    };
     24
     25
     26Description
     27-----------
     28
     29An |Integral Constant| wrapper for ``char``.
     30
     31
     32Header
     33------
     34
     35.. parsed-literal::
     36   
     37    #include <boost/mpl/char.hpp>
     38
     39
     40Model of
     41--------
     42
     43|Integral Constant|
     44
     45
     46Parameters
     47----------
     48
     49+---------------+-------------------------------+---------------------------+
     50| Parameter     | Requirement                   | Description               |
     51+===============+===============================+===========================+
     52| ``N``         | A character constant          | A value to wrap.          |
     53+---------------+-------------------------------+---------------------------+
     54
     55Expression semantics
     56--------------------
     57
     58|Semantics disclaimer...| |Integral Constant|.
     59
     60For arbitrary character constant ``c``:
     61
     62+-------------------+-----------------------------------------------------------+
     63| Expression        | Semantics                                                 |
     64+===================+===========================================================+
     65| ``char_<c>``      | An |Integral Constant| ``x`` such that ``x::value == c``  |
     66|                   | and ``x::value_type`` is identical to ``char``.           |
     67+-------------------+-----------------------------------------------------------+
     68
     69
     70Example
     71-------
     72
     73.. parsed-literal::
     74
     75    typedef char_<'c'> c;
     76   
     77    BOOST_MPL_ASSERT(( is_same< c::value_type, char > ));
     78    BOOST_MPL_ASSERT(( is_same< c::type, c > ));
     79    BOOST_MPL_ASSERT(( is_same< next< c >::type, char_<'d'> > ));
     80    BOOST_MPL_ASSERT(( is_same< prior< c >::type, char_<'b'> > ));
     81    BOOST_MPL_ASSERT_RELATION( (c::value), ==, 'c' );
     82    assert( c() == 'c' );
     83
     84
     85See also
     86--------
     87
     88|Data Types|, |Integral Constant|, |int_|, |size_t|, |integral_c|
     89
  • libs/mpl/doc/src/refmanual/string.rst

     
     1.. Sequences/Classes//string |100
     2
     3.. Copyright Eric Niebler 2009.
     4.. Distributed under the Boost
     5.. Software License, Version 1.0. (See accompanying
     6.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
     7
     8string
     9======
     10
     11Description
     12-----------
     13
     14``string`` is a |variadic|, `random access`__, `extensible`__ |Integral Sequence Wrapper| of
     15characters that supports constant-time insertion and removal of elements at both ends, and
     16linear-time insertion and removal of elements in the middle. The parameters to ``string``
     17are multi-character literals, giving a somewhat readable syntax for compile-time strings.
     18``string`` also has a class static null-terminated character array called ``c_str`` that
     19facilitates interoperability with runtime string processing routines.
     20
     21__ `Random Access Sequence`_
     22__ `Extensible Sequence`_
     23
     24Header
     25------
     26
     27+-------------------+-------------------------------------------------------+
     28| Sequence form     | Header                                                |
     29+===================+=======================================================+
     30| Variadic          | ``#include <boost/mpl/string.hpp>``                   |
     31+-------------------+-------------------------------------------------------+
     32
     33Model of
     34--------
     35
     36* |Integral Sequence Wrapper|
     37* |Variadic Sequence|
     38* |Random Access Sequence|
     39* |Extensible Sequence|
     40* |Back Extensible Sequence|
     41* |Front Extensible Sequence|
     42
     43
     44Expression semantics
     45--------------------
     46
     47In the following table, ``s`` is an instance of ``string``, ``pos`` and ``last`` are iterators
     48into ``s``, ``r`` is a |Forward Sequence| of characters, ``n`` and ``x`` are |Integral Constant|\ s,
     49and |c1...cn| are arbitrary (multi-)characters.
     50
     51+---------------------------------------+-----------------------------------------------------------+
     52| Expression                            | Semantics                                                 |
     53+=======================================+===========================================================+
     54| .. parsed-literal::                   | ``string`` of characters |c1...cn|; see                   |
     55|                                       | |Variadic Sequence|.                                      |
     56|    string<|c1...cn|>                  |                                                           |
     57+---------------------------------------+-----------------------------------------------------------+
     58| .. parsed-literal::                   | Identical to ``string<``\ |c1...cn|\ ``>``;               |
     59|                                       | see |Variadic Sequence|.                                  |
     60|    string<|c1...cn|>::type            |                                                           |
     61+---------------------------------------+-----------------------------------------------------------+
     62| ``begin<s>::type``                    | An iterator pointing to the beginning of ``s``;           |
     63|                                       | see |Random Access Sequence|.                             |
     64+---------------------------------------+-----------------------------------------------------------+
     65| ``end<s>::type``                      | An iterator pointing to the end of ``s``;                 |
     66|                                       | see |Random Access Sequence|.                             |
     67+---------------------------------------+-----------------------------------------------------------+
     68| ``size<s>::type``                     | The size of ``s``; see |Random Access Sequence|.          |
     69+---------------------------------------+-----------------------------------------------------------+
     70| ``empty<s>::type``                    | |true if and only if| the sequence is empty;              |
     71|                                       | see |Random Access Sequence|.                             |
     72+---------------------------------------+-----------------------------------------------------------+
     73| ``front<s>::type``                    | The first element in ``s``; see                           |
     74|                                       | |Random Access Sequence|.                                 |
     75+---------------------------------------+-----------------------------------------------------------+
     76| ``back<s>::type``                     | The last element in ``s``; see                            |
     77|                                       | |Random Access Sequence|.                                 |
     78+---------------------------------------+-----------------------------------------------------------+
     79| ``at<s,n>::type``                     | The ``n``\ th element from the beginning of ``s``; see    |
     80|                                       | |Random Access Sequence|.                                 |
     81+---------------------------------------+-----------------------------------------------------------+
     82| ``insert<s,pos,x>::type``             | A new ``string`` of following elements:                   |
     83|                                       | [``begin<s>::type``, ``pos``), ``x``,                     |
     84|                                       | [``pos``, ``end<s>::type``); see |Extensible Sequence|.   |
     85+---------------------------------------+-----------------------------------------------------------+
     86| ``insert_range<s,pos,r>::type``       | A new ``string`` of following elements:                   |
     87|                                       | [``begin<s>::type``, ``pos``),                            |
     88|                                       | [``begin<r>::type``, ``end<r>::type``)                    |
     89|                                       | [``pos``, ``end<s>::type``); see |Extensible Sequence|.   |
     90+---------------------------------------+-----------------------------------------------------------+
     91| ``erase<s,pos>::type``                | A new ``string`` of following elements:                   |
     92|                                       | [``begin<s>::type``, ``pos``),                            |
     93|                                       | [``next<pos>::type``, ``end<s>::type``); see              |
     94|                                       | |Extensible Sequence|.                                    |
     95+---------------------------------------+-----------------------------------------------------------+
     96| ``erase<s,pos,last>::type``           | A new ``string`` of following elements:                   |
     97|                                       | [``begin<s>::type``, ``pos``),                            |
     98|                                       | [``last``, ``end<s>::type``); see |Extensible Sequence|.  |
     99+---------------------------------------+-----------------------------------------------------------+
     100| ``clear<s>::type``                    | An empty ``string``; see |Extensible Sequence|.           |
     101+---------------------------------------+-----------------------------------------------------------+
     102| ``push_back<s,x>::type``              | A new ``string`` of following elements:                   |
     103|                                       | |begin/end<s>|, ``x``;                                    |
     104|                                       | see |Back Extensible Sequence|.                           |
     105+---------------------------------------+-----------------------------------------------------------+
     106| ``pop_back<s>::type``                 | A new ``string`` of following elements:                   |
     107|                                       | [``begin<s>::type``, ``prior< end<s>::type >::type``);    |
     108|                                       | see |Back Extensible Sequence|.                           |
     109+---------------------------------------+-----------------------------------------------------------+
     110| ``push_front<s,x>::type``             | A new ``string`` of following elements:                   |
     111|                                       | |begin/end<s>|, ``x``; see |Front Extensible Sequence|.   |
     112+---------------------------------------+-----------------------------------------------------------+
     113| ``pop_front<s>::type``                | A new ``string`` of following elements:                   |
     114|                                       | [``next< begin<s>::type >::type``, ``end<s>::type``);     |
     115|                                       | see |Front Extensible Sequence|.                          |
     116+---------------------------------------+-----------------------------------------------------------+
     117| ``s::c_str``                          | A null-terminated byte string such that                   |
     118|                                       | ``s::c_str[``\ *n*\ ``]`` is                              |
     119|                                       | ``at<s,``\ *n*\ ``>::type::value`` for each *n* in        |
     120|                                       | [``0``, ``size<s>::type::value``), and                    |
     121|                                       | ``s::c_str[size<s>::type::value]`` is ``'\0'``.           |
     122+---------------------------------------+-----------------------------------------------------------+
     123
     124
     125Example
     126-------
     127
     128.. parsed-literal::
     129   
     130    typedef string<'hell','o wo','rld'> hello;
     131    typedef push_back<hello, char_<'!'> >::type hello2;
     132
     133    BOOST_ASSERT(0 == std::strcmp(hello2::c_str, "hello world!"));
     134
     135
     136See also
     137--------
     138
     139|Sequences|, |Variadic Sequence|, |Random Access Sequence|, |Extensible Sequence|, |Integral Sequence Wrapper|, |char_|
     140
  • libs/mpl/doc/src/refmanual/IntegralConstant.rst

     
    7171* |bool_|
    7272* |int_|
    7373* |long_|
     74* |char_|
    7475* |integral_c|
    7576
    7677