1 //Copyright (c) 2006-2013 Emil Dotchevski and Reverge Studios, Inc.
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //This file tests the N3757 syntax for adding error info to std::exception, which is
7 //different from the syntax used by Boost Exception.
9 #include <boost/exception/N3757.hpp>
10 #include <boost/exception/diagnostic_information.hpp>
11 #include <boost/detail/lightweight_test.hpp>
14 struct tag1
{ typedef int type
; };
15 struct tag2
{ typedef std::string type
; };
16 struct tag3
{ typedef int type
; };
18 struct my_error_code
{ int ec
; my_error_code(int ec
):ec(ec
){ } };
19 struct tag_error_code
{ typedef my_error_code type
; };
21 bool my_error_code_to_string_called
=false;
24 operator<<( std::ostream
& s
, my_error_code
const & x
)
26 my_error_code_to_string_called
=true;
27 return s
<< "my_error_code(" << x
.ec
<< ')';
30 struct my_exception
: virtual std::exception
, virtual boost::exception
{ };
40 boost::exception
& e
)
44 e
.set
<tag_error_code
>(42); //Implicit conversion
52 BOOST_TEST(e
.get
<tag1
>() && *e
.get
<tag1
>()==42);
53 BOOST_TEST(e
.get
<tag2
>() && *e
.get
<tag2
>()=="42");
54 BOOST_TEST(!e
.get
<tag3
>());
55 BOOST_TEST(e
.get
<tag_error_code
>() && e
.get
<tag_error_code
>()->ec
==42);
57 //Below we're verifying that an error code wrapped in a user-defined type
58 //invokes the correct op<< to convert the error code to string.
59 //Note that N3757 diagnostic_information uses different syntax, it is a
60 //member of of std::exception.
61 std::string di
=boost::diagnostic_information(e
);
62 BOOST_TEST(!di
.empty());
63 BOOST_TEST(my_error_code_to_string_called
);
66 return boost::report_errors();