Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 11 | #ifndef NDNBOOST_RANGE_AS_LITERAL_HPP |
| 12 | #define NDNBOOST_RANGE_AS_LITERAL_HPP |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 13 | |
| 14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
| 15 | # pragma once |
| 16 | #endif |
| 17 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 18 | #ifdef NDNBOOST_NO_FUNCTION_TEMPLATE_ORDERING |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 19 | #include <ndnboost/range/detail/as_literal.hpp> |
| 20 | #else |
| 21 | |
| 22 | #include <ndnboost/range/iterator_range.hpp> |
| 23 | #include <ndnboost/range/detail/str_types.hpp> |
| 24 | |
| 25 | #include <ndnboost/detail/workaround.hpp> |
| 26 | |
| 27 | #include <cstring> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 28 | #ifndef NDNBOOST_NO_CWCHAR |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 29 | #include <cwchar> |
| 30 | #endif |
| 31 | |
| 32 | namespace ndnboost |
| 33 | { |
| 34 | namespace range_detail |
| 35 | { |
| 36 | inline std::size_t length( const char* s ) |
| 37 | { |
| 38 | return strlen( s ); |
| 39 | } |
| 40 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 41 | #ifndef NDNBOOST_NO_CWCHAR |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 64 | #ifndef NDNBOOST_NO_CWCHAR |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 > |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 90 | inline iterator_range<NDNBOOST_DEDUCED_TYPENAME range_iterator<T>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 91 | make_range( T& r, long ) |
| 92 | { |
| 93 | return ndnboost::make_iterator_range( r ); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | template< class Range > |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 99 | inline iterator_range<NDNBOOST_DEDUCED_TYPENAME range_iterator<Range>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 > |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 106 | inline iterator_range<NDNBOOST_DEDUCED_TYPENAME range_iterator<const Range>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 125 | #endif // NDNBOOST_NO_FUNCTION_TEMPLATE_ORDERING |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 126 | |
| 127 | #endif |