-// Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
+// Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-#include <boost/leaf/handle_errors.hpp>
-#include <boost/leaf/pred.hpp>
-#include <boost/leaf/result.hpp>
+#ifdef BOOST_LEAF_TEST_SINGLE_HEADER
+# include "leaf.hpp"
+#else
+# include <boost/leaf/handle_errors.hpp>
+# include <boost/leaf/pred.hpp>
+# include <boost/leaf/result.hpp>
+#endif
+
#include "_test_ec.hpp"
#include "lightweight_test.hpp"
struct e_my_error_code { my_error_code value; };
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
struct e_std_error_code { std::error_code value; };
+#endif
template <class R>
leaf::result<R> f( my_error_code ec )
return leaf::new_error(ec, e_my_error_code{ec}, info<1>{1}, info<2>{2}, info<3>{3});
}
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
template <class R, class Errc>
leaf::result<R> f_errc( Errc ec )
{
{
return leaf::new_error(e_std_error_code{make_error_code(ec)}, info<1>{1}, info<2>{2}, info<3>{3});
}
+#endif
+
+struct move_only
+{
+ explicit move_only( int value ): value(value) { }
+ int value;
+
+#ifndef BOOST_LEAF_NO_CXX11_REF_QUALIFIERS
+ move_only( move_only const & ) = delete;
+ move_only( move_only && ) = default;
+#endif
+};
int main()
{
BOOST_TEST_EQ(c, 1);
}
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
// void, try_handle_all (failure), match cond_x (single enum value)
{
int c=0;
} );
BOOST_TEST_EQ(c, 2);
}
+#endif
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
// void, try_handle_all (failure), match cond_x (wrapped std::error_code)
{
int c=0;
} );
BOOST_TEST_EQ(c, 2);
}
+#endif
// void, try_handle_all (failure), match enum (single enum value)
{
BOOST_TEST_EQ(r, 1);
}
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
// int, try_handle_all (failure), match cond_x (single enum value)
{
int r = leaf::try_handle_all(
} );
BOOST_TEST_EQ(r, 2);
}
+#endif
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
// int, try_handle_all (failure), match cond_x (wrapped std::error_code)
{
int r = leaf::try_handle_all(
} );
BOOST_TEST_EQ(r, 2);
}
+#endif
// int, try_handle_all (failure), match enum (single enum value)
{
BOOST_TEST_EQ(r, 2);
}
+ //////////////////////////////////////
+
+ // move_only, try_handle_all (success)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f<move_only>(my_error_code::ok));
+ return std::move(answer);
+ },
+ []
+ {
+ return move_only(1);
+ } );
+ BOOST_TEST_EQ(r.value, 42);
+ }
+
+ // move_only, try_handle_all (failure)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f<move_only>(my_error_code::error1));
+ return std::move(answer);
+ },
+ []( my_error_code ec, info<1> const & x, info<2> y )
+ {
+ BOOST_TEST(ec==my_error_code::error1);
+ BOOST_TEST_EQ(x.value, 1);
+ BOOST_TEST_EQ(y.value, 2);
+ return move_only(1);
+ },
+ []
+ {
+ return move_only(2);
+ } );
+ BOOST_TEST_EQ(r.value, 1);
+ }
+
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
+ // move_only, try_handle_all (failure), match cond_x (single enum value)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f_errc<move_only>(errc_a::a0));
+ return std::move(answer);
+ },
+ []( leaf::match<leaf::condition<cond_x>, cond_x::x11> )
+ {
+ return move_only(1);
+ },
+ []( leaf::match<leaf::condition<cond_x>, cond_x::x00> ec, info<1> const & x, info<2> y )
+ {
+ BOOST_TEST_EQ(ec.matched, make_error_code(errc_a::a0));
+ BOOST_TEST_EQ(x.value, 1);
+ BOOST_TEST_EQ(y.value, 2);
+ return move_only(2);
+ },
+ []
+ {
+ return move_only(3);
+ } );
+ BOOST_TEST_EQ(r.value, 2);
+ }
+#endif
+
+#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
+ // move_only, try_handle_all (failure), match cond_x (wrapped std::error_code)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f_errc_wrapped<move_only>(errc_a::a0));
+ return std::move(answer);
+ },
+ []( leaf::match<leaf::condition<cond_x>, cond_x::x11> )
+ {
+ return move_only(1);
+ },
+ []( leaf::match_value<leaf::condition<e_std_error_code, cond_x>, cond_x::x00> ec, info<1> const & x, info<2> y )
+ {
+ BOOST_TEST_EQ(ec.matched.value, make_error_code(errc_a::a0));
+ BOOST_TEST_EQ(x.value, 1);
+ BOOST_TEST_EQ(y.value, 2);
+ return move_only(2);
+ },
+ []
+ {
+ return move_only(3);
+ } );
+ BOOST_TEST_EQ(r.value, 2);
+ }
+#endif
+
+ // move_only, try_handle_all (failure), match enum (single enum value)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f<move_only>(my_error_code::error1));
+ return std::move(answer);
+ },
+ []( leaf::match<my_error_code, my_error_code::error2> )
+ {
+ return move_only(1);
+ },
+ []( leaf::match<my_error_code, my_error_code::error1> ec, info<1> const & x, info<2> y )
+ {
+ BOOST_TEST(ec.matched==my_error_code::error1);
+ BOOST_TEST_EQ(x.value, 1);
+ BOOST_TEST_EQ(y.value, 2);
+ return move_only(2);
+ },
+ []
+ {
+ return move_only(3);
+ } );
+ BOOST_TEST_EQ(r.value, 2);
+ }
+
+ // move_only, try_handle_all (failure), match enum (multiple enum values)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f<move_only>(my_error_code::error1));
+ return std::move(answer);
+ },
+ []( leaf::match<my_error_code, my_error_code::error2> )
+ {
+ return move_only(1);
+ },
+ []( leaf::match<my_error_code, my_error_code::error2, my_error_code::error1> ec, info<1> const & x, info<2> y )
+ {
+ BOOST_TEST(ec.matched==my_error_code::error1);
+ BOOST_TEST_EQ(x.value, 1);
+ BOOST_TEST_EQ(y.value, 2);
+ return move_only(2);
+ },
+ []
+ {
+ return move_only(3);
+ } );
+ BOOST_TEST_EQ(r.value, 2);
+ }
+
+ // move_only, try_handle_all (failure), match value (single value)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f<move_only>(my_error_code::error1));
+ return std::move(answer);
+ },
+ []( leaf::match_value<e_my_error_code, my_error_code::error2> )
+ {
+ return move_only(1);
+ },
+ []( leaf::match_value<e_my_error_code, my_error_code::error1> ec, info<1> const & x, info<2> y )
+ {
+ BOOST_TEST(ec.matched.value==my_error_code::error1);
+ BOOST_TEST_EQ(x.value, 1);
+ BOOST_TEST_EQ(y.value, 2);
+ return move_only(2);
+ },
+ []
+ {
+ return move_only(3);
+ } );
+ BOOST_TEST_EQ(r.value, 2);
+ }
+
+ // move_only, try_handle_all (failure), match value (multiple values)
+ {
+ move_only r = leaf::try_handle_all(
+ []() -> leaf::result<move_only>
+ {
+ BOOST_LEAF_AUTO(answer, f<move_only>(my_error_code::error1));
+ return std::move(answer);
+ },
+ []( leaf::match_value<e_my_error_code, my_error_code::error2> )
+ {
+ return move_only(1);
+ },
+ []( leaf::match_value<e_my_error_code, my_error_code::error2, my_error_code::error1> ec, info<1> const & x, info<2> y )
+ {
+ BOOST_TEST(ec.matched.value==my_error_code::error1);
+ BOOST_TEST_EQ(x.value, 1);
+ BOOST_TEST_EQ(y.value, 2);
+ return move_only(2);
+ },
+ []
+ {
+ return move_only(3);
+ } );
+ BOOST_TEST_EQ(r.value, 2);
+ }
+
return boost::report_errors();
}