Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
rename recover by catch_error.
Browse files Browse the repository at this point in the history
  • Loading branch information
viboes committed Apr 21, 2014
1 parent 750f699 commit 4b62de9
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion example/get_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ template <class Num, class CharT=char, class InputIterator = std::istreambuf_ite
boost::expected<std::ios_base::iostate, std::pair<Num,Num>> get_interval4(ios_range<CharT, InputIterator>& r)
{
return get_num<Num>(r)
.recover([](std::ios_base::iostate st) {
.catch_error([](std::ios_base::iostate st) {
std::cout << __FILE__ << "[" << __LINE__ << "] " << st << std::endl;
return boost::make_unexpected(st);
}
Expand Down
8 changes: 4 additions & 4 deletions example/safe_divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ namespace exception_based

boost::expected<std::exception_ptr, int> divide(int i, int j)
{
return safe_divide(i,j).
recover([](std::exception_ptr ex) -> boost::expected<std::exception_ptr, int>
return safe_divide(i,j).
catch_error([](std::exception_ptr ex) -> boost::expected<std::exception_ptr, int>
{
try
{
Expand Down Expand Up @@ -196,7 +196,7 @@ boost::expected<std::exception_ptr, int> divide1(int i, int j)
boost::expected<std::exception_ptr, int> divide2(int i, int j)
{
return safe_divide(i,j).
catch_exception<NotDivisible>([](NotDivisible& e) -> expected<std::exception_ptr, int>
catch_exception<NotDivisible>([](NotDivisible& e) -> expected<std::exception_ptr, int>
{
return e.i / e.j;
});
Expand All @@ -205,7 +205,7 @@ boost::expected<std::exception_ptr, int> divide2(int i, int j)
boost::expected<std::exception_ptr, int> divide3(int i, int j)
{
return safe_divide(i,j).
catch_exception<NotDivisible>([](NotDivisible& e)
catch_exception<NotDivisible>([](NotDivisible& e)
{
return make_expected(e.i / e.j);
});
Expand Down
2 changes: 1 addition & 1 deletion example/safe_divide_monad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ namespace expected_based
expected<std::exception_ptr, int> divide0(int i, int j)
{
using namespace boost::functional::monad_error;
return safe_divide(i,j).recover(
return safe_divide(i,j).catch_error(
[](std::exception_ptr ex) -> expected<std::exception_ptr, int>
{
try
Expand Down
4 changes: 2 additions & 2 deletions include/boost/expected/algorithms/if_then_else.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ namespace expected_alg

template <class T, class E, class True, class False>
auto if_then_else(expected<E,T> e, True&& t, False&& f)
-> decltype(unwrap(e.fmap(std::forward<True>(t)).recover(std::forward<False>(f))))
-> decltype(unwrap(e.fmap(std::forward<True>(t)).catch_error(std::forward<False>(f))))
{
return unwrap(e.fmap(std::forward<True>(t)).recover(std::forward<False>(f)));
return unwrap(e.fmap(std::forward<True>(t)).catch_error(std::forward<False>(f)));
}

} // namespace expected_alg
Expand Down
8 changes: 4 additions & 4 deletions include/boost/expected/algorithms/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ namespace expected_alg
template <class T, class E>
BOOST_CONSTEXPR T value(expected<E,T> const& e)
{
// We are sure that e.recover(thrower<T>()) will be valid or a exception will be thrown
// We are sure that e.catch_error(thrower<T>()) will be valid or a exception will be thrown
// so the derefference is safe
return * e.recover(thrower<T>());
return * e.catch_error(thrower<T>());
}

template <class T, class E>
BOOST_CONSTEXPR T value(expected<E,T> && e)
{
// We are sure that e.recover(thrower<T>()) will be valid or a exception will be thrown
// We are sure that e.catch_error(thrower<T>()) will be valid or a exception will be thrown
// so the derefference is safe
return * e.recover(thrower<T>());
return * e.catch_error(thrower<T>());
}

} // namespace expected_alg
Expand Down
8 changes: 4 additions & 4 deletions include/boost/expected/algorithms/value_or.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ namespace expected_alg
template <class T, class E>
BOOST_CONSTEXPR T value_or(expected<E,T> const& e, BOOST_FWD_REF(T) v)
{
// We are sure that e.recover(just(std::forward<T>(v))) will be valid or a exception will be thrown
// We are sure that e.catch_error(just(std::forward<T>(v))) will be valid or a exception will be thrown
// so the dereference is safe
return * e.recover(just(std::forward<T>(v)));
return * e.catch_error(just(std::forward<T>(v)));
}

template <class T, class E>
BOOST_CONSTEXPR T value_or(expected<E,T> && e, BOOST_FWD_REF(T) v)
{
// We are sure that e.recover(just(std::forward<T>(v))) will be valid or a exception will be thrown
// We are sure that e.catch_error(just(std::forward<T>(v))) will be valid or a exception will be thrown
// so the dereference is safe
return * e.recover(just(std::forward<T>(v)));
return * e.catch_error(just(std::forward<T>(v)));
}

} // namespace expected_alg
Expand Down
8 changes: 4 additions & 4 deletions include/boost/expected/algorithms/value_or_call.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ namespace expected_alg
template <class T, class E, class F>
BOOST_CONSTEXPR T value_or_call(expected<E,T> const& e, BOOST_FWD_REF(F) f)
{
// We are sure that e.recover(just(std::forward<T>(v))) will be valid or a exception will be thrown
// We are sure that e.catch_error(just(std::forward<T>(v))) will be valid or a exception will be thrown
// so the derefference is safe
return * e.recover(defer(std::forward<F>(f)));
return * e.catch_error(defer(std::forward<F>(f)));
}

template <class T, class E, class F>
BOOST_CONSTEXPR T value_or_call(expected<E,T> && e, BOOST_FWD_REF(F) f)
{
// We are sure that e.recover(just(std::forward<T>(v))) will be valid or a exception will be thrown
// We are sure that e.catch_error(just(std::forward<T>(v))) will be valid or a exception will be thrown
// so the derefference is safe
return * e.recover(defer(std::forward<F>(f)));
return * e.catch_error(defer(std::forward<F>(f)));
}

} // namespace expected_alg
Expand Down
12 changes: 6 additions & 6 deletions include/boost/expected/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ class expected

template <typename F>
BOOST_CONSTEXPR this_type
recover(BOOST_RV_REF(F) f,
catch_error(BOOST_RV_REF(F) f,
REQUIRES(boost::is_same<typename result_of<F(error_type)>::type, value_type>::value)) const
{
#if ! defined BOOST_NO_CXX14_RELAXED_CONSTEXPR
Expand All @@ -1204,7 +1204,7 @@ class expected
}

template <typename F>
BOOST_CONSTEXPR this_type recover(BOOST_RV_REF(F) f,
BOOST_CONSTEXPR this_type catch_error(BOOST_RV_REF(F) f,
REQUIRES(boost::is_same<typename result_of<F(error_type)>::type, this_type>::value)) const
{
#if ! defined BOOST_NO_CXX14_RELAXED_CONSTEXPR
Expand All @@ -1222,7 +1222,7 @@ class expected
}

template <typename F>
BOOST_CONSTEXPR this_type recover(BOOST_RV_REF(F) f,
BOOST_CONSTEXPR this_type catch_error(BOOST_RV_REF(F) f,
REQUIRES(boost::is_same<typename result_of<F(error_type)>::type, unexpected_type<error_type> >::value)) const
{
#if ! defined BOOST_NO_CXX14_RELAXED_CONSTEXPR
Expand Down Expand Up @@ -1655,10 +1655,10 @@ class expected<ErrorType,void>
return f(boost::move(*this));
}

// recover factory
// catch_error factory

template <typename F>
BOOST_CONSTEXPR this_type recover(BOOST_RV_REF(F) f,
BOOST_CONSTEXPR this_type catch_error(BOOST_RV_REF(F) f,
REQUIRES(boost::is_same<typename result_of<F(error_type)>::type, value_type>::value)) const
{
#if ! defined BOOST_NO_CXX14_RELAXED_CONSTEXPR
Expand All @@ -1676,7 +1676,7 @@ class expected<ErrorType,void>
}

template <typename F>
BOOST_CONSTEXPR this_type recover(BOOST_RV_REF(F) f,
BOOST_CONSTEXPR this_type catch_error(BOOST_RV_REF(F) f,
REQUIRES(! boost::is_same<typename result_of<F(error_type)>::type, value_type>::value)) const
{
#if ! defined BOOST_NO_CXX14_RELAXED_CONSTEXPR
Expand Down
2 changes: 1 addition & 1 deletion include/boost/expected/expected_monad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace functional
template <class M, class F>
static M catch_error(M&& m, F&& f)
{
return m.recover(std::forward<F>(f));
return m.catch_error(std::forward<F>(f));
}
};
template <class T1, class E1>
Expand Down
2 changes: 1 addition & 1 deletion test/algorithms/test_if_then_else.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BOOST_AUTO_TEST_SUITE(Copyable)
{
expected<int,int> e0{unexpect, 1};
expected<int, expected<int, std::string>> e1 = e0.fmap(to_expected_string_ok);
expected<int, expected<int, std::string>> e2 = e1.recover(to_expected_error_ko);
expected<int, expected<int, std::string>> e2 = e1.catch_error(to_expected_error_ko);
//expected<int, std::string> e3 = unwrap(e2);
expected<int, std::string> e3 = e2.unwrap();
BOOST_CHECK (!e3 && (e3.error() == 2));
Expand Down
2 changes: 1 addition & 1 deletion test/algorithms/test_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ BOOST_AUTO_TEST_SUITE(Value)
BOOST_AUTO_TEST_CASE(Value_Valued_RValue)
{
expected<std::exception_ptr, int> ei = 1;
auto ej = ei.recover(thrower<int>());
auto ej = ei.catch_error(thrower<int>());
BOOST_CHECK (*ej == 1);
int i = value(std::move(ei));
BOOST_CHECK_EQUAL (i, 1);
Expand Down
42 changes: 21 additions & 21 deletions test/test_expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,24 +848,24 @@ BOOST_AUTO_TEST_CASE(expected_recover)
throw test_exception();
};

BOOST_CHECK_EQUAL(fun(false).recover(recover_error).valid(), true);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error).value(), 0);
BOOST_CHECK_EQUAL(fun(true).recover(recover_error).value(), 5);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error_silent_failure).valid(), false);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error_failure).valid(), false);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).valid(), true);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).value(), 0);
BOOST_CHECK_EQUAL(fun(true).catch_error(recover_error).value(), 5);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error_silent_failure).valid(), false);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error_failure).valid(), false);

BOOST_CHECK_EQUAL(fun(true).mbind(add_five).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).recover(recover_error).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).recover(recover_error_silent_failure).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).recover(recover_error_failure).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).catch_error(recover_error).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).catch_error(recover_error_silent_failure).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).catch_error(recover_error_failure).value(), 10);

