Home | History | Annotate | Download | only in range
      1 // Boost.Range library
      2 //
      3 //  Copyright Thorsten Ottosen 2006. Use, modification and
      4 //  distribution is subject to the Boost Software License, Version
      5 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
      6 //  http://www.boost.org/LICENSE_1_0.txt)
      7 //
      8 // For more information, see http://www.boost.org/libs/range/
      9 //
     10 
     11 #ifndef BOOST_RANGE_AS_LITERAL_HPP
     12 #define BOOST_RANGE_AS_LITERAL_HPP
     13 
     14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
     15 # pragma once
     16 #endif
     17 
     18 #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
     19 #include <boost/range/detail/as_literal.hpp>
     20 #else
     21 
     22 #include <boost/range/iterator_range.hpp>
     23 #include <boost/range/detail/str_types.hpp>
     24 
     25 #include <boost/detail/workaround.hpp>
     26 
     27 #include <cstring>
     28 #ifndef BOOST_NO_CWCHAR
     29 #include <cwchar>
     30 #endif
     31 
     32 namespace boost
     33 {
     34     namespace range_detail
     35     {
     36         inline std::size_t length( const char* s )
     37         {
     38             return strlen( s );
     39         }
     40 
     41 #ifndef BOOST_NO_CWCHAR
     42         inline std::size_t length( const wchar_t* s )
     43         {
     44             return wcslen( s );
     45         }
     46 #endif
     47 
     48         //
     49         // Remark: the compiler cannot choose between T* and T[sz]
     50         // overloads, so we must put the T* internal to the
     51         // unconstrained version.
     52         //
     53 
     54         inline bool is_char_ptr( char* )
     55         {
     56             return true;
     57         }
     58 
     59         inline bool is_char_ptr( const char* )
     60         {
     61             return true;
     62         }
     63 
     64 #ifndef BOOST_NO_CWCHAR
     65         inline bool is_char_ptr( wchar_t* )
     66         {
     67             return true;
     68         }
     69 
     70         inline bool is_char_ptr( const wchar_t* )
     71         {
     72             return true;
     73         }
     74 #endif
     75 
     76         template< class T >
     77         inline long is_char_ptr( const T& /* r */ )
     78         {
     79             return 0L;
     80         }
     81 
     82         template< class T >
     83         inline iterator_range<T*>
     84         make_range( T* const r, bool )
     85         {
     86             return iterator_range<T*>( r, r + length(r) );
     87         }
     88 
     89         template< class T >
     90         inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<T>::type>
     91         make_range( T& r, long )
     92         {
     93             return boost::make_iterator_range( r );
     94         }
     95 
     96     }
     97 
     98     template< class Range >
     99     inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<Range>::type>
    100     as_literal( Range& r )
    101     {
    102         return range_detail::make_range( r, range_detail::is_char_ptr(r) );
    103     }
    104 
    105     template< class Range >
    106     inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<const Range>::type>
    107     as_literal( const Range& r )
    108     {
    109         return range_detail::make_range( r, range_detail::is_char_ptr(r) );
    110     }
    111 
    112     template< class Char, std::size_t sz >
    113     inline iterator_range<Char*> as_literal( Char (&arr)[sz] )
    114     {
    115         return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
    116     }
    117 
    118     template< class Char, std::size_t sz >
    119     inline iterator_range<const Char*> as_literal( const Char (&arr)[sz] )
    120     {
    121         return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
    122     }
    123 }
    124 
    125 #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
    126 
    127 #endif
    128