Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50507 - in trunk: boost/exception libs/exception/example libs/exception/test
From: emil_at_[hidden]
Date: 2009-01-07 20:26:15


Author: emildotchevski
Date: 2009-01-07 20:26:15 EST (Wed, 07 Jan 2009)
New Revision: 50507
URL: http://svn.boost.org/trac/boost/changeset/50507

Log:
Implemented support for custom to_string overloads for converting boost::error_info objects to string by the boost::diagnostic_information function.
Text files modified:
   trunk/boost/exception/get_error_info.hpp | 2
   trunk/boost/exception/info.hpp | 10 ++++++
   trunk/libs/exception/example/example_io.cpp | 10 +++++++
   trunk/libs/exception/test/diagnostic_information_test.cpp | 54 +++++++++++++++++++++++++++++----------
   4 files changed, 60 insertions(+), 16 deletions(-)

Modified: trunk/boost/exception/get_error_info.hpp
==============================================================================
--- trunk/boost/exception/get_error_info.hpp (original)
+++ trunk/boost/exception/get_error_info.hpp 2009-01-07 20:26:15 EST (Wed, 07 Jan 2009)
@@ -20,7 +20,7 @@
         struct
         strwrap
             {
- std::string str;
+ std::string str;
             char const * ptr;
 
             explicit

Modified: trunk/boost/exception/info.hpp
==============================================================================
--- trunk/boost/exception/info.hpp (original)
+++ trunk/boost/exception/info.hpp 2009-01-07 20:26:15 EST (Wed, 07 Jan 2009)
@@ -17,6 +17,14 @@
     {
     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>
+ inline
     error_info<Tag,T>::
     error_info( value_type const & value ):
         value_(value)
@@ -45,7 +53,7 @@
     error_info<Tag,T>::
     value_as_string() const
         {
- return to_string_stub(value_);
+ return to_string_stub(*this);
         }
 
     namespace

Modified: trunk/libs/exception/example/example_io.cpp
==============================================================================
--- trunk/libs/exception/example/example_io.cpp (original)
+++ trunk/libs/exception/example/example_io.cpp 2009-01-07 20:26:15 EST (Wed, 07 Jan 2009)
@@ -19,6 +19,7 @@
 #include <errno.h>
 #include <string>
 #include <iostream>
+#include <sstream>
 
 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;
@@ -28,6 +29,15 @@
 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.
 
+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);
 

Modified: trunk/libs/exception/test/diagnostic_information_test.cpp
==============================================================================
--- trunk/libs/exception/test/diagnostic_information_test.cpp (original)
+++ trunk/libs/exception/test/diagnostic_information_test.cpp 2009-01-07 20:26:15 EST (Wed, 07 Jan 2009)
@@ -9,10 +9,21 @@
 #include <boost/detail/workaround.hpp>
 
 #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
-struct test_tag {};
+struct test_tag1 {};
+struct test_tag2 {};
 #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
 error1:
@@ -38,19 +49,30 @@
     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(di1.find("type:")!=std::string::npos);
+ BOOST_TEST(di1.find("error1")!=std::string::npos);
+#endif
+ 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(di.find("type:")!=std::string::npos);
- BOOST_TEST(di.find("error1")!=std::string::npos);
+ BOOST_TEST(di2.find("type:")!=std::string::npos);
+ BOOST_TEST(di2.find("error1")!=std::string::npos);
 #endif
- BOOST_TEST(di.find("test_tag")!=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(
     ... )
@@ -60,18 +82,22 @@
     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(
     ... )


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk