]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/BOOST_LEAF_AUTO_test.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / leaf / test / BOOST_LEAF_AUTO_test.cpp
1 // Copyright 2018-2022 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 #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
13 #include "lightweight_test.hpp"
14 #ifdef BOOST_LEAF_BOOST_AVAILABLE
15 # include <boost/config/workaround.hpp>
16 #else
17 # define BOOST_WORKAROUND(a,b) 0
18 #endif
19
20 namespace leaf = boost::leaf;
21
22 struct value
23 {
24 int x;
25
26 explicit value( int x ): x(x) { };
27
28 #ifndef BOOST_LEAF_NO_CXX11_REF_QUALIFIERS
29 value( value const & ) = delete;
30 value( value && ) = default;
31 #endif
32 };
33
34 leaf::result<value> f1()
35 {
36 return value { 21 };
37 }
38
39 leaf::result<value> f2()
40 {
41 BOOST_LEAF_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 template <class Lambda>
50 leaf::result<value> f2_lambda( Lambda )
51 {
52 BOOST_LEAF_AUTO(a, f1());
53 #if BOOST_WORKAROUND( BOOST_GCC, < 50000 ) || BOOST_WORKAROUND( BOOST_CLANG, <= 30800 )
54 return std::move(a); // Older compilers are confused, but...
55 #else
56 return a; // ...this doesn't need to be return std::move(a);
57 #endif
58 }
59
60 leaf::result<value> f3()
61 {
62 BOOST_LEAF_AUTO(a, f2());
63
64 // Invoking the macro twice in the same scope, testing the temp name
65 // generation. Also making sure we can pass a lambda (See
66 // https://github.com/boostorg/leaf/issues/16).
67 BOOST_LEAF_AUTO(b, f2_lambda([]{}));
68
69 return value { a.x + b.x };
70 }
71
72 int main()
73 {
74 BOOST_TEST_EQ(f3()->x, 42);
75
76 {
77 int r = leaf::try_handle_all(
78 []() -> leaf::result<int>
79 {
80 int x = 42;
81
82 leaf::result<int> r1(x);
83 BOOST_LEAF_AUTO(rx1, r1);
84 BOOST_TEST_EQ(r1.value(), rx1);
85
86 leaf::result<int &> r2(x);
87 BOOST_LEAF_AUTO(rx2, r2);
88 BOOST_TEST_EQ(r2.value(), rx2);
89
90 return 0;
91 },
92 []
93 {
94 return 1;
95 } );
96 BOOST_TEST_EQ(r, 0);
97 }
98
99 return boost::report_errors();
100 }