mirror of
https://github.com/BlackMATov/promise.hpp.git
synced 2025-12-15 04:25:28 +07:00
@@ -1,16 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
project(promise)
|
||||
|
||||
if(MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
|
||||
endif(MSVC)
|
||||
|
||||
file(GLOB test_sources "*.cpp" "*.hpp")
|
||||
add_executable(${PROJECT_NAME} ${test_sources})
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
CXX_STANDARD 14
|
||||
CXX_STANDARD_REQUIRED YES
|
||||
CXX_EXTENSIONS NO)
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE /bigobj)
|
||||
endif(MSVC)
|
||||
|
||||
enable_testing()
|
||||
add_test(${PROJECT_NAME} ${PROJECT_NAME})
|
||||
|
||||
370
promise.hpp
370
promise.hpp
@@ -133,33 +133,6 @@ namespace promise_hpp
|
||||
promise()
|
||||
: state_(std::make_shared<state>()) {}
|
||||
|
||||
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](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<
|
||||
@@ -177,10 +150,37 @@ namespace promise_hpp
|
||||
v);
|
||||
np.then([n = n]() mutable {
|
||||
n.resolve();
|
||||
}).fail([n = n](std::exception_ptr e) mutable {
|
||||
}, [n = n](std::exception_ptr e) mutable {
|
||||
n.reject(e);
|
||||
});
|
||||
}).fail([n = next](std::exception_ptr e) mutable {
|
||||
}, [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](const typename ResolveFR::value_type& nv) mutable {
|
||||
n.resolve(nv);
|
||||
}, [n = n](std::exception_ptr e) mutable {
|
||||
n.reject(e);
|
||||
});
|
||||
}, [n = next](std::exception_ptr e) mutable {
|
||||
n.reject(e);
|
||||
});
|
||||
|
||||
@@ -211,42 +211,53 @@ namespace promise_hpp
|
||||
});
|
||||
}
|
||||
|
||||
template < typename ResolveF
|
||||
, typename RejectF
|
||||
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
|
||||
std::enable_if_t<
|
||||
!is_promise<ResolveFR>::value,
|
||||
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 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,
|
||||
return then(
|
||||
std::forward<ResolveF>(on_resolve),
|
||||
[](std::exception_ptr){});
|
||||
return next;
|
||||
}
|
||||
|
||||
template < typename RejectF >
|
||||
promise<T> fail(RejectF&& on_reject) {
|
||||
promise<T> next;
|
||||
state_->attach(
|
||||
next,
|
||||
promise<T> except(RejectF&& on_reject) {
|
||||
return then(
|
||||
[](const T& value) { return value; },
|
||||
std::forward<RejectF>(on_reject));
|
||||
return next;
|
||||
}
|
||||
|
||||
template < typename U >
|
||||
bool resolve(U&& value) {
|
||||
return state_->resolve(std::forward<U>(value));
|
||||
return state_->resolve(
|
||||
std::forward<U>(value));
|
||||
}
|
||||
|
||||
bool reject(std::exception_ptr e) {
|
||||
bool reject(std::exception_ptr e) noexcept {
|
||||
return state_->reject(e);
|
||||
}
|
||||
|
||||
template < typename E >
|
||||
bool reject(E&& e) {
|
||||
return state_->reject(std::make_exception_ptr(std::forward<E>(e)));
|
||||
return state_->reject(
|
||||
std::make_exception_ptr(std::forward<E>(e)));
|
||||
}
|
||||
private:
|
||||
class state;
|
||||
@@ -268,7 +279,7 @@ namespace promise_hpp
|
||||
return true;
|
||||
}
|
||||
|
||||
bool reject(std::exception_ptr e) {
|
||||
bool reject(std::exception_ptr e) noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
if ( status_ != status::pending ) {
|
||||
return false;
|
||||
@@ -280,22 +291,8 @@ namespace promise_hpp
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<!std::is_void<U>::value, void>
|
||||
std::enable_if_t<std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
auto resolve_h = [
|
||||
n = next,
|
||||
f = std::forward<ResolveF>(resolve)
|
||||
](const T& v) mutable {
|
||||
try {
|
||||
auto r = invoke_hpp::invoke(
|
||||
std::forward<decltype(f)>(f),
|
||||
v);
|
||||
n.resolve(std::move(r));
|
||||
} catch (...) {
|
||||
n.reject(std::current_exception());
|
||||
}
|
||||
};
|
||||
|
||||
auto reject_h = [
|
||||
n = next,
|
||||
f = std::forward<RejectF>(reject)
|
||||
@@ -310,29 +307,10 @@ namespace promise_hpp
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
|
||||
if ( status_ == status::resolved ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(resolve_h),
|
||||
storage_.value());
|
||||
} else if ( status_ == status::rejected ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(reject_h),
|
||||
exception_);
|
||||
} else {
|
||||
handlers_.emplace_back(
|
||||
std::move(resolve_h),
|
||||
std::move(reject_h));
|
||||
}
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
auto resolve_h = [
|
||||
n = next,
|
||||
f = std::forward<ResolveF>(resolve)
|
||||
f = std::forward<ResolveF>(resolve),
|
||||
j = reject_h
|
||||
](const T& v) mutable {
|
||||
try {
|
||||
invoke_hpp::invoke(
|
||||
@@ -340,10 +318,19 @@ namespace promise_hpp
|
||||
v);
|
||||
n.resolve();
|
||||
} catch (...) {
|
||||
n.reject(std::current_exception());
|
||||
invoke_hpp::invoke(
|
||||
std::move(j),
|
||||
std::current_exception());
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
add_handlers_(std::move(resolve_h), std::move(reject_h));
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<!std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
auto reject_h = [
|
||||
n = next,
|
||||
f = std::forward<RejectF>(reject)
|
||||
@@ -358,23 +345,44 @@ namespace promise_hpp
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
auto resolve_h = [
|
||||
n = next,
|
||||
f = std::forward<ResolveF>(resolve),
|
||||
j = reject_h
|
||||
](const T& v) mutable {
|
||||
try {
|
||||
auto r = invoke_hpp::invoke(
|
||||
std::forward<decltype(f)>(f),
|
||||
v);
|
||||
n.resolve(std::move(r));
|
||||
} catch (...) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(j),
|
||||
std::current_exception());
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
add_handlers_(std::move(resolve_h), std::move(reject_h));
|
||||
}
|
||||
private:
|
||||
template < typename ResolveF, typename RejectF >
|
||||
void add_handlers_(ResolveF&& resolve, RejectF&& reject) {
|
||||
if ( status_ == status::resolved ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(resolve_h),
|
||||
std::forward<ResolveF>(resolve),
|
||||
storage_.value());
|
||||
} else if ( status_ == status::rejected ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(reject_h),
|
||||
std::forward<RejectF>(reject),
|
||||
exception_);
|
||||
} else {
|
||||
handlers_.emplace_back(
|
||||
std::move(resolve_h),
|
||||
std::move(reject_h));
|
||||
std::forward<ResolveF>(resolve),
|
||||
std::forward<RejectF>(reject));
|
||||
}
|
||||
}
|
||||
private:
|
||||
|
||||
void invoke_resolve_handlers_() noexcept {
|
||||
const T& value = storage_.value();
|
||||
for ( const auto& h : handlers_ ) {
|
||||
@@ -431,32 +439,6 @@ namespace promise_hpp
|
||||
promise()
|
||||
: state_(std::make_shared<state>()) {}
|
||||
|
||||
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](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<
|
||||
@@ -473,10 +455,36 @@ namespace promise_hpp
|
||||
std::forward<decltype(f)>(f));
|
||||
np.then([n = n]() mutable {
|
||||
n.resolve();
|
||||
}).fail([n = n](std::exception_ptr e) mutable {
|
||||
}, [n = n](std::exception_ptr e) mutable {
|
||||
n.reject(e);
|
||||
});
|
||||
}).fail([n = next](std::exception_ptr e) mutable {
|
||||
}, [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](const typename ResolveFR::value_type& nv) mutable {
|
||||
n.resolve(nv);
|
||||
}, [n = n](std::exception_ptr e) mutable {
|
||||
n.reject(e);
|
||||
});
|
||||
}, [n = next](std::exception_ptr e) mutable {
|
||||
n.reject(e);
|
||||
});
|
||||
|
||||
@@ -505,41 +513,51 @@ namespace promise_hpp
|
||||
});
|
||||
}
|
||||
|
||||
template < typename ResolveF
|
||||
, typename RejectF
|
||||
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
|
||||
std::enable_if_t<
|
||||
!is_promise<ResolveFR>::value,
|
||||
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 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,
|
||||
return then(
|
||||
std::forward<ResolveF>(on_resolve),
|
||||
[](std::exception_ptr){});
|
||||
return next;
|
||||
}
|
||||
|
||||
template < typename RejectF >
|
||||
promise<void> fail(RejectF&& on_reject) {
|
||||
promise<void> next;
|
||||
state_->attach(
|
||||
next,
|
||||
promise<void> except(RejectF&& on_reject) {
|
||||
return then(
|
||||
[]{},
|
||||
std::forward<RejectF>(on_reject));
|
||||
return next;
|
||||
}
|
||||
|
||||
bool resolve() {
|
||||
return state_->resolve();
|
||||
}
|
||||
|
||||
bool reject(std::exception_ptr e) {
|
||||
bool reject(std::exception_ptr e) noexcept {
|
||||
return state_->reject(e);
|
||||
}
|
||||
|
||||
template < typename E >
|
||||
bool reject(E&& e) {
|
||||
return state_->reject(std::make_exception_ptr(std::forward<E>(e)));
|
||||
return state_->reject(
|
||||
std::make_exception_ptr(std::forward<E>(e)));
|
||||
}
|
||||
private:
|
||||
class state;
|
||||
@@ -559,7 +577,7 @@ namespace promise_hpp
|
||||
return true;
|
||||
}
|
||||
|
||||
bool reject(std::exception_ptr e) {
|
||||
bool reject(std::exception_ptr e) noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
if ( status_ != status::pending ) {
|
||||
return false;
|
||||
@@ -571,21 +589,8 @@ namespace promise_hpp
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<!std::is_void<U>::value, void>
|
||||
std::enable_if_t<std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
auto resolve_h = [
|
||||
n = next,
|
||||
f = std::forward<ResolveF>(resolve)
|
||||
]() mutable {
|
||||
try {
|
||||
auto r = invoke_hpp::invoke(
|
||||
std::forward<decltype(f)>(f));
|
||||
n.resolve(std::move(r));
|
||||
} catch (...) {
|
||||
n.reject(std::current_exception());
|
||||
}
|
||||
};
|
||||
|
||||
auto reject_h = [
|
||||
n = next,
|
||||
f = std::forward<RejectF>(reject)
|
||||
@@ -600,38 +605,29 @@ namespace promise_hpp
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
|
||||
if ( status_ == status::resolved ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(resolve_h));
|
||||
} else if ( status_ == status::rejected ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(reject_h),
|
||||
exception_);
|
||||
} else {
|
||||
handlers_.emplace_back(
|
||||
std::move(resolve_h),
|
||||
std::move(reject_h));
|
||||
}
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
auto resolve_h = [
|
||||
n = next,
|
||||
f = std::forward<ResolveF>(resolve)
|
||||
f = std::forward<ResolveF>(resolve),
|
||||
j = reject_h
|
||||
]() mutable {
|
||||
try {
|
||||
invoke_hpp::invoke(
|
||||
std::forward<decltype(f)>(f));
|
||||
n.resolve();
|
||||
} catch (...) {
|
||||
n.reject(std::current_exception());
|
||||
invoke_hpp::invoke(
|
||||
std::move(j),
|
||||
std::current_exception());
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
add_handlers_(std::move(resolve_h), std::move(reject_h));
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<!std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
auto reject_h = [
|
||||
n = next,
|
||||
f = std::forward<RejectF>(reject)
|
||||
@@ -646,22 +642,42 @@ namespace promise_hpp
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
auto resolve_h = [
|
||||
n = next,
|
||||
f = std::forward<ResolveF>(resolve),
|
||||
j = reject_h
|
||||
]() mutable {
|
||||
try {
|
||||
auto r = invoke_hpp::invoke(
|
||||
std::forward<decltype(f)>(f));
|
||||
n.resolve(std::move(r));
|
||||
} catch (...) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(j),
|
||||
std::current_exception());
|
||||
}
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
add_handlers_(std::move(resolve_h), std::move(reject_h));
|
||||
}
|
||||
private:
|
||||
template < typename ResolveF, typename RejectF >
|
||||
void add_handlers_(ResolveF&& resolve, RejectF&& reject) {
|
||||
if ( status_ == status::resolved ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(resolve_h));
|
||||
std::forward<ResolveF>(resolve));
|
||||
} else if ( status_ == status::rejected ) {
|
||||
invoke_hpp::invoke(
|
||||
std::move(reject_h),
|
||||
std::forward<RejectF>(reject),
|
||||
exception_);
|
||||
} else {
|
||||
handlers_.emplace_back(
|
||||
std::move(resolve_h),
|
||||
std::move(reject_h));
|
||||
std::forward<ResolveF>(resolve),
|
||||
std::forward<RejectF>(reject));
|
||||
}
|
||||
}
|
||||
private:
|
||||
|
||||
void invoke_resolve_handlers_() noexcept {
|
||||
for ( const auto& h : handlers_ ) {
|
||||
h.resolve_();
|
||||
@@ -711,16 +727,12 @@ namespace promise_hpp
|
||||
promise<R> make_promise(F&& f) {
|
||||
promise<R> result;
|
||||
|
||||
auto resolver = [
|
||||
p = result
|
||||
](auto&& v) mutable {
|
||||
return p.resolve(std::forward<decltype(v)>(v));
|
||||
auto resolver = [result](auto&& v) mutable {
|
||||
return result.resolve(std::forward<decltype(v)>(v));
|
||||
};
|
||||
|
||||
auto rejector = [
|
||||
p = result
|
||||
](auto&& e) mutable {
|
||||
return p.reject(std::forward<decltype(e)>(e));
|
||||
auto rejector = [result](auto&& e) mutable {
|
||||
return result.reject(std::forward<decltype(e)>(e));
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -809,9 +821,7 @@ namespace promise_hpp
|
||||
if ( context->apply_result(result_index, v) ) {
|
||||
resolver(std::move(context->results));
|
||||
}
|
||||
}).fail([rejector](std::exception_ptr e) mutable {
|
||||
rejector(e);
|
||||
});
|
||||
}, rejector);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -838,11 +848,7 @@ namespace promise_hpp
|
||||
|
||||
return make_promise<child_promise_value_t>([begin, end](auto&& resolver, auto&& rejector){
|
||||
for ( auto iter = begin; iter != end; ++iter ) {
|
||||
(*iter).then([resolver](const child_promise_value_t& v) mutable {
|
||||
resolver(v);
|
||||
}).fail([rejector](std::exception_ptr e) mutable {
|
||||
rejector(e);
|
||||
});
|
||||
(*iter).then(resolver, rejector);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
73
tests.cpp
73
tests.cpp
@@ -18,6 +18,16 @@ namespace
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool check_hello_fail2_exception(std::exception_ptr e) {
|
||||
try {
|
||||
std::rethrow_exception(e);
|
||||
} catch (std::logic_error& ee) {
|
||||
return 0 == std::strcmp(ee.what(), "hello fail2");
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("is_promise") {
|
||||
@@ -108,7 +118,7 @@ TEST_CASE("promise") {
|
||||
int check_42_int = 0;
|
||||
auto p = pr::promise<int>();
|
||||
p.resolve(42);
|
||||
p.fail([](std::exception_ptr){
|
||||
p.except([](std::exception_ptr){
|
||||
}).then([&check_42_int](int value){
|
||||
check_42_int = value;
|
||||
});
|
||||
@@ -145,7 +155,7 @@ TEST_CASE("promise") {
|
||||
p.then([¬_call_then_on_reject](int value) {
|
||||
(void)value;
|
||||
not_call_then_on_reject = false;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
REQUIRE(not_call_then_on_reject);
|
||||
@@ -157,7 +167,7 @@ TEST_CASE("promise") {
|
||||
auto p = pr::promise<int>();
|
||||
p.reject(ee);
|
||||
p.then([](int){
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&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);
|
||||
@@ -168,7 +178,7 @@ TEST_CASE("promise") {
|
||||
auto p = pr::promise<int>();
|
||||
p.reject(std::make_exception_ptr(ee));
|
||||
p.then([](int){
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&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);
|
||||
@@ -177,9 +187,9 @@ TEST_CASE("promise") {
|
||||
int check_multi_fail = 0;
|
||||
auto p = pr::promise<>();
|
||||
p.reject(std::logic_error("hello fail"));
|
||||
p.fail([&check_multi_fail](std::exception_ptr){
|
||||
p.except([&check_multi_fail](std::exception_ptr){
|
||||
++check_multi_fail;
|
||||
}).fail([&check_multi_fail](std::exception_ptr){
|
||||
}).except([&check_multi_fail](std::exception_ptr){
|
||||
++check_multi_fail;
|
||||
});
|
||||
REQUIRE(check_multi_fail == 2);
|
||||
@@ -209,7 +219,7 @@ TEST_CASE("promise") {
|
||||
auto p = pr::promise<int>();
|
||||
p.then([¬_call_then_on_reject](int){
|
||||
not_call_then_on_reject = false;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
REQUIRE(not_call_then_on_reject);
|
||||
@@ -239,7 +249,7 @@ TEST_CASE("promise") {
|
||||
(void)resolve;
|
||||
reject(std::logic_error("hello fail"));
|
||||
});
|
||||
p.fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
p.except([&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);
|
||||
@@ -251,7 +261,7 @@ TEST_CASE("promise") {
|
||||
(void)reject;
|
||||
throw std::logic_error("hello fail");
|
||||
});
|
||||
p.fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
p.except([&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);
|
||||
@@ -279,7 +289,7 @@ TEST_CASE("promise") {
|
||||
{
|
||||
bool call_fail_with_logic_error = false;
|
||||
pr::make_rejected_promise<int>(std::logic_error("hello fail"))
|
||||
.fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
.except([&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);
|
||||
@@ -287,7 +297,7 @@ TEST_CASE("promise") {
|
||||
{
|
||||
bool call_fail_with_logic_error = false;
|
||||
pr::make_rejected_promise(std::logic_error("hello fail"))
|
||||
.fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
.except([&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);
|
||||
@@ -303,12 +313,27 @@ TEST_CASE("promise") {
|
||||
throw std::logic_error("hello fail");
|
||||
}).then([¬_call_then_on_reject](){
|
||||
not_call_then_on_reject = false;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
REQUIRE(not_call_then_on_reject);
|
||||
REQUIRE(call_fail_with_logic_error);
|
||||
}
|
||||
{
|
||||
bool not_call_then_on_reject = true;
|
||||
bool call_fail_with_logic_error = false;
|
||||
auto p = pr::promise<int>();
|
||||
p.resolve(42);
|
||||
p.then([](int){
|
||||
throw std::logic_error("hello fail");
|
||||
}, [](std::exception_ptr){
|
||||
throw std::logic_error("hello fail2");
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail2_exception(e);
|
||||
});
|
||||
REQUIRE(not_call_then_on_reject);
|
||||
REQUIRE(call_fail_with_logic_error);
|
||||
}
|
||||
}
|
||||
SECTION("multi_then") {
|
||||
{
|
||||
@@ -347,7 +372,7 @@ TEST_CASE("promise") {
|
||||
{
|
||||
auto pa = p.then([](int){
|
||||
throw std::logic_error("hello fail");
|
||||
}).fail([&pa_value](std::exception_ptr e){
|
||||
}).except([&pa_value](std::exception_ptr e){
|
||||
if ( check_hello_fail_exception(e) ) {
|
||||
pa_value = 84;
|
||||
}
|
||||
@@ -460,7 +485,7 @@ TEST_CASE("promise") {
|
||||
return p2;
|
||||
}).then([](int v2){
|
||||
(void)v2;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -477,7 +502,7 @@ TEST_CASE("promise") {
|
||||
}).then([](int v2){
|
||||
(void)v2;
|
||||
throw std::logic_error("hello fail");
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -493,7 +518,7 @@ TEST_CASE("promise") {
|
||||
return p2;
|
||||
}).then([](int v2){
|
||||
(void)v2;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -509,7 +534,7 @@ TEST_CASE("promise") {
|
||||
return p2;
|
||||
}).then([](int v2){
|
||||
(void)v2;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -526,7 +551,7 @@ TEST_CASE("promise") {
|
||||
throw std::logic_error("hello fail");
|
||||
return p2;
|
||||
}).then([](){
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -541,7 +566,7 @@ TEST_CASE("promise") {
|
||||
return p2;
|
||||
}).then([](){
|
||||
throw std::logic_error("hello fail");
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -555,7 +580,7 @@ TEST_CASE("promise") {
|
||||
p1.then([&p2](){
|
||||
return p2;
|
||||
}).then([](){
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -569,7 +594,7 @@ TEST_CASE("promise") {
|
||||
p1.then([&p2](){
|
||||
return p2;
|
||||
}).then([](){
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
|
||||
@@ -666,7 +691,7 @@ TEST_CASE("promise") {
|
||||
}).then([¬_call_then_on_reject](const std::vector<int>& c){
|
||||
(void)c;
|
||||
not_call_then_on_reject = false;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
REQUIRE(not_call_then_on_reject);
|
||||
@@ -686,7 +711,7 @@ TEST_CASE("promise") {
|
||||
}).then([¬_call_then_on_reject](const int& c){
|
||||
(void)c;
|
||||
not_call_then_on_reject = false;
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
REQUIRE(not_call_then_on_reject);
|
||||
@@ -746,7 +771,7 @@ TEST_CASE("promise") {
|
||||
return std::vector<pr::promise<int>>{
|
||||
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
|
||||
pr::make_resolved_promise(42)};
|
||||
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
|
||||
}).except([&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);
|
||||
|
||||
Reference in New Issue
Block a user