promises chaining

This commit is contained in:
2018-12-09 22:33:31 +07:00
parent d572288cb6
commit 1148557b2f
2 changed files with 380 additions and 112 deletions

View File

@@ -62,6 +62,57 @@ namespace promise_hpp
struct is_promise_r
: impl::is_promise_r_impl<R, std::remove_cv_t<T>> {};
//
// detail
//
namespace detail
{
class noncopyable {
public:
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
protected:
noncopyable() = default;
~noncopyable() = default;
};
template < typename T >
class storage final : private noncopyable {
public:
storage() = default;
~storage() noexcept(std::is_nothrow_destructible<T>::value) {
if ( initialized_ ) {
ptr_()->~T();
}
}
template < typename U >
void set(U&& value) noexcept(std::is_nothrow_constructible<T,U&&>::value) {
assert(!initialized_);
::new(ptr_()) T(std::forward<U>(value));
initialized_ = true;
}
const T& value() const noexcept {
assert(initialized_);
return *ptr_();
}
private:
T* ptr_() noexcept {
return reinterpret_cast<T*>(&data_);
}
const T* ptr_() const noexcept {
return reinterpret_cast<const T*>(&data_);
}
private:
std::aligned_storage_t<sizeof(T), alignof(T)> data_;
bool initialized_ = false;
};
}
//
// promise<T>
//
@@ -82,7 +133,64 @@ namespace promise_hpp
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
promise<ResolveFR> then(ResolveF&& on_resolve) {
std::enable_if_t<
is_promise<ResolveFR>::value && !std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
](const T& v) mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
np.then([n = n](const typename ResolveFR::value_type& nv) mutable {
n.resolve(nv);
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
std::enable_if_t<
is_promise<ResolveFR>::value && std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
](const T& v) mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
np.then([n = n]() mutable {
n.resolve();
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
std::enable_if_t<
!is_promise<ResolveFR>::value,
promise<ResolveFR>>
then(ResolveF&& on_resolve) {
promise<ResolveFR> next;
state_->attach(
next,
@@ -91,18 +199,6 @@ namespace promise_hpp
return next;
}
template < typename ResolveF
, typename RejectF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
promise<ResolveFR> then(ResolveF&& on_resolve, RejectF&& on_reject) {
promise<ResolveFR> next;
state_->attach(
next,
std::forward<ResolveF>(on_resolve),
std::forward<RejectF>(on_reject));
return next;
}
template < typename RejectF >
promise<T> fail(RejectF&& on_reject) {
promise<T> next;
@@ -133,50 +229,10 @@ namespace promise_hpp
class state;
std::shared_ptr<state> state_;
private:
class storage final {
public:
storage() = default;
storage(const storage&) = delete;
storage& operator=(const storage&) = delete;
~storage() noexcept(std::is_nothrow_destructible<T>::value) {
if ( initialized_ ) {
ptr_()->~T();
}
}
template < typename U >
void set(U&& value) {
assert(!initialized_);
::new(ptr_()) T(std::forward<U>(value));
initialized_ = true;
}
const T& value() const noexcept {
assert(initialized_);
return *ptr_();
}
private:
T* ptr_() noexcept {
return reinterpret_cast<T*>(&data_);
}
const T* ptr_() const noexcept {
return reinterpret_cast<const T*>(&data_);
}
private:
std::aligned_storage_t<sizeof(T), alignof(T)> data_;
bool initialized_ = false;
};
class state final {
class state final : private detail::noncopyable {
public:
state() = default;
state(const state&) = delete;
state& operator=(const state&) = delete;
template < typename U >
void resolve(U&& value) {
std::lock_guard<std::mutex> guard(mutex_);
@@ -200,32 +256,32 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<!std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) {
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = [
p = other,
n = next,
f = std::forward<ResolveF>(resolve)
](const T& v) mutable {
try {
auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
p.resolve(std::move(r));
n.resolve(std::move(r));
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};
auto reject_h = [
p = other,
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try {
invoke_hpp::invoke(
std::forward<decltype(f)>(f),
e);
p.reject(e);
n.reject(e);
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};
@@ -248,32 +304,32 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) {
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = [
p = other,
n = next,
f = std::forward<ResolveF>(resolve)
](const T& v) mutable {
try {
invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
p.resolve();
n.resolve();
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};
auto reject_h = [
p = other,
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try {
invoke_hpp::invoke(
std::forward<decltype(f)>(f),
e);
p.reject(e);
n.reject(e);
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};
@@ -309,7 +365,7 @@ namespace promise_hpp
handlers_.clear();
}
private:
storage storage_;
detail::storage<T> storage_;
status status_ = status::pending;
std::exception_ptr exception_ = nullptr;
@@ -352,7 +408,62 @@ namespace promise_hpp
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
promise<ResolveFR> then(ResolveF&& on_resolve) {
std::enable_if_t<
is_promise<ResolveFR>::value && !std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
]() mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
np.then([n = n](const typename ResolveFR::value_type& nv) mutable {
n.resolve(nv);
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
std::enable_if_t<
is_promise<ResolveFR>::value && std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
]() mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
np.then([n = n]() mutable {
n.resolve();
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
std::enable_if_t<
!is_promise<ResolveFR>::value,
promise<ResolveFR>>
then(ResolveF&& on_resolve) {
promise<ResolveFR> next;
state_->attach(
next,
@@ -361,18 +472,6 @@ namespace promise_hpp
return next;
}
template < typename ResolveF
, typename RejectF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
promise<ResolveFR> then(ResolveF&& on_resolve, RejectF&& on_reject) {
promise<ResolveFR> next;
state_->attach(
next,
std::forward<ResolveF>(on_resolve),
std::forward<RejectF>(on_reject));
return next;
}
template < typename RejectF >
promise<void> fail(RejectF&& on_reject) {
promise<void> next;
@@ -402,13 +501,10 @@ namespace promise_hpp
class state;
std::shared_ptr<state> state_;
private:
class state final {
class state final : private detail::noncopyable {
public:
state() = default;
state(const state&) = delete;
state& operator=(const state&) = delete;
void resolve() {
std::lock_guard<std::mutex> guard(mutex_);
if ( status_ != status::pending ) {
@@ -430,31 +526,31 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<!std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) {
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = [
p = other,
n = next,
f = std::forward<ResolveF>(resolve)
]() mutable {
try {
auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
p.resolve(std::move(r));
n.resolve(std::move(r));
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};
auto reject_h = [
p = other,
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try {
invoke_hpp::invoke(
std::forward<decltype(f)>(f),
e);
p.reject(e);
n.reject(e);
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};
@@ -476,31 +572,31 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) {
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = [
p = other,
n = next,
f = std::forward<ResolveF>(resolve)
]() mutable {
try {
invoke_hpp::invoke(
std::forward<decltype(f)>(f));
p.resolve();
n.resolve();
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};
auto reject_h = [
p = other,
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try {
invoke_hpp::invoke(
std::forward<decltype(f)>(f),
e);
p.reject(e);
n.reject(e);
} catch (...) {
p.reject(std::current_exception());
n.reject(std::current_exception());
}
};

196
tests.cpp
View File

@@ -114,16 +114,6 @@ TEST_CASE("promise") {
});
REQUIRE(check_42_int == 42);
}
{
int check_42_int = 0;
pr::promise<int>()
.resolve(42)
.then([&check_42_int](int value){
check_42_int = value;
}, [](std::exception_ptr){
});
REQUIRE(check_42_int == 42);
}
{
int check_84_int = 0;
bool check_void_call = false;
@@ -167,7 +157,7 @@ TEST_CASE("promise") {
pr::promise<int>()
.reject(ee)
.then([](int){
}).fail([&call_fail_with_logic_error](const std::exception_ptr& e){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
@@ -178,7 +168,7 @@ TEST_CASE("promise") {
pr::promise<int>()
.reject(std::make_exception_ptr(ee))
.then([](int){
}).fail([&call_fail_with_logic_error](const std::exception_ptr& e){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
@@ -382,4 +372,186 @@ TEST_CASE("promise") {
REQUIRE(pb_value == 21);
}
}
SECTION("chaining") {
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
return p2;
}).then([&check_84_int](int v2){
check_84_int = v2;
});
REQUIRE(check_84_int == 84);
}
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](){
return p2;
}).then([&check_84_int](int v){
check_84_int = v;
});
REQUIRE(check_84_int == 84);
}
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise();
p1.then([&p2](int v){
return p2;
}).then([&check_84_int](){
check_84_int = 84;
});
REQUIRE(check_84_int == 84);
}
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
return p2;
}).then([&check_84_int](){
check_84_int = 84;
});
REQUIRE(check_84_int == 84);
}
}
SECTION("typed_chaining_fails") {
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
(void)v;
throw std::logic_error("hello fail");
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
throw std::logic_error("hello fail");
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
}
SECTION("void_chaining_fails") {
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
throw std::logic_error("hello fail");
return p2;
}).then([&call_fail_with_logic_error](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
throw std::logic_error("hello fail");
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
}
}