mirror of
https://github.com/BlackMATov/promise.hpp.git
synced 2025-12-15 12:29:50 +07:00
is_promise, is_promise_r, make_resolved_promise, make_rejected_promise
This commit is contained in:
113
promise.hpp
113
promise.hpp
@@ -25,33 +25,43 @@ namespace promise_hpp
|
||||
class promise;
|
||||
|
||||
//
|
||||
// make_promise
|
||||
// is_promise
|
||||
//
|
||||
|
||||
template < typename R, typename F >
|
||||
promise<R> make_promise(F&& f) {
|
||||
promise<R> result;
|
||||
namespace impl
|
||||
{
|
||||
template < typename T >
|
||||
struct is_promise_impl
|
||||
: std::false_type {};
|
||||
|
||||
auto resolver = std::bind([](promise<R>& p, auto&& v){
|
||||
p.resolve(std::forward<decltype(v)>(v));
|
||||
}, result, std::placeholders::_1);
|
||||
|
||||
auto rejector = std::bind([](promise<R>& p, auto&& e){
|
||||
p.reject(std::forward<decltype(e)>(e));
|
||||
}, result, std::placeholders::_1);
|
||||
|
||||
try {
|
||||
invoke_hpp::invoke(
|
||||
std::forward<F>(f),
|
||||
std::move(resolver),
|
||||
std::move(rejector));
|
||||
} catch (...) {
|
||||
result.reject(std::current_exception());
|
||||
}
|
||||
|
||||
return result;
|
||||
template < typename R >
|
||||
struct is_promise_impl<promise<R>>
|
||||
: std::true_type {};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
struct is_promise
|
||||
: impl::is_promise_impl<std::remove_cv_t<T>> {};
|
||||
|
||||
//
|
||||
// is_promise_r
|
||||
//
|
||||
|
||||
namespace impl
|
||||
{
|
||||
template < typename R, typename T >
|
||||
struct is_promise_r_impl
|
||||
: std::false_type {};
|
||||
|
||||
template < typename R, typename PR >
|
||||
struct is_promise_r_impl<R, promise<PR>>
|
||||
: std::is_convertible<PR, R> {};
|
||||
}
|
||||
|
||||
template < typename R, typename T >
|
||||
struct is_promise_r
|
||||
: impl::is_promise_r_impl<R, std::remove_cv_t<T>> {};
|
||||
|
||||
//
|
||||
// promise<T>
|
||||
//
|
||||
@@ -59,6 +69,8 @@ namespace promise_hpp
|
||||
template < typename T >
|
||||
class promise final {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
enum class status : std::uint8_t {
|
||||
pending,
|
||||
resolved,
|
||||
@@ -298,6 +310,8 @@ namespace promise_hpp
|
||||
template <>
|
||||
class promise<void> final {
|
||||
public:
|
||||
using value_type = void;
|
||||
|
||||
enum class status : std::uint8_t {
|
||||
pending,
|
||||
resolved,
|
||||
@@ -487,4 +501,59 @@ namespace promise_hpp
|
||||
std::vector<handler> handlers_;
|
||||
};
|
||||
};
|
||||
|
||||
//
|
||||
// make_promise
|
||||
//
|
||||
|
||||
template < typename R, typename F >
|
||||
promise<R> make_promise(F&& f) {
|
||||
promise<R> result;
|
||||
|
||||
auto resolver = std::bind([](promise<R>& p, auto&& v){
|
||||
p.resolve(std::forward<decltype(v)>(v));
|
||||
}, result, std::placeholders::_1);
|
||||
|
||||
auto rejector = std::bind([](promise<R>& p, auto&& e){
|
||||
p.reject(std::forward<decltype(e)>(e));
|
||||
}, result, std::placeholders::_1);
|
||||
|
||||
try {
|
||||
invoke_hpp::invoke(
|
||||
std::forward<F>(f),
|
||||
std::move(resolver),
|
||||
std::move(rejector));
|
||||
} catch (...) {
|
||||
result.reject(std::current_exception());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
inline promise<void> make_resolved_promise() {
|
||||
promise<void> result;
|
||||
result.resolve();
|
||||
return result;
|
||||
}
|
||||
|
||||
template < typename R >
|
||||
promise<std::decay_t<R>> make_resolved_promise(R&& v) {
|
||||
promise<std::decay_t<R>> result;
|
||||
result.resolve(std::forward<R>(v));
|
||||
return result;
|
||||
}
|
||||
|
||||
template < typename E >
|
||||
promise<void> make_rejected_promise(E&& e) {
|
||||
promise<void> result;
|
||||
result.reject(std::forward<E>(e));
|
||||
return result;
|
||||
}
|
||||
|
||||
template < typename R, typename E >
|
||||
promise<R> make_rejected_promise(E&& e) {
|
||||
promise<R> result;
|
||||
result.reject(std::forward<E>(e));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
112
tests.cpp
112
tests.cpp
@@ -6,6 +6,9 @@ namespace pr = promise_hpp;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct obj_t {
|
||||
};
|
||||
|
||||
bool check_hello_fail_exception(std::exception_ptr e) {
|
||||
try {
|
||||
std::rethrow_exception(e);
|
||||
@@ -17,6 +20,79 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("is_promise") {
|
||||
SECTION("positive") {
|
||||
static_assert(
|
||||
pr::is_promise<pr::promise<void>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
pr::is_promise<const pr::promise<void>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
pr::is_promise<const volatile pr::promise<void>>::value,
|
||||
"unit test fail");
|
||||
|
||||
static_assert(
|
||||
pr::is_promise<pr::promise<int>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
pr::is_promise<const pr::promise<int>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
pr::is_promise<const volatile pr::promise<int>>::value,
|
||||
"unit test fail");
|
||||
}
|
||||
SECTION("negative") {
|
||||
static_assert(
|
||||
!pr::is_promise<pr::promise<void>&>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
!pr::is_promise<const pr::promise<void>*>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
!pr::is_promise<const volatile pr::promise<int>&>::value,
|
||||
"unit test fail");
|
||||
|
||||
static_assert(
|
||||
!pr::is_promise<int>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
!pr::is_promise<void>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
!pr::is_promise<const volatile int>::value,
|
||||
"unit test fail");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("is_promise_r") {
|
||||
SECTION("positive") {
|
||||
static_assert(
|
||||
pr::is_promise_r<void, pr::promise<void>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
pr::is_promise_r<int, const pr::promise<int>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
pr::is_promise_r<long, const pr::promise<int>>::value,
|
||||
"unit test fail");
|
||||
}
|
||||
SECTION("negative") {
|
||||
static_assert(
|
||||
!pr::is_promise_r<void, pr::promise<int>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
!pr::is_promise_r<void, const pr::promise<int>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
!pr::is_promise_r<int, pr::promise<obj_t>>::value,
|
||||
"unit test fail");
|
||||
static_assert(
|
||||
!pr::is_promise_r<long, int>::value,
|
||||
"unit test fail");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("promise") {
|
||||
SECTION("resolved") {
|
||||
{
|
||||
@@ -191,6 +267,42 @@ TEST_CASE("promise") {
|
||||
REQUIRE(call_fail_with_logic_error);
|
||||
}
|
||||
}
|
||||
SECTION("make_resolved_promise") {
|
||||
{
|
||||
bool call_check = false;
|
||||
pr::make_resolved_promise()
|
||||
.then([&call_check]{
|
||||
call_check = true;
|
||||
});
|
||||
REQUIRE(call_check);
|
||||
}
|
||||
{
|
||||
int check_42_int = 0;
|
||||
pr::make_resolved_promise(42)
|
||||
.then([&check_42_int](int value){
|
||||
check_42_int = value;
|
||||
});
|
||||
REQUIRE(check_42_int == 42);
|
||||
}
|
||||
}
|
||||
SECTION("make_rejected_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){
|
||||
call_fail_with_logic_error = check_hello_fail_exception(e);
|
||||
});
|
||||
REQUIRE(call_fail_with_logic_error);
|
||||
}
|
||||
{
|
||||
bool call_fail_with_logic_error = 0;
|
||||
pr::make_rejected_promise(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);
|
||||
}
|
||||
}
|
||||
SECTION("exceptions") {
|
||||
{
|
||||
bool not_call_then_on_reject = true;
|
||||
|
||||
Reference in New Issue
Block a user