From f5dc3715bc3d0c15985846be75e4aa69623cd318 Mon Sep 17 00:00:00 2001 From: Emil Dotchevski Date: Sun, 10 Apr 2011 02:13:12 +0000 Subject: [PATCH] merged changes from trunk: better boost::diagnostic_information [SVN r71158] --- .../exception/diagnostic_information.hpp | 58 ++++++++++++------- include/boost/exception/info.hpp | 1 - test/diagnostic_information_test.cpp | 11 ++++ 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/include/boost/exception/diagnostic_information.hpp b/include/boost/exception/diagnostic_information.hpp index 7cbb355..ef89d73 100644 --- a/include/boost/exception/diagnostic_information.hpp +++ b/include/boost/exception/diagnostic_information.hpp @@ -14,6 +14,7 @@ #include #include +#include #include #ifndef BOOST_NO_RTTI #include @@ -85,19 +86,23 @@ boost char const * get_diagnostic_information( exception const & x, char const * header ) { - if( error_info_container * c=x.data_.get() ) #ifndef BOOST_NO_EXCEPTIONS - try - { + try + { #endif - return c->diagnostic_information(header); + error_info_container * c=x.data_.get(); + if( !c ) + x.data_.adopt(c=new exception_detail::error_info_container_impl); + char const * di=c->diagnostic_information(header); + BOOST_ASSERT(di!=0); + return di; #ifndef BOOST_NO_EXCEPTIONS - } - catch(...) - { - } + } + catch(...) + { + return 0; + } #endif - return 0; } inline @@ -122,18 +127,26 @@ boost std::ostringstream tmp; if( be ) { - if( char const * const * f=get_error_info(*be) ) - { - tmp << *f; - if( int const * l=get_error_info(*be) ) - tmp << '(' << *l << "): "; - } - tmp << "Throw in function "; - if( char const * const * fn=get_error_info(*be) ) - tmp << *fn; + char const * const * f=get_error_info(*be); + int const * l=get_error_info(*be); + char const * const * fn=get_error_info(*be); + if( !f && !l && !fn ) + tmp << "Throw location unknown (consider using BOOST_THROW_EXCEPTION)\n"; else - tmp << "(unknown)"; - tmp << '\n'; + { + if( f ) + { + tmp << *f; + if( int const * l=get_error_info(*be) ) + tmp << '(' << *l << "): "; + } + tmp << "Throw in function "; + if( char const * const * fn=get_error_info(*be) ) + tmp << *fn; + else + tmp << "(unknown)"; + tmp << '\n'; + } } #ifndef BOOST_NO_RTTI tmp << std::string("Dynamic exception type: ") << @@ -166,7 +179,10 @@ boost { #endif (void) exception_detail::diagnostic_information_impl(&e,0,false); - return exception_detail::get_diagnostic_information(e,0); + if( char const * di=exception_detail::get_diagnostic_information(e,0) ) + return di; + else + return "Failed to produce boost::diagnostic_information_what()"; #ifndef BOOST_NO_EXCEPTIONS } catch( diff --git a/include/boost/exception/info.hpp b/include/boost/exception/info.hpp index c918dbd..5530746 100644 --- a/include/boost/exception/info.hpp +++ b/include/boost/exception/info.hpp @@ -109,7 +109,6 @@ boost { if( header ) { - BOOST_ASSERT(*header!=0); std::ostringstream tmp; tmp << header; for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i ) diff --git a/test/diagnostic_information_test.cpp b/test/diagnostic_information_test.cpp index 8ced6e3..85e8a67 100644 --- a/test/diagnostic_information_test.cpp +++ b/test/diagnostic_information_test.cpp @@ -200,6 +200,17 @@ main() std::string di=current_exception_diagnostic_information(); test3(di); } + try + { + throw error4(); + } + catch( + error4 & x ) + { + std::string di1=boost::diagnostic_information(x); + std::string wh1=x.what(); + BOOST_TEST(wh1==di1); + } try { error4 x; x << tagged_int1(42) << tagged_int2(42);