]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //Copyright (c) 2006-2013 Emil Dotchevski and Reverge Studios, Inc. |
2 | ||
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) | |
5 | ||
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. | |
8 | ||
9 | #include <boost/exception/N3757.hpp> | |
10 | #include <boost/exception/diagnostic_information.hpp> | |
11 | #include <boost/detail/lightweight_test.hpp> | |
12 | #include <ostream> | |
13 | ||
14 | struct tag1 { typedef int type; }; | |
15 | struct tag2 { typedef std::string type; }; | |
16 | struct tag3 { typedef int type; }; | |
17 | ||
18 | struct my_error_code { int ec; my_error_code(int ec):ec(ec){ } }; | |
19 | struct tag_error_code { typedef my_error_code type; }; | |
20 | ||
21 | bool my_error_code_to_string_called=false; | |
22 | ||
23 | std::ostream & | |
24 | operator<<( std::ostream & s, my_error_code const & x ) | |
25 | { | |
26 | my_error_code_to_string_called=true; | |
27 | return s << "my_error_code(" << x.ec << ')'; | |
28 | } | |
29 | ||
30 | struct my_exception: virtual std::exception, virtual boost::exception { }; | |
31 | ||
32 | int | |
33 | main() | |
34 | { | |
35 | try | |
36 | { | |
37 | throw my_exception(); | |
38 | } | |
39 | catch( | |
40 | boost::exception & e ) | |
41 | { | |
42 | e.set<tag1>(42); | |
43 | e.set<tag2>("42"); | |
44 | e.set<tag_error_code>(42); //Implicit conversion | |
45 | try | |
46 | { | |
47 | throw; | |
48 | } | |
49 | catch( | |
50 | my_exception & e ) | |
51 | { | |
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); | |
56 | ||
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); | |
64 | } | |
65 | } | |
66 | return boost::report_errors(); | |
67 | } |