Implemented support for custom to_string overloads for converting boost::error_info objects to string by the boost::diagnostic_information function.

[SVN r50507]
This commit is contained in:
Emil Dotchevski
2009-01-08 01:26:15 +00:00
parent b52e6c7d6b
commit f28285c073
4 changed files with 60 additions and 16 deletions

View File

@ -19,6 +19,7 @@
#include <errno.h> #include <errno.h>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <sstream>
typedef boost::error_info<struct tag_errno,int> errno_info; typedef boost::error_info<struct tag_errno,int> errno_info;
typedef boost::error_info<struct tag_file_stream,boost::weak_ptr<FILE> > file_stream_info; typedef boost::error_info<struct tag_file_stream,boost::weak_ptr<FILE> > file_stream_info;
@ -28,6 +29,15 @@ typedef boost::error_info<struct tag_file_name_src,std::string> file_name_src_in
typedef boost::error_info<struct tag_file_name_dst,std::string> file_name_dst_info; //The destination file name of a failed copy operation. typedef boost::error_info<struct tag_file_name_dst,std::string> file_name_dst_info; //The destination file name of a failed copy operation.
typedef boost::error_info<struct tag_function,std::string> function_info; //The name of the C function which reported the failure. typedef boost::error_info<struct tag_function,std::string> function_info; //The name of the C function which reported the failure.
std::string
to_string( errno_info const & e )
{
int en=e.value();
std::ostringstream s;
s << en << ", OS says \"" << strerror(en) << "\"";
return s.str();
}
char const data[] = "example"; char const data[] = "example";
size_t const data_size = sizeof(data); size_t const data_size = sizeof(data);

View File

@ -15,6 +15,14 @@
namespace namespace
boost boost
{ {
template <class Tag,class T>
inline
typename enable_if<has_to_string<T>,std::string>::type
to_string( error_info<Tag,T> const & x )
{
return to_string(x.value());
}
template <class Tag,class T> template <class Tag,class T>
inline inline
error_info<Tag,T>:: error_info<Tag,T>::
@ -45,7 +53,7 @@ boost
error_info<Tag,T>:: error_info<Tag,T>::
value_as_string() const value_as_string() const
{ {
return to_string_stub(value_); return to_string_stub(*this);
} }
namespace namespace

View File

@ -9,10 +9,21 @@
#include <boost/detail/workaround.hpp> #include <boost/detail/workaround.hpp>
#if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610)) #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
struct test_tag {}; struct test_tag1 {};
struct test_tag2 {};
#endif #endif
typedef boost::error_info<struct test_tag,int> tag_int; typedef boost::error_info<struct test_tag1,int> tagged_int1;
typedef boost::error_info<struct test_tag2,int> tagged_int2;
std::string
to_string( tagged_int2 const & x )
{
if( x.value()==42 )
return "fourty-two";
else
return "bad value";
}
struct struct
error1: error1:
@ -38,19 +49,30 @@ main()
using namespace boost; using namespace boost;
try try
{ {
error1 x; x << tag_int(42); error1 x; x << tagged_int1(42) << tagged_int2(42);
BOOST_TEST(x.what()==std::string("error1")); BOOST_TEST(x.what()==std::string("error1"));
throw x; throw x;
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
std::string di=boost::diagnostic_information(x); std::string di1=boost::diagnostic_information(x);
x << tagged_int1(2) << tagged_int2(2);
std::string di2 = diagnostic_information(x);
#ifndef BOOST_NO_RTTI #ifndef BOOST_NO_RTTI
BOOST_TEST(di.find("type:")!=std::string::npos); BOOST_TEST(di1.find("type:")!=std::string::npos);
BOOST_TEST(di.find("error1")!=std::string::npos); BOOST_TEST(di1.find("error1")!=std::string::npos);
#endif #endif
BOOST_TEST(di.find("test_tag")!=std::string::npos); BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
#ifndef BOOST_NO_RTTI
BOOST_TEST(di2.find("type:")!=std::string::npos);
BOOST_TEST(di2.find("error1")!=std::string::npos);
#endif
BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
BOOST_TEST(di2.find("bad value")!=std::string::npos);
} }
catch( catch(
... ) ... )
@ -60,18 +82,22 @@ main()
try try
{ {
error2 x; error2 x;
x << tag_int(1); x << tagged_int1(42) << tagged_int2(42);
throw x; throw x;
} }
catch( catch(
boost::exception & x ) boost::exception & x )
{ {
std::string w1 = diagnostic_information(x); std::string di1 = diagnostic_information(x);
x << tag_int(2); x << tagged_int1(2) << tagged_int2(2);
std::string w2 = diagnostic_information(x); std::string di2 = diagnostic_information(x);
BOOST_TEST( w1!=w2 ); BOOST_TEST( di1!=di2 );
BOOST_TEST(w1.find("test_tag")!=std::string::npos); BOOST_TEST(di1.find("test_tag1")!=std::string::npos);
BOOST_TEST(w2.find("test_tag")!=std::string::npos); BOOST_TEST(di1.find("test_tag2")!=std::string::npos);
BOOST_TEST(di1.find("fourty-two")!=std::string::npos);
BOOST_TEST(di2.find("test_tag1")!=std::string::npos);
BOOST_TEST(di2.find("test_tag2")!=std::string::npos);
BOOST_TEST(di2.find("bad value")!=std::string::npos);
} }
catch( catch(
... ) ... )