Commit 6243793a authored by Matthias Vallentin's avatar Matthias Vallentin Committed by Marian Triebe

Enable construction of result<T> from expected<T>

parent 03b33445
...@@ -24,9 +24,12 @@ ...@@ -24,9 +24,12 @@
#include "caf/none.hpp" #include "caf/none.hpp"
#include "caf/skip.hpp" #include "caf/skip.hpp"
#include "caf/error.hpp" #include "caf/error.hpp"
#include "caf/expected.hpp"
#include "caf/message.hpp" #include "caf/message.hpp"
#include "caf/delegated.hpp" #include "caf/delegated.hpp"
#include "caf/detail/type_list.hpp"
namespace caf { namespace caf {
enum result_runtime_type { enum result_runtime_type {
...@@ -48,13 +51,7 @@ public: ...@@ -48,13 +51,7 @@ public:
init(std::move(x), std::move(xs)...); init(std::move(x), std::move(xs)...);
} }
template <class E, template <class E, class = enable_if_has_make_error_t<E>>
class = typename std::enable_if<
std::is_same<
decltype(make_error(std::declval<const E&>())),
error
>::value
>::type>
result(E x) : flag(rt_error), err(make_error(x)) { result(E x) : flag(rt_error), err(make_error(x)) {
// nop // nop
} }
...@@ -63,6 +60,26 @@ public: ...@@ -63,6 +60,26 @@ public:
// nop // nop
} }
template <
class T,
class = typename std::enable_if<
sizeof...(Ts) == 1
&& std::is_convertible<
T,
detail::tl_head_t<detail::type_list<Ts...>>
>::value
>::type
>
result(expected<T> x) {
if (x) {
flag = rt_value;
init(std::move(*x));
} else {
flag = rt_error;
err = std::move(x.error());
}
}
result(skip_t) : flag(rt_skip) { result(skip_t) : flag(rt_skip) {
// nop // nop
} }
...@@ -100,13 +117,7 @@ public: ...@@ -100,13 +117,7 @@ public:
// nop // nop
} }
template <class E, template <class E, class = enable_if_has_make_error_t<E>>
class = typename std::enable_if<
std::is_same<
decltype(make_error(std::declval<const E&>())),
error
>::value
>::type>
result(E x) : flag(rt_error), err(make_error(x)) { result(E x) : flag(rt_error), err(make_error(x)) {
// nop // nop
} }
...@@ -115,6 +126,15 @@ public: ...@@ -115,6 +126,15 @@ public:
// nop // nop
} }
result(expected<void> x) {
if (x) {
flag = rt_value;
} else {
flag = rt_error;
err = std::move(x.error());
}
}
result(skip_t) : flag(rt_skip) { result(skip_t) : flag(rt_skip) {
// nop // nop
} }
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/config.hpp"
#define CAF_SUITE result
#include "caf/test/unit_test.hpp"
#include "caf/sec.hpp"
#include "caf/result.hpp"
using namespace std;
using namespace caf;
CAF_TEST(skip) {
auto x = result<>{skip()};
CAF_CHECK_EQUAL(x.flag, rt_skip);
CAF_CHECK(x.value.empty());
}
CAF_TEST(value) {
auto x = result<int>{42};
CAF_CHECK_EQUAL(x.flag, rt_value);
CAF_CHECK_EQUAL(x.value.get_as<int>(0), 42);
}
CAF_TEST(expected) {
auto x = result<int>{expected<int>{42}};
CAF_CHECK_EQUAL(x.flag, rt_value);
CAF_CHECK_EQUAL(x.value.get_as<int>(0), 42);
x = expected<int>{sec::unexpected_message};
CAF_CHECK_EQUAL(x.flag, rt_error);
CAF_CHECK_EQUAL(x.err, make_error(sec::unexpected_message));
CAF_CHECK(x.value.empty());
}
CAF_TEST(void_specialization) {
auto x = result<void>{};
CAF_CHECK_EQUAL(x.flag, rt_value);
x = skip();
CAF_CHECK_EQUAL(x.flag, rt_skip);
x = expected<void>{};
CAF_CHECK_EQUAL(x.flag, rt_value);
x = expected<void>{sec::unexpected_message};
CAF_CHECK_EQUAL(x.flag, rt_error);
CAF_CHECK_EQUAL(x.err, make_error(sec::unexpected_message));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment