]>
Commit | Line | Data |
---|---|---|
1e59de90 | 1 | // Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc. |
20effc67 TL |
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 | ||
1e59de90 TL |
6 | #include <boost/leaf/config.hpp> |
7 | ||
8 | #if !BOOST_LEAF_CFG_CAPTURE | |
9 | ||
10 | #include <iostream> | |
11 | ||
12 | int main() | |
13 | { | |
14 | std::cout << "Unit test not applicable." << std::endl; | |
15 | return 0; | |
16 | } | |
17 | ||
18 | #else | |
19 | ||
20 | #ifdef BOOST_LEAF_TEST_SINGLE_HEADER | |
21 | # include "leaf.hpp" | |
22 | #else | |
23 | # include <boost/leaf/capture.hpp> | |
24 | # include <boost/leaf/result.hpp> | |
25 | # include <boost/leaf/handle_errors.hpp> | |
26 | #endif | |
27 | ||
20effc67 TL |
28 | #include "lightweight_test.hpp" |
29 | ||
30 | namespace leaf = boost::leaf; | |
31 | ||
32 | int count = 0; | |
33 | ||
34 | template <int> | |
35 | struct info | |
36 | { | |
37 | info() noexcept | |
38 | { | |
39 | ++count; | |
40 | } | |
41 | ||
42 | info( info const & ) noexcept | |
43 | { | |
44 | ++count; | |
45 | } | |
46 | ||
47 | ~info() noexcept | |
48 | { | |
49 | --count; | |
50 | } | |
51 | }; | |
52 | ||
53 | int main() | |
54 | { | |
55 | auto error_handlers = std::make_tuple( | |
56 | []( info<1>, info<3> ) | |
57 | { | |
58 | return 42; | |
59 | }, | |
60 | [] | |
61 | { | |
62 | return -42; | |
63 | } ); | |
64 | ||
65 | { | |
66 | auto r = leaf::capture( | |
67 | leaf::make_shared_context(error_handlers), | |
68 | []() -> leaf::result<int> | |
69 | { | |
70 | return leaf::new_error( info<1>{}, info<3>{} ); | |
71 | } ); | |
72 | BOOST_TEST_EQ(count, 2); | |
73 | ||
74 | int answer = leaf::try_handle_all( | |
75 | [&r] | |
76 | { | |
77 | return std::move(r); | |
78 | }, | |
79 | error_handlers); | |
80 | BOOST_TEST_EQ(answer, 42); | |
81 | } | |
82 | BOOST_TEST_EQ(count, 0); | |
83 | ||
84 | return boost::report_errors(); | |
85 | } | |
1e59de90 TL |
86 | |
87 | #endif |