lexical_cast_old.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2021.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // what: lexical_cast custom keyword cast
  10. // who: contributed by Kevlin Henney,
  11. // enhanced with contributions from Terje Slettebo,
  12. // with additional fixes and suggestions from Gennaro Prota,
  13. // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
  14. // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
  15. // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters
  16. // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2014
  17. #ifndef BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
  18. #define BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
  24. #define BOOST_LCAST_NO_WCHAR_T
  25. #endif
  26. #include <climits>
  27. #include <cstddef>
  28. #include <string>
  29. #include <cstring>
  30. #include <cstdio>
  31. #include <boost/limits.hpp>
  32. #include <boost/type_traits/is_pointer.hpp>
  33. #include <boost/static_assert.hpp>
  34. #include <boost/detail/lcast_precision.hpp>
  35. #include <boost/detail/workaround.hpp>
  36. #ifdef BOOST_NO_STRINGSTREAM
  37. #include <strstream>
  38. #else
  39. #include <sstream>
  40. #endif
  41. #include <boost/lexical_cast/bad_lexical_cast.hpp>
  42. #include <boost/lexical_cast/detail/widest_char.hpp>
  43. namespace boost {
  44. namespace detail
  45. {
  46. // selectors for choosing stream character type
  47. template<typename Type>
  48. struct stream_char
  49. {
  50. typedef char type;
  51. };
  52. #ifndef BOOST_LCAST_NO_WCHAR_T
  53. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  54. template<>
  55. struct stream_char<wchar_t>
  56. {
  57. typedef wchar_t type;
  58. };
  59. #endif
  60. template<>
  61. struct stream_char<wchar_t *>
  62. {
  63. typedef wchar_t type;
  64. };
  65. template<>
  66. struct stream_char<const wchar_t *>
  67. {
  68. typedef wchar_t type;
  69. };
  70. template<>
  71. struct stream_char<std::wstring>
  72. {
  73. typedef wchar_t type;
  74. };
  75. #endif
  76. // stream wrapper for handling lexical conversions
  77. template<typename Target, typename Source, typename Traits>
  78. class lexical_stream
  79. {
  80. private:
  81. typedef typename widest_char<
  82. typename stream_char<Target>::type,
  83. typename stream_char<Source>::type>::type char_type;
  84. typedef Traits traits_type;
  85. public:
  86. lexical_stream(char_type* = 0, char_type* = 0)
  87. {
  88. stream.unsetf(std::ios::skipws);
  89. lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
  90. }
  91. ~lexical_stream()
  92. {
  93. #if defined(BOOST_NO_STRINGSTREAM)
  94. stream.freeze(false);
  95. #endif
  96. }
  97. bool operator<<(const Source &input)
  98. {
  99. return !(stream << input).fail();
  100. }
  101. template<typename InputStreamable>
  102. bool operator>>(InputStreamable &output)
  103. {
  104. return !is_pointer<InputStreamable>::value &&
  105. stream >> output &&
  106. stream.get() == traits_type::eof();
  107. }
  108. bool operator>>(std::string &output)
  109. {
  110. #if defined(BOOST_NO_STRINGSTREAM)
  111. stream << '\0';
  112. #endif
  113. stream.str().swap(output);
  114. return true;
  115. }
  116. #ifndef BOOST_LCAST_NO_WCHAR_T
  117. bool operator>>(std::wstring &output)
  118. {
  119. stream.str().swap(output);
  120. return true;
  121. }
  122. #endif
  123. private:
  124. #if defined(BOOST_NO_STRINGSTREAM)
  125. std::strstream stream;
  126. #elif defined(BOOST_NO_STD_LOCALE)
  127. std::stringstream stream;
  128. #else
  129. std::basic_stringstream<char_type,traits_type> stream;
  130. #endif
  131. };
  132. }
  133. // call-by-value fallback version (deprecated)
  134. template<typename Target, typename Source>
  135. Target lexical_cast(Source arg)
  136. {
  137. typedef typename detail::widest_char<
  138. BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
  139. , BOOST_DEDUCED_TYPENAME detail::stream_char<Source>::type
  140. >::type char_type;
  141. typedef std::char_traits<char_type> traits;
  142. detail::lexical_stream<Target, Source, traits> interpreter;
  143. Target result;
  144. if(!(interpreter << arg && interpreter >> result))
  145. boost::conversion::detail::throw_bad_cast<Source, Target>();
  146. return result;
  147. }
  148. } // namespace boost
  149. #undef BOOST_LCAST_NO_WCHAR_T
  150. #endif // BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP