1 | ////////////////////////////////////////////////////////////////////////////// |
---|---|
2 | // |
3 | // (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost |
4 | // Software License, Version 1.0. (See accompanying file |
5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | // |
7 | // See http://www.boost.org/libs/container for documentation. |
8 | // |
9 | ////////////////////////////////////////////////////////////////////////////// |
10 | #define BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS |
11 | |
12 | #include <boost/container/throw_exception.hpp> |
13 | #include <boost/core/lightweight_test.hpp> |
14 | |
15 | using namespace boost::container; |
16 | |
17 | static bool bad_alloc_called = false; |
18 | static bool out_of_range_called = false; |
19 | static bool length_error_called = false; |
20 | static bool logic_error_called = false; |
21 | static bool runtime_error_called = false; |
22 | |
23 | BOOST_NORETURN static void validate_and_never_return() |
24 | { |
25 | BOOST_TEST(bad_alloc_called == true); |
26 | BOOST_TEST(out_of_range_called == true); |
27 | BOOST_TEST(length_error_called == true); |
28 | BOOST_TEST(logic_error_called == true); |
29 | BOOST_TEST(runtime_error_called == true); |
30 | std::exit(status: ::boost::report_errors()); |
31 | } |
32 | |
33 | //User defined throw implementations |
34 | namespace boost { |
35 | namespace container { |
36 | |
37 | BOOST_NORETURN void throw_bad_alloc() |
38 | { |
39 | bad_alloc_called = true; |
40 | throw_out_of_range(str: "dummy"); |
41 | } |
42 | |
43 | BOOST_NORETURN void throw_out_of_range(const char* str) |
44 | { |
45 | out_of_range_called = true; |
46 | throw_length_error(str); |
47 | } |
48 | |
49 | BOOST_NORETURN void throw_length_error(const char* str) |
50 | { |
51 | length_error_called = true; |
52 | throw_logic_error(str); |
53 | } |
54 | |
55 | BOOST_NORETURN void throw_logic_error(const char* str) |
56 | { |
57 | logic_error_called = true; |
58 | throw_runtime_error(str); |
59 | } |
60 | |
61 | BOOST_NORETURN void throw_runtime_error(const char* str) |
62 | { |
63 | (void)str; |
64 | runtime_error_called = true; |
65 | validate_and_never_return(); |
66 | } |
67 | |
68 | }} //boost::container |
69 | |
70 | int main() |
71 | { |
72 | //Check user-defined throw callbacks are called |
73 | throw_bad_alloc(); |
74 | //Never reached |
75 | return 33; |
76 | } |
77 |