]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/defer_basic_test.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / leaf / test / defer_basic_test.cpp
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/on_error.hpp>
7 #include <boost/leaf/handle_errors.hpp>
8 #include <boost/leaf/result.hpp>
9 #include "lightweight_test.hpp"
10 #include <sstream>
11
12 namespace leaf = boost::leaf;
13
14 int global;
15
16 int get_global() noexcept
17 {
18 return global;
19 }
20
21 template <int>
22 struct info
23 {
24 int value;
25 };
26
27 leaf::error_id g()
28 {
29 global = 0;
30 auto load = leaf::on_error( []{ return info<42>{get_global()}; }, []{ return info<-42>{-42}; } );
31 global = 42;
32 return leaf::new_error();
33 }
34
35 leaf::error_id f()
36 {
37 return g();
38 }
39
40 int main()
41 {
42 int r = leaf::try_handle_all(
43 []() -> leaf::result<int>
44 {
45 return f();
46 },
47 []( info<42> const & i42, leaf::diagnostic_info const & di )
48 {
49 BOOST_TEST_EQ(i42.value, 42);
50 std::stringstream ss; ss << di;
51 std::string s = ss.str();
52 std::cout << s;
53 #if BOOST_LEAF_DIAGNOSTICS
54 BOOST_TEST(s.find("info<-42>")!=s.npos);
55 #else
56 BOOST_TEST(s.find("BOOST_LEAF_DIAGNOSTICS")!=s.npos);
57 #endif
58 return 1;
59 },
60 []
61 {
62 return 2;
63 } );
64 BOOST_TEST_EQ(r, 1);
65 return boost::report_errors();
66 }