to_string adjustments

[SVN r44192]
This commit is contained in:
Emil Dotchevski
2008-04-11 18:34:46 +00:00
parent 3c7f53176f
commit 761ae0bc1e
5 changed files with 82 additions and 39 deletions

View File

@ -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 };
};
}

View File

@ -14,25 +14,32 @@ namespace
boost
{
namespace
exception_detail
to_string_detail
{
template <bool>
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_dispatch
has_to_string_impl<T,true>
{
enum e { value=1 };
};
template <>
template <class T>
struct
has_to_string_dispatch<false>
has_to_string_impl<T,false>
{
enum e { value=0 };
enum e { value=1!=sizeof(to_string(*(T*)0)) };
};
}
template <class T>
std::string
to_string( T const & x, typename enable_if< is_output_streamable<T> >::type * = 0 )
typename enable_if<is_output_streamable<T>,std::string>::type
to_string( T const & x )
{
std::ostringstream out;
out << x;
@ -40,29 +47,11 @@ boost
}
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
{
enum e { value=has_to_string_dispatch<1!=sizeof(to_string(*(T*)0))>::value };
};
}
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

View File

@ -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 ;

View 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();
}

View File

@ -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) );