Compare commits

...

4 Commits

Author SHA1 Message Date
nobody ae524a712c This commit was manufactured by cvs2svn to create tag
'Version_1_30_2'.

[SVN r19685]
2003-08-18 18:40:31 +00:00
Aleksey Gurtovoy f0c79866ad merge changes from the main trunk to get in sync with the header
[SVN r19669]
2003-08-17 21:02:53 +00:00
Terje Slettebø 3804825f6c no message
[SVN r18238]
2003-04-12 08:02:14 +00:00
Terje Slettebø fdd5ca3353 Merged from trunk
[SVN r18237]
2003-04-11 22:15:04 +00:00
2 changed files with 45 additions and 35 deletions
+31 -33
View File
@@ -18,6 +18,7 @@
#include <typeinfo>
#include <boost/config.hpp>
#include <boost/limits.hpp>
#include <boost/throw_exception.hpp>
#include <boost/type_traits/is_pointer.hpp>
#ifdef BOOST_NO_STRINGSTREAM
@@ -29,51 +30,47 @@
#if defined(BOOST_NO_STRINGSTREAM) || \
defined(BOOST_NO_STD_WSTRING) || \
defined(BOOST_NO_STD_LOCALE) || \
defined(BOOST_NO_CWCHAR) || \
defined(BOOST_MSVC) && (BOOST_MSVC <= 1200)
defined(BOOST_NO_INTRINSIC_WCHAR_T)
#define DISABLE_WIDE_CHAR_SUPPORT
#endif
#ifdef BOOST_NO_INTRINSIC_WCHAR_T
#include <cwchar>
#endif
namespace boost
{
// exception used to indicate runtime lexical_cast failure
class bad_lexical_cast : public std::bad_cast
{
public:
bad_lexical_cast() :
source(&typeid(void)), target(&typeid(void))
{
}
bad_lexical_cast(
const std::type_info &s,
const std::type_info &t) :
source(&s), target(&t)
{
}
const std::type_info &source_type() const
{
return *source;
}
const std::type_info &target_type() const
{
return *target;
}
virtual const char *what() const throw()
{
return "bad lexical cast: "
"source type value could not be interpreted as target";
}
virtual ~bad_lexical_cast() throw()
{
}
private:
const std::type_info *source;
const std::type_info *target;
};
namespace detail // actual underlying concrete exception type
{
template<typename Target, typename Source>
class no_lexical_conversion : public bad_lexical_cast
{
public:
no_lexical_conversion()
: description(
std::string() + "bad lexical cast: " +
"source type value could not be interpreted as target, Target=" +
typeid(Target).name() + ", Source=" + typeid(Source).name())
{
}
virtual ~no_lexical_conversion() throw()
{
}
virtual const char *what() const throw()
{
return description.c_str();
}
private:
const std::string description; // static initialization fails on MSVC6
};
}
namespace detail // selectors for choosing stream character type
{
template<typename Type>
@@ -144,7 +141,7 @@ namespace boost
}
bool operator<<(const Source &input)
{
return stream << input;
return !(stream << input).fail();
}
template<typename InputStreamable>
bool operator>>(InputStreamable &output)
@@ -190,7 +187,7 @@ namespace boost
Target result;
if(!(interpreter << arg && interpreter >> result))
throw detail::no_lexical_conversion<Target, Source>();
throw_exception(bad_lexical_cast(typeid(Target), typeid(Source)));
return result;
}
}
@@ -205,3 +202,4 @@ namespace boost
#undef DISABLE_WIDE_CHAR_SUPPORT
#endif
+14 -2
View File
@@ -25,8 +25,7 @@
#if defined(BOOST_NO_STRINGSTREAM) || \
defined(BOOST_NO_STD_WSTRING) || \
defined(BOOST_NO_STD_LOCALE) || \
defined(BOOST_NO_CWCHAR) || \
defined(BOOST_MSVC) && (BOOST_MSVC <= 1200)
defined(BOOST_NO_INTRINSIC_WCHAR_T)
#define DISABLE_WIDE_CHAR_SUPPORT
#endif
@@ -37,6 +36,7 @@ void test_conversion_to_int();
void test_conversion_to_double();
void test_conversion_to_bool();
void test_conversion_to_string();
void test_conversion_from_to_wchar_t_alias();
void test_conversion_to_pointer();
void test_conversion_from_wchar_t();
void test_conversion_to_wchar_t();
@@ -51,6 +51,7 @@ unit_test_framework::test_suite *init_unit_test_suite(int, char **)
suite->add(BOOST_TEST_CASE(test_conversion_to_int));
suite->add(BOOST_TEST_CASE(test_conversion_to_double));
suite->add(BOOST_TEST_CASE(test_conversion_to_bool));
suite->add(BOOST_TEST_CASE(test_conversion_from_to_wchar_t_alias));
suite->add(BOOST_TEST_CASE(test_conversion_to_pointer));
suite->add(BOOST_TEST_CASE(test_conversion_to_string));
#ifndef DISABLE_WIDE_CHAR_SUPPORT
@@ -180,6 +181,17 @@ void test_conversion_to_string()
BOOST_CHECK_EQUAL("", lexical_cast<std::string>(std::string("")));
}
void test_conversion_from_to_wchar_t_alias()
{
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned short>("123"));
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned int>("123"));
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned long>("123"));
BOOST_CHECK_EQUAL(std::string("123"),
lexical_cast<std::string>(static_cast<unsigned short>(123)));
BOOST_CHECK_EQUAL(std::string("123"), lexical_cast<std::string>(123u));
BOOST_CHECK_EQUAL(std::string("123"), lexical_cast<std::string>(123ul));
}
void test_conversion_to_pointer()
{
BOOST_CHECK_THROW(lexical_cast<char *>("Test"), boost::bad_lexical_cast);