BOOST_CHECK_EQUAL(fun(false).recover(recover_error).mbind(add_five).value(), 5);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error).mbind(add_five).mbind(add_five).value(), 10);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error_failure).mbind(add_five).valid(), false);
BOOST_CHECK_EQUAL(fun(false).mbind(add_five).recover(recover_error_failure).mbind(add_five).valid(), false);
BOOST_CHECK_EQUAL(fun(false).mbind(add_five).recover(recover_error_silent_failure).mbind(add_five).valid(), false);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).mbind(add_five).value(), 5);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).mbind(add_five).mbind(add_five).value(), 10);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error_failure).mbind(add_five).valid(), false);
BOOST_CHECK_EQUAL(fun(false).mbind(add_five).catch_error(recover_error_failure).mbind(add_five).valid(), false);
BOOST_CHECK_EQUAL(fun(false).mbind(add_five).catch_error(recover_error_silent_failure).mbind(add_five).valid(), false);

BOOST_CHECK_THROW(fun(false).recover(recover_error_throws), test_exception);
BOOST_CHECK_THROW(fun(false).catch_error(recover_error_throws), test_exception);

}

Expand Down Expand Up @@ -896,16 +896,16 @@ BOOST_AUTO_TEST_CASE(expected_void_recover)
throw test_exception();
};

// The recover doesn't alter the expected if it's valid.
BOOST_CHECK_EQUAL(fun(true).recover(recover_error_failure).valid(), true);
// The catch_error doesn't alter the expected if it's valid.
BOOST_CHECK_EQUAL(fun(true).catch_error(recover_error_failure).valid(), true);

// Simple recover tests.
BOOST_CHECK_EQUAL(fun(false).recover(recover_error).valid(), true);
BOOST_CHECK_THROW(fun(false).recover(recover_error_failure), test_exception);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error_silent_failure).valid(), false);
// Simple catch_error tests.
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).valid(), true);
BOOST_CHECK_THROW(fun(false).catch_error(recover_error_failure), test_exception);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error_silent_failure).valid(), false);

// With a mbind between.
BOOST_CHECK_THROW(fun(false).mbind(do_nothing).recover(recover_error_failure), test_exception);
BOOST_CHECK_THROW(fun(false).mbind(do_nothing).catch_error(recover_error_failure), test_exception);

}
BOOST_AUTO_TEST_SUITE_END()
Expand Down
52 changes: 26 additions & 26 deletions test/test_expected2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,24 +591,24 @@ BOOST_AUTO_TEST_CASE(expected_recover)
throw test_exception();
};

BOOST_CHECK_EQUAL(fun(false).recover(recover_error).value(), 0);
BOOST_CHECK_EQUAL(fun(true).recover(recover_error).value(), 5);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error_silent_failure).valid(), false);
BOOST_CHECK_THROW(fun(false).recover(recover_error_failure).valid(), test_exception);

BOOST_CHECK_EQUAL(fun(true).mbind(add_five).recover(recover_error).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).recover(recover_error_silent_failure).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).recover(recover_error_failure).value(), 10);

BOOST_CHECK_EQUAL(fun(false).recover(recover_error).mbind(add_five).value(), 5);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error).mbind(add_five).mbind(add_five).value(), 10);
BOOST_CHECK_THROW(fun(false).recover(recover_error_failure).mbind(add_five).valid(), test_exception);
BOOST_CHECK_THROW(fun(false).mbind(add_five).recover(recover_error_failure).mbind(add_five).valid(), test_exception);
BOOST_CHECK_EQUAL(fun(false).mbind(add_five).recover(recover_error_silent_failure).mbind(add_five).valid(), false);

BOOST_CHECK_THROW(fun(true).mbind(then_launch).recover(recover_error).value(), test_exception);
BOOST_CHECK_THROW(fun(true).mbind(then_launch).recover(recover_error).mbind(add_five).value(), test_exception);
BOOST_CHECK_THROW(fun(true).mbind(then_launch).recover(recover_error_failure).valid(), test_exception);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).value(), 0);
BOOST_CHECK_EQUAL(fun(true).catch_error(recover_error).value(), 5);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error_silent_failure).valid(), false);
BOOST_CHECK_THROW(fun(false).catch_error(recover_error_failure).valid(), test_exception);

BOOST_CHECK_EQUAL(fun(true).mbind(add_five).catch_error(recover_error).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).catch_error(recover_error_silent_failure).value(), 10);
BOOST_CHECK_EQUAL(fun(true).mbind(add_five).catch_error(recover_error_failure).value(), 10);

BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).mbind(add_five).value(), 5);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).mbind(add_five).mbind(add_five).value(), 10);
BOOST_CHECK_THROW(fun(false).catch_error(recover_error_failure).mbind(add_five).valid(), test_exception);
BOOST_CHECK_THROW(fun(false).mbind(add_five).catch_error(recover_error_failure).mbind(add_five).valid(), test_exception);
BOOST_CHECK_EQUAL(fun(false).mbind(add_five).catch_error(recover_error_silent_failure).mbind(add_five).valid(), false);

BOOST_CHECK_THROW(fun(true).mbind(then_launch).catch_error(recover_error).value(), test_exception);
BOOST_CHECK_THROW(fun(true).mbind(then_launch).catch_error(recover_error).mbind(add_five).value(), test_exception);
BOOST_CHECK_THROW(fun(true).mbind(then_launch).catch_error(recover_error_failure).valid(), test_exception);
}

BOOST_AUTO_TEST_CASE(expected_void_recover)
Expand Down Expand Up @@ -643,18 +643,18 @@ BOOST_AUTO_TEST_CASE(expected_void_recover)
throw test_exception();
};

// The recover doesn't alter the expected if it's valid.
BOOST_CHECK_EQUAL(fun(true).recover(recover_error_failure).valid(), true);
// The catch_error doesn't alter the expected if it's valid.
BOOST_CHECK_EQUAL(fun(true).catch_error(recover_error_failure).valid(), true);

// Simple recover tests.
BOOST_CHECK_EQUAL(fun(false).recover(recover_error).valid(), true);
BOOST_CHECK_THROW(fun(false).recover(recover_error_failure).valid(), test_exception);
BOOST_CHECK_EQUAL(fun(false).recover(recover_error_silent_failure).valid(), false);
// Simple catch_error tests.
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error).valid(), true);
BOOST_CHECK_THROW(fun(false).catch_error(recover_error_failure).valid(), test_exception);
BOOST_CHECK_EQUAL(fun(false).catch_error(recover_error_silent_failure).valid(), false);

// With a mbind between.
BOOST_CHECK_THROW(fun(false).mbind(do_nothing).recover(recover_error_failure).valid(), test_exception);
BOOST_CHECK_THROW(fun(false).mbind(do_nothing).catch_error(recover_error_failure).valid(), test_exception);

BOOST_CHECK_NO_THROW(fun(false).mbind(then_launch).recover(recover_error).value());
BOOST_CHECK_NO_THROW(fun(false).mbind(then_launch).catch_error(recover_error).value());
}
#endif

Expand Down

0 comments on commit 4b62de9

Please sign in to comment.