Commit aa845e40 authored by octal's avatar octal

async: added static functions to directly resolve or reject a promise

parent 0b87248e
...@@ -458,6 +458,7 @@ namespace Async { ...@@ -458,6 +458,7 @@ namespace Async {
typedef std::function<void (Resolver&, Rejection&)> ResolveFunc; typedef std::function<void (Resolver&, Rejection&)> ResolveFunc;
typedef Private::CoreT<T> Core; typedef Private::CoreT<T> Core;
Promise(ResolveFunc func) Promise(ResolveFunc func)
: core_(std::make_shared<Core>()) : core_(std::make_shared<Core>())
, resolver_(core_) , resolver_(core_)
...@@ -476,6 +477,40 @@ namespace Async { ...@@ -476,6 +477,40 @@ namespace Async {
{ {
} }
template<typename U>
static
Promise<T>
resolved(U&& value) {
static_assert(!std::is_void<T>::value,
"Can not resolve a void promise with parameters");
static_assert(std::is_same<T, U>::value || std::is_convertible<U, T>::value,
"Incompatible value type");
auto core = std::make_shared<Core>();
core->template construct<T>(std::forward<U>(value));
return Promise<T>(std::move(core));
}
static
Promise<void>
resolved() {
static_assert(std::is_void<T>::value,
"Resolving a non-void promise requires parameters");
auto core = std::make_shared<Core>();
core->state = State::Fulfilled;
return Promise<T>(std::move(core));
}
template<typename Exc>
static Promise<T>
rejected(Exc exc) {
auto core = std::make_shared<Core>();
core->exc = std::make_exception_ptr(exc);
core->state = State::Rejected;
return Promise<T>(std::move(core));
}
bool isPending() const { return core_->state == State::Pending; } bool isPending() const { return core_->state == State::Pending; }
bool isFulfilled() const { return core_->state == State::Fulfilled; } bool isFulfilled() const { return core_->state == State::Fulfilled; }
bool isRejected() const { return core_->state == State::Rejected; } bool isRejected() const { return core_->state == State::Rejected; }
...@@ -528,6 +563,12 @@ namespace Async { ...@@ -528,6 +563,12 @@ namespace Async {
{ {
} }
Promise(std::shared_ptr<Core>&& core)
: core_(core)
, resolver_(core_)
, rejection_(core_)
{ }
template<typename ResolveFunc> template<typename ResolveFunc>
void thenStaticChecks(std::true_type /* is_void */) { void thenStaticChecks(std::true_type /* is_void */) {
static_assert(detail::FunctionTrait<ResolveFunc>::ArgsCount == 0, static_assert(detail::FunctionTrait<ResolveFunc>::ArgsCount == 0,
......
...@@ -49,6 +49,14 @@ TEST(async_test, basic_test) { ...@@ -49,6 +49,14 @@ TEST(async_test, basic_test) {
ASSERT_THROW(std::rethrow_exception(eptr), std::runtime_error); ASSERT_THROW(std::rethrow_exception(eptr), std::runtime_error);
}); });
auto p4 = Async::Promise<int>::resolved(10);
ASSERT_TRUE(p4.isFulfilled());
auto p5 = Async::Promise<void>::resolved();
ASSERT_TRUE(p5.isFulfilled());
auto p6 = Async::Promise<int>::rejected(std::invalid_argument("Invalid"));
ASSERT_TRUE(p6.isRejected());
} }
TEST(async_test, void_promise) { TEST(async_test, void_promise) {
......
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