mirror of
https://github.com/boostorg/exception.git
synced 2025-07-12 03:56:33 +02:00
to_string adjustments
[SVN r44192]
This commit is contained in:
@ -12,7 +12,7 @@ namespace
|
||||
boost
|
||||
{
|
||||
namespace
|
||||
exception_detail
|
||||
to_string_detail
|
||||
{
|
||||
template <bool>
|
||||
struct
|
||||
@ -43,7 +43,7 @@ boost
|
||||
struct
|
||||
is_output_streamable
|
||||
{
|
||||
enum e { value=exception_detail::is_output_streamable_impl<T,CharT,Traits>::value };
|
||||
enum e { value=to_string_detail::is_output_streamable_impl<T,CharT,Traits>::value };
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -14,55 +14,44 @@ namespace
|
||||
boost
|
||||
{
|
||||
namespace
|
||||
exception_detail
|
||||
to_string_detail
|
||||
{
|
||||
template <bool>
|
||||
struct
|
||||
has_to_string_dispatch
|
||||
template <class T>
|
||||
typename disable_if<is_output_streamable<T>,char>::type to_string( T const & );
|
||||
|
||||
template <class,bool IsOutputStreamable>
|
||||
struct has_to_string_impl;
|
||||
|
||||
template <class T>
|
||||
struct
|
||||
has_to_string_impl<T,true>
|
||||
{
|
||||
enum e { value=1 };
|
||||
};
|
||||
|
||||
template <>
|
||||
struct
|
||||
has_to_string_dispatch<false>
|
||||
{
|
||||
enum e { value=0 };
|
||||
};
|
||||
|
||||
template <class T>
|
||||
std::string
|
||||
to_string( T const & x, typename enable_if< is_output_streamable<T> >::type * = 0 )
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << x;
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
char to_string( T const &, typename disable_if< is_output_streamable<T> >::type * = 0 );
|
||||
|
||||
template <class T>
|
||||
struct
|
||||
has_to_string_impl
|
||||
has_to_string_impl<T,false>
|
||||
{
|
||||
enum e { value=has_to_string_dispatch<1!=sizeof(to_string(*(T*)0))>::value };
|
||||
enum e { value=1!=sizeof(to_string(*(T*)0)) };
|
||||
};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
typename enable_if<is_output_streamable<T>,std::string>::type
|
||||
to_string( T const & x )
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << x;
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
struct
|
||||
has_to_string
|
||||
{
|
||||
enum e { value=exception_detail::has_to_string_impl<T>::value };
|
||||
enum e { value=to_string_detail::has_to_string_impl<T,is_output_streamable<T>::value>::value };
|
||||
};
|
||||
|
||||
template <class T>
|
||||
std::string
|
||||
to_string( T const & x, typename enable_if< is_output_streamable<T> >::type * = 0 )
|
||||
{
|
||||
return exception_detail::to_string(x);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -9,6 +9,7 @@ import testing ;
|
||||
|
||||
#to_string
|
||||
run is_output_streamable_test.cpp ;
|
||||
run has_to_string_test.cpp ;
|
||||
run to_string_test.cpp ;
|
||||
run to_string_stub_test.cpp ;
|
||||
compile-fail to_string_fail.cpp ;
|
||||
|
57
test/has_to_string_test.cpp
Normal file
57
test/has_to_string_test.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
|
||||
|
||||
//Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/exception/to_string.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
namespace
|
||||
n1
|
||||
{
|
||||
struct
|
||||
c1
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
namespace
|
||||
n2
|
||||
{
|
||||
struct
|
||||
c2
|
||||
{
|
||||
};
|
||||
|
||||
std::string
|
||||
to_string( c2 const & )
|
||||
{
|
||||
return "c2";
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
n3
|
||||
{
|
||||
struct
|
||||
c3
|
||||
{
|
||||
};
|
||||
|
||||
std::ostream &
|
||||
operator<<( std::ostream & s, c3 const & )
|
||||
{
|
||||
return s << "c3";
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace boost;
|
||||
BOOST_TEST( !has_to_string<n1::c1>::value );
|
||||
BOOST_TEST( has_to_string<n2::c2>::value );
|
||||
BOOST_TEST( has_to_string<n3::c3>::value );
|
||||
BOOST_TEST( has_to_string<int>::value );
|
||||
return boost::report_errors();
|
||||
}
|
@ -49,10 +49,6 @@ int
|
||||
main()
|
||||
{
|
||||
using namespace boost;
|
||||
BOOST_TEST( !has_to_string<n1::c1>::value );
|
||||
BOOST_TEST( has_to_string<n2::c2>::value );
|
||||
BOOST_TEST( has_to_string<n3::c3>::value );
|
||||
BOOST_TEST( has_to_string<int>::value );
|
||||
BOOST_TEST( "c2"==to_string(n2::c2()) );
|
||||
BOOST_TEST( "c3"==to_string(n3::c3()) );
|
||||
BOOST_TEST( "42"==to_string(42) );
|
||||
|
Reference in New Issue
Block a user