]>
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 | #ifdef BOOST_LEAF_TEST_SINGLE_HEADER |
7 | # include "leaf.hpp" | |
8 | #else | |
9 | # include <boost/leaf/result.hpp> | |
10 | # include <boost/leaf/handle_errors.hpp> | |
11 | #endif | |
12 | ||
20effc67 | 13 | #include "lightweight_test.hpp" |
1e59de90 TL |
14 | #ifdef BOOST_LEAF_BOOST_AVAILABLE |
15 | # include <boost/config/workaround.hpp> | |
16 | #else | |
17 | # define BOOST_WORKAROUND(a,b) 0 | |
18 | #endif | |
20effc67 TL |
19 | |
20 | namespace leaf = boost::leaf; | |
21 | ||
22 | struct value | |
23 | { | |
24 | int x; | |
25 | ||
26 | explicit value( int x ): x(x) { }; | |
1e59de90 TL |
27 | |
28 | #ifndef BOOST_LEAF_NO_CXX11_REF_QUALIFIERS | |
20effc67 TL |
29 | value( value const & ) = delete; |
30 | value( value && ) = default; | |
1e59de90 | 31 | #endif |
20effc67 TL |
32 | }; |
33 | ||
34 | leaf::result<value> f1() | |
35 | { | |
36 | return value { 21 }; | |
37 | } | |
38 | ||
39 | leaf::result<value> f2() | |
40 | { | |
41 | BOOST_LEAF_ASSIGN(auto a, f1()); | |
42 | #if BOOST_WORKAROUND( BOOST_GCC, < 50000 ) || BOOST_WORKAROUND( BOOST_CLANG, <= 30800 ) | |
43 | return std::move(a); // Older compilers are confused, but... | |
44 | #else | |
45 | return a; // ...this doesn't need to be return std::move(a); | |
46 | #endif | |
47 | } | |
48 | ||
49 | leaf::result<value> f3() | |
50 | { | |
51 | BOOST_LEAF_ASSIGN(auto a, f2()); | |
52 | BOOST_LEAF_ASSIGN(auto b, f2()); // Invoking the macro twice in the same scope, testing the temp name generation | |
53 | return value { a.x + b.x }; | |
54 | } | |
55 | ||
56 | int main() | |
57 | { | |
58 | BOOST_TEST_EQ(f3()->x, 42); | |
59 | ||
60 | { | |
61 | int r = leaf::try_handle_all( | |
62 | []() -> leaf::result<int> | |
63 | { | |
64 | int x = 42; | |
65 | ||
66 | leaf::result<int> r1(x); | |
67 | BOOST_LEAF_ASSIGN(auto && rx1, r1); | |
68 | BOOST_TEST_EQ(r1.value(), rx1); | |
69 | ||
70 | leaf::result<int &> r2(x); | |
71 | BOOST_LEAF_ASSIGN(auto && rx2, r2); | |
72 | BOOST_TEST_EQ(r2.value(), rx2); | |
73 | ||
74 | leaf::result<int &> r3(x); | |
75 | BOOST_LEAF_ASSIGN(auto & rx3, r3); | |
76 | BOOST_TEST_EQ(&r3.value(), &rx3); | |
77 | ||
78 | return 0; | |
79 | }, | |
80 | [] | |
81 | { | |
82 | return 1; | |
83 | } ); | |
84 | BOOST_TEST_EQ(r, 0); | |
85 | } | |
86 | ||
87 | return boost::report_errors(); | |
88 | } |