as_literal.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #ifndef BOOST_RANGE_AS_LITERAL_HPP
  11. #define BOOST_RANGE_AS_LITERAL_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  16. #include <boost/range/detail/as_literal.hpp>
  17. #else
  18. #include <boost/range/iterator_range.hpp>
  19. #include <boost/range/detail/str_types.hpp>
  20. #include <boost/detail/workaround.hpp>
  21. #include <cstring>
  22. #if !defined(BOOST_NO_CXX11_CHAR16_T) || !defined(BOOST_NO_CXX11_CHAR32_T)
  23. #include <string> // for std::char_traits
  24. #endif
  25. #ifndef BOOST_NO_CWCHAR
  26. #include <cwchar>
  27. #endif
  28. namespace boost
  29. {
  30. namespace range_detail
  31. {
  32. inline std::size_t length( const char* s )
  33. {
  34. return strlen( s );
  35. }
  36. #ifndef BOOST_NO_CXX11_CHAR16_T
  37. inline std::size_t length( const char16_t* s )
  38. {
  39. return std::char_traits<char16_t>::length( s );
  40. }
  41. #endif
  42. #ifndef BOOST_NO_CXX11_CHAR32_T
  43. inline std::size_t length( const char32_t* s )
  44. {
  45. return std::char_traits<char32_t>::length( s );
  46. }
  47. #endif
  48. #ifndef BOOST_NO_CWCHAR
  49. inline std::size_t length( const wchar_t* s )
  50. {
  51. return wcslen( s );
  52. }
  53. #endif
  54. //
  55. // Remark: the compiler cannot choose between T* and T[sz]
  56. // overloads, so we must put the T* internal to the
  57. // unconstrained version.
  58. //
  59. inline bool is_char_ptr( char* )
  60. {
  61. return true;
  62. }
  63. inline bool is_char_ptr( const char* )
  64. {
  65. return true;
  66. }
  67. #ifndef BOOST_NO_CXX11_CHAR16_T
  68. inline bool is_char_ptr( char16_t* )
  69. {
  70. return true;
  71. }
  72. inline bool is_char_ptr( const char16_t* )
  73. {
  74. return true;
  75. }
  76. #endif
  77. #ifndef BOOST_NO_CXX11_CHAR32_T
  78. inline bool is_char_ptr( char32_t* )
  79. {
  80. return true;
  81. }
  82. inline bool is_char_ptr( const char32_t* )
  83. {
  84. return true;
  85. }
  86. #endif
  87. #ifndef BOOST_NO_CWCHAR
  88. inline bool is_char_ptr( wchar_t* )
  89. {
  90. return true;
  91. }
  92. inline bool is_char_ptr( const wchar_t* )
  93. {
  94. return true;
  95. }
  96. #endif
  97. template< class T >
  98. inline long is_char_ptr( const T& /* r */ )
  99. {
  100. return 0L;
  101. }
  102. template< class T >
  103. inline iterator_range<T*>
  104. make_range( T* const r, bool )
  105. {
  106. return iterator_range<T*>( r, r + length(r) );
  107. }
  108. template< class T >
  109. inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<T>::type>
  110. make_range( T& r, long )
  111. {
  112. return boost::make_iterator_range( r );
  113. }
  114. }
  115. template< class Range >
  116. inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<Range>::type>
  117. as_literal( Range& r )
  118. {
  119. return range_detail::make_range( r, range_detail::is_char_ptr(r) );
  120. }
  121. template< class Range >
  122. inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<const Range>::type>
  123. as_literal( const Range& r )
  124. {
  125. return range_detail::make_range( r, range_detail::is_char_ptr(r) );
  126. }
  127. template< class Char, std::size_t sz >
  128. inline iterator_range<Char*> as_literal( Char (&arr)[sz] )
  129. {
  130. return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
  131. }
  132. template< class Char, std::size_t sz >
  133. inline iterator_range<const Char*> as_literal( const Char (&arr)[sz] )
  134. {
  135. return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
  136. }
  137. }
  138. #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  139. #endif