Add an overload to make_result which accepts a exception_arg_t and exception_t

* Can be used to forward the result correctly from next handlers
This commit is contained in:
Denis Blank 2018-12-25 09:05:51 +01:00
parent d052a02595
commit 4127c02c3f
2 changed files with 43 additions and 1 deletions

View File

@ -277,7 +277,18 @@ decltype(auto) get(result<T...>&& result) {
return detail::result_trait<T...>::template get<I>(std::move(result));
}
/// Creates a present result from the given values
/// Creates a present result from the given values.
///
/// This could be used to pass the result of the next handler to the same
/// asynchronous path it came from as shown below:
/// ```cpp
/// make_ready_continuable().next([&](auto&&... args) {
/// result<> captured = make_result(std::forward<decltype(args)>(args)...);
/// return shutdown().then([captured = std::move(captured)]() mutable {
/// return std::move(captured);
/// });
/// });
/// ```
///
/// \since 4.0.0
template <typename... T,
@ -285,6 +296,16 @@ template <typename... T,
Result make_result(T&&... values) {
return Result::from(std::forward<T>(values)...);
}
/// Creates an exceptional_result from the given exception.
///
/// \copydetails make_result
///
/// \since 4.0.0
inline exceptional_result make_result(exception_arg_t, exception_t exception) {
// NOLINTNEXTLINE(hicpp-move-const-arg, performance-move-const-arg)
return exceptional_result{std::move(exception)};
}
/// \}
} // namespace cti

View File

@ -23,6 +23,7 @@
#include <memory>
#include <utility>
#include <continuable/continuable-primitives.hpp>
#include <continuable/continuable-result.hpp>
#include <continuable/detail/core/types.hpp>
#include <test-continuable.hpp>
@ -253,6 +254,26 @@ TYPED_TEST(result_all_tests, is_assignable_from_empty_helper) {
EXPECT_TRUE(e.is_empty());
}
TYPED_TEST(result_all_tests, can_make_from_multipath_args) {
{
TypeParam e = make_result(this->supply(CANARY));
EXPECT_TRUE(bool(e));
EXPECT_EQ(this->get(*e), CANARY);
EXPECT_TRUE(e.is_value());
EXPECT_FALSE(e.is_exception());
}
{
TypeParam e = make_result(cti::exception_arg_t{}, //
supply_test_exception());
EXPECT_FALSE(bool(e));
EXPECT_FALSE(e.is_value());
EXPECT_TRUE(e.is_empty());
}
}
// This regression test shows a memory leak which happens when using the
// result class move constructed from another result object.
TEST(result_single_test, test_leak_regression) {