Fixed so it doesn't trim any whitespace when converting, fixed reversed bad_lexical_cast constructor parameters, changed to pass-by-const reference

[SVN r29629]
This commit is contained in:
Terje Slettebø
2005-06-16 18:27:22 +00:00
parent cfe358b570
commit 461dce3851

View File

@@ -12,7 +12,7 @@
// with additional fixes and suggestions from Gennaro Prota, // with additional fixes and suggestions from Gennaro Prota,
// Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
// and other Boosters // and other Boosters
// when: November 2000, March 2003 // when: November 2000, March 2003, June 2005
#include <string> #include <string>
#include <typeinfo> #include <typeinfo>
@@ -45,9 +45,9 @@ namespace boost
{ {
} }
bad_lexical_cast( bad_lexical_cast(
const std::type_info &s, const std::type_info &source_type,
const std::type_info &t) : const std::type_info &target_type) :
source(&s), target(&t) source(&source_type), target(&target_type)
{ {
} }
const std::type_info &source_type() const const std::type_info &source_type() const
@@ -123,6 +123,11 @@ namespace boost
template<typename Target, typename Source> template<typename Target, typename Source>
class lexical_stream class lexical_stream
{ {
private:
typedef typename widest_char<
typename stream_char<Target>::type,
typename stream_char<Source>::type>::type char_type;
public: public:
lexical_stream() lexical_stream()
{ {
@@ -148,7 +153,7 @@ namespace boost
{ {
return !is_pointer<InputStreamable>::value && return !is_pointer<InputStreamable>::value &&
stream >> output && stream >> output &&
(stream >> std::ws).eof(); stream.get() == std::char_traits<char_type>::eof();
} }
bool operator>>(std::string &output) bool operator>>(std::string &output)
{ {
@@ -166,10 +171,6 @@ namespace boost
} }
#endif #endif
private: private:
typedef typename widest_char<
typename stream_char<Target>::type,
typename stream_char<Source>::type>::type char_type;
#if defined(BOOST_NO_STRINGSTREAM) #if defined(BOOST_NO_STRINGSTREAM)
std::strstream stream; std::strstream stream;
#elif defined(BOOST_NO_STD_LOCALE) #elif defined(BOOST_NO_STD_LOCALE)
@@ -180,6 +181,42 @@ namespace boost
}; };
} }
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// call-by-const reference version
namespace detail
{
template<class T>
struct array_to_pointer_decay
{
typedef T type;
};
template<class T, size_t N>
struct array_to_pointer_decay<T[N]>
{
typedef const T * type;
};
}
template<typename Target, typename Source>
Target lexical_cast(const Source &arg)
{
typedef typename detail::array_to_pointer_decay<Source>::type NewSource;
detail::lexical_stream<Target, NewSource> interpreter;
Target result;
if(!(interpreter << arg && interpreter >> result))
throw_exception(bad_lexical_cast(typeid(NewSource), typeid(Target)));
return result;
}
#else
// call-by-value fallback version (deprecated)
template<typename Target, typename Source> template<typename Target, typename Source>
Target lexical_cast(Source arg) Target lexical_cast(Source arg)
{ {
@@ -187,12 +224,14 @@ namespace boost
Target result; Target result;
if(!(interpreter << arg && interpreter >> result)) if(!(interpreter << arg && interpreter >> result))
throw_exception(bad_lexical_cast(typeid(Target), typeid(Source))); throw_exception(bad_lexical_cast(typeid(Source), typeid(Target)));
return result; return result;
} }
#endif
} }
// Copyright Kevlin Henney, 2000-2003. All rights reserved. // Copyright Kevlin Henney, 2000-2005. All rights reserved.
// //
// Distributed under the Boost Software License, Version 1.0. (See // Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at // accompanying file LICENSE_1_0.txt or copy at
@@ -200,4 +239,3 @@ namespace boost
#undef DISABLE_WIDE_CHAR_SUPPORT #undef DISABLE_WIDE_CHAR_SUPPORT
#endif #endif