diff --git a/example/example_io.cpp b/example/example_io.cpp index 1c15672..4b3a5a3 100644 --- a/example/example_io.cpp +++ b/example/example_io.cpp @@ -19,6 +19,7 @@ #include #include #include +#include typedef boost::error_info errno_info; typedef boost::error_info > file_stream_info; @@ -28,6 +29,15 @@ typedef boost::error_info file_name_src_in typedef boost::error_info file_name_dst_info; //The destination file name of a failed copy operation. typedef boost::error_info 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"; size_t const data_size = sizeof(data); diff --git a/include/boost/exception/get_error_info.hpp b/include/boost/exception/get_error_info.hpp index e45d3d6..1bca37b 100644 --- a/include/boost/exception/get_error_info.hpp +++ b/include/boost/exception/get_error_info.hpp @@ -20,7 +20,7 @@ boost struct strwrap { - std::string str; + std::string str; char const * ptr; explicit diff --git a/include/boost/exception/info.hpp b/include/boost/exception/info.hpp index a0892c9..64097af 100644 --- a/include/boost/exception/info.hpp +++ b/include/boost/exception/info.hpp @@ -15,6 +15,14 @@ namespace boost { + template + inline + typename enable_if,std::string>::type + to_string( error_info const & x ) + { + return to_string(x.value()); + } + template inline error_info:: @@ -45,7 +53,7 @@ boost error_info:: value_as_string() const { - return to_string_stub(value_); + return to_string_stub(*this); } namespace diff --git a/test/diagnostic_information_test.cpp b/test/diagnostic_information_test.cpp index 56be4d8..669b7b0 100644 --- a/test/diagnostic_information_test.cpp +++ b/test/diagnostic_information_test.cpp @@ -9,10 +9,21 @@ #include #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610)) -struct test_tag {}; +struct test_tag1 {}; +struct test_tag2 {}; #endif -typedef boost::error_info tag_int; +typedef boost::error_info tagged_int1; +typedef boost::error_info tagged_int2; + +std::string +to_string( tagged_int2 const & x ) + { + if( x.value()==42 ) + return "fourty-two"; + else + return "bad value"; + } struct error1: @@ -38,19 +49,30 @@ main() using namespace boost; try { - error1 x; x << tag_int(42); + error1 x; x << tagged_int1(42) << tagged_int2(42); BOOST_TEST(x.what()==std::string("error1")); throw x; } catch( 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 - BOOST_TEST(di.find("type:")!=std::string::npos); - BOOST_TEST(di.find("error1")!=std::string::npos); + BOOST_TEST(di1.find("type:")!=std::string::npos); + BOOST_TEST(di1.find("error1")!=std::string::npos); #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( ... ) @@ -60,18 +82,22 @@ main() try { error2 x; - x << tag_int(1); + x << tagged_int1(42) << tagged_int2(42); throw x; } catch( boost::exception & x ) { - std::string w1 = diagnostic_information(x); - x << tag_int(2); - std::string w2 = diagnostic_information(x); - BOOST_TEST( w1!=w2 ); - BOOST_TEST(w1.find("test_tag")!=std::string::npos); - BOOST_TEST(w2.find("test_tag")!=std::string::npos); + std::string di1 = diagnostic_information(x); + x << tagged_int1(2) << tagged_int2(2); + std::string di2 = diagnostic_information(x); + BOOST_TEST( di1!=di2 ); + 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); + 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( ... )