Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:44:54

0001 // Copyright Kevlin Henney, 2000-2005.
0002 // Copyright Alexander Nasonov, 2006-2010.
0003 // Copyright Antony Polukhin, 2011-2023.
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See
0006 // accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 //
0009 // what:  lexical_cast custom keyword cast
0010 // who:   contributed by Kevlin Henney,
0011 //        enhanced with contributions from Terje Slettebo,
0012 //        with additional fixes and suggestions from Gennaro Prota,
0013 //        Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
0014 //        Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
0015 //        Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters
0016 // when:  November 2000, March 2003, June 2005, June 2006, March 2011 - 2014
0017 
0018 #ifndef BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
0019 #define BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
0020 
0021 #include <boost/config.hpp>
0022 #ifdef BOOST_HAS_PRAGMA_ONCE
0023 #   pragma once
0024 #endif
0025 
0026 #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
0027 #define BOOST_LCAST_NO_WCHAR_T
0028 #endif
0029 
0030 #include <climits>
0031 #include <cstddef>
0032 #include <string>
0033 #include <cstring>
0034 #include <cstdio>
0035 #include <boost/limits.hpp>
0036 #include <boost/type_traits/is_pointer.hpp>
0037 #include <boost/detail/lcast_precision.hpp>
0038 #include <boost/detail/workaround.hpp>
0039 
0040 #ifdef BOOST_NO_STRINGSTREAM
0041 #include <strstream>
0042 #else
0043 #include <sstream>
0044 #endif
0045 
0046 #include <boost/lexical_cast/bad_lexical_cast.hpp>
0047 #include <boost/lexical_cast/detail/widest_char.hpp>
0048 
0049 namespace boost {
0050     namespace detail
0051     {
0052 
0053         // selectors for choosing stream character type
0054         template<typename Type>
0055         struct stream_char
0056         {
0057             typedef char type;
0058         };
0059 
0060 #ifndef BOOST_LCAST_NO_WCHAR_T
0061 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
0062         template<>
0063         struct stream_char<wchar_t>
0064         {
0065             typedef wchar_t type;
0066         };
0067 #endif
0068 
0069         template<>
0070         struct stream_char<wchar_t *>
0071         {
0072             typedef wchar_t type;
0073         };
0074 
0075         template<>
0076         struct stream_char<const wchar_t *>
0077         {
0078             typedef wchar_t type;
0079         };
0080 
0081         template<>
0082         struct stream_char<std::wstring>
0083         {
0084             typedef wchar_t type;
0085         };
0086 #endif
0087 
0088         // stream wrapper for handling lexical conversions
0089         template<typename Target, typename Source, typename Traits>
0090         class lexical_stream
0091         {
0092         private:
0093             typedef typename widest_char<
0094                 typename stream_char<Target>::type,
0095                 typename stream_char<Source>::type>::type char_type;
0096 
0097             typedef Traits traits_type;
0098 
0099         public:
0100             lexical_stream(char_type* = 0, char_type* = 0)
0101             {
0102                 stream.unsetf(std::ios::skipws);
0103                 lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
0104             }
0105             ~lexical_stream()
0106             {
0107                 #if defined(BOOST_NO_STRINGSTREAM)
0108                 stream.freeze(false);
0109                 #endif
0110             }
0111             bool operator<<(const Source &input)
0112             {
0113                 return !(stream << input).fail();
0114             }
0115             template<typename InputStreamable>
0116             bool operator>>(InputStreamable &output)
0117             {
0118                 return !is_pointer<InputStreamable>::value &&
0119                        stream >> output &&
0120                        stream.get() == traits_type::eof();
0121             }
0122 
0123             bool operator>>(std::string &output)
0124             {
0125                 #if defined(BOOST_NO_STRINGSTREAM)
0126                 stream << '\0';
0127                 #endif
0128                 stream.str().swap(output);
0129                 return true;
0130             }
0131             #ifndef BOOST_LCAST_NO_WCHAR_T
0132             bool operator>>(std::wstring &output)
0133             {
0134                 stream.str().swap(output);
0135                 return true;
0136             }
0137             #endif
0138 
0139         private:
0140             #if defined(BOOST_NO_STRINGSTREAM)
0141             std::strstream stream;
0142             #elif defined(BOOST_NO_STD_LOCALE)
0143             std::stringstream stream;
0144             #else
0145             std::basic_stringstream<char_type,traits_type> stream;
0146             #endif
0147         };
0148     }
0149 
0150     // call-by-value fallback version (deprecated)
0151 
0152     template<typename Target, typename Source>
0153     Target lexical_cast(Source arg)
0154     {
0155         typedef typename detail::widest_char<
0156             typename detail::stream_char<Target>::type
0157           , typename detail::stream_char<Source>::type
0158         >::type char_type;
0159 
0160         typedef std::char_traits<char_type> traits;
0161         detail::lexical_stream<Target, Source, traits> interpreter;
0162         Target result;
0163 
0164         if(!(interpreter << arg && interpreter >> result))
0165             boost::conversion::detail::throw_bad_cast<Source, Target>();
0166         return result;
0167     }
0168 
0169 } // namespace boost
0170 
0171 #undef BOOST_LCAST_NO_WCHAR_T
0172 
0173 #endif // BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
0174