lexical_cast_old.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2014.
  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/mpl/if.hpp>
  33. #include <boost/type_traits/is_pointer.hpp>
  34. #include <boost/static_assert.hpp>
  35. #include <boost/detail/lcast_precision.hpp>
  36. #include <boost/detail/workaround.hpp>
  37. #ifdef BOOST_NO_STRINGSTREAM
  38. #include <strstream>
  39. #else
  40. #include <sstream>
  41. #endif
  42. #include <boost/lexical_cast/bad_lexical_cast.hpp>
  43. #include <boost/lexical_cast/detail/widest_char.hpp>
  44. namespace boost {
  45. namespace detail
  46. {
  47. // selectors for choosing stream character type
  48. template<typename Type>
  49. struct stream_char
  50. {
  51. typedef char type;
  52. };
  53. #ifndef BOOST_LCAST_NO_WCHAR_T
  54. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  55. template<>
  56. struct stream_char<wchar_t>
  57. {
  58. typedef wchar_t type;
  59. };
  60. #endif
  61. template<>
  62. struct stream_char<wchar_t *>
  63. {
  64. typedef wchar_t type;
  65. };
  66. template<>
  67. struct stream_char<const wchar_t *>
  68. {
  69. typedef wchar_t type;
  70. };
  71. template<>
  72. struct stream_char<std::wstring>
  73. {
  74. typedef wchar_t type;
  75. };
  76. #endif
  77. // stream wrapper for handling lexical conversions
  78. template<typename Target, typename Source, typename Traits>
  79. class lexical_stream
  80. {
  81. private:
  82. typedef typename widest_char<
  83. typename stream_char<Target>::type,
  84. typename stream_char<Source>::type>::type char_type;
  85. typedef Traits traits_type;
  86. public:
  87. lexical_stream(char_type* = 0, char_type* = 0)
  88. {
  89. stream.unsetf(std::ios::skipws);
  90. lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
  91. }
  92. ~lexical_stream()
  93. {
  94. #if defined(BOOST_NO_STRINGSTREAM)
  95. stream.freeze(false);
  96. #endif
  97. }
  98. bool operator<<(const Source &input)
  99. {
  100. return !(stream << input).fail();
  101. }
  102. template<typename InputStreamable>
  103. bool operator>>(InputStreamable &output)
  104. {
  105. return !is_pointer<InputStreamable>::value &&
  106. stream >> output &&
  107. stream.get() == traits_type::eof();
  108. }
  109. bool operator>>(std::string &output)
  110. {
  111. #if defined(BOOST_NO_STRINGSTREAM)
  112. stream << '\0';
  113. #endif
  114. stream.str().swap(output);
  115. return true;
  116. }
  117. #ifndef BOOST_LCAST_NO_WCHAR_T
  118. bool operator>>(std::wstring &output)
  119. {
  120. stream.str().swap(output);
  121. return true;
  122. }
  123. #endif
  124. private:
  125. #if defined(BOOST_NO_STRINGSTREAM)
  126. std::strstream stream;
  127. #elif defined(BOOST_NO_STD_LOCALE)
  128. std::stringstream stream;
  129. #else
  130. std::basic_stringstream<char_type,traits_type> stream;
  131. #endif
  132. };
  133. }
  134. // call-by-value fallback version (deprecated)
  135. template<typename Target, typename Source>
  136. Target lexical_cast(Source arg)
  137. {
  138. typedef typename detail::widest_char<
  139. BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
  140. , BOOST_DEDUCED_TYPENAME detail::stream_char<Source>::type
  141. >::type char_type;
  142. typedef std::char_traits<char_type> traits;
  143. detail::lexical_stream<Target, Source, traits> interpreter;
  144. Target result;
  145. if(!(interpreter << arg && interpreter >> result))
  146. boost::conversion::detail::throw_bad_cast<Source, Target>();
  147. return result;
  148. }
  149. } // namespace boost
  150. #undef BOOST_LCAST_NO_WCHAR_T
  151. #endif // BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP