]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // Copyright (c) 2018-2020 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 | #include <boost/leaf/capture.hpp> | |
7 | #include <boost/leaf/result.hpp> | |
8 | #include <boost/leaf/handle_errors.hpp> | |
9 | #include "_test_ec.hpp" | |
10 | #include "lightweight_test.hpp" | |
11 | ||
12 | namespace leaf = boost::leaf; | |
13 | ||
14 | template <int> struct info { int value; }; | |
15 | ||
16 | template <class F> | |
17 | void test( F f ) | |
18 | { | |
19 | { | |
20 | int c=0; | |
21 | auto r = f(); | |
22 | leaf::try_handle_all( | |
23 | [&r]() -> leaf::result<void> | |
24 | { | |
25 | BOOST_LEAF_CHECK(std::move(r)); | |
26 | return { }; | |
27 | }, | |
28 | [&c]( info<1> const & x ) | |
29 | { | |
30 | BOOST_TEST_EQ(x.value, 1); | |
31 | BOOST_TEST_EQ(c, 0); | |
32 | c = 1; | |
33 | }, | |
34 | [&c] | |
35 | { | |
36 | BOOST_TEST_EQ(c, 0); | |
37 | c = 2; | |
38 | } ); | |
39 | BOOST_TEST_EQ(c, 1); | |
40 | } | |
41 | ||
42 | { | |
43 | int c=0; | |
44 | auto r = f(); | |
45 | leaf::try_handle_all( | |
46 | [&r]() -> leaf::result<void> | |
47 | { | |
48 | BOOST_LEAF_CHECK(std::move(r)); | |
49 | return { }; | |
50 | }, | |
51 | [&c]( info<2> const & x ) | |
52 | { | |
53 | BOOST_TEST_EQ(x.value, 2); | |
54 | BOOST_TEST_EQ(c, 0); | |
55 | c = 1; | |
56 | }, | |
57 | [&c] | |
58 | { | |
59 | BOOST_TEST_EQ(c, 0); | |
60 | c = 2; | |
61 | } ); | |
62 | BOOST_TEST_EQ(c, 2); | |
63 | } | |
64 | ||
65 | { | |
66 | auto r = f(); | |
67 | int what = leaf::try_handle_all( | |
68 | [&r]() -> leaf::result<int> | |
69 | { | |
70 | BOOST_LEAF_CHECK(std::move(r)); | |
71 | return 0; | |
72 | }, | |
73 | []( info<1> const & x ) | |
74 | { | |
75 | BOOST_TEST_EQ(x.value, 1); | |
76 | return 1; | |
77 | }, | |
78 | [] | |
79 | { | |
80 | return 2; | |
81 | } ); | |
82 | BOOST_TEST_EQ(what, 1); | |
83 | } | |
84 | ||
85 | { | |
86 | auto r = f(); | |
87 | int what = leaf::try_handle_all( | |
88 | [&r]() -> leaf::result<int> | |
89 | { | |
90 | BOOST_LEAF_CHECK(std::move(r)); | |
91 | return 0; | |
92 | }, | |
93 | []( info<2> const & x ) | |
94 | { | |
95 | BOOST_TEST_EQ(x.value, 2); | |
96 | return 1; | |
97 | }, | |
98 | [] | |
99 | { | |
100 | return 2; | |
101 | } ); | |
102 | BOOST_TEST_EQ(what, 2); | |
103 | } | |
104 | } | |
105 | ||
106 | int main() | |
107 | { | |
108 | test( [] | |
109 | { | |
110 | return leaf::capture( | |
111 | std::make_shared<leaf::leaf_detail::polymorphic_context_impl<leaf::context<std::error_code, info<1>, info<2>, info<3>>>>(), | |
112 | []() -> leaf::result<int> | |
113 | { | |
114 | return leaf::new_error(errc_a::a0, info<1>{1}, info<3>{3}); | |
115 | } ); | |
116 | } ); | |
117 | ||
118 | test( [] | |
119 | { | |
120 | return leaf::capture( | |
121 | std::make_shared<leaf::leaf_detail::polymorphic_context_impl<leaf::context<std::error_code, info<1>, info<2>, info<3>>>>(), | |
122 | []() -> leaf::result<void> | |
123 | { | |
124 | return leaf::new_error(errc_a::a0, info<1>{1}, info<3>{3}); | |
125 | } ); | |
126 | } ); | |
127 | ||
128 | return boost::report_errors(); | |
129 | } |