From e609deb93ca365b253efa2173ce83046835a8d84 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 1 Feb 2018 14:14:32 -0500 Subject: ... --- promise.hpp | 20 ++++++++++---------- test.cpp | 23 ++++++++++++++++++----- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/promise.hpp b/promise.hpp index 4f1ea06..0f9198c 100644 --- a/promise.hpp +++ b/promise.hpp @@ -128,7 +128,7 @@ namespace promise { std::is_same::ret_type, promise_t>::value>::type gen_on_fulfilled(Func on_fulfilled, const promise_t &npm, function &ret) { - ret = [this, on_fulfilled, npm]() { + ret = [this, on_fulfilled, npm]() mutable { on_fulfilled(result).then_any( [npm] (pm_any_t result_) { npm.resolve(result_); @@ -146,7 +146,7 @@ namespace promise { std::is_same::ret_type, pm_any_t>::value>::type gen_on_fulfilled(Func on_fulfilled, const promise_t &npm, function &ret) { - ret = [this, on_fulfilled, npm]() { + ret = [this, on_fulfilled, npm]() mutable { npm.resolve(on_fulfilled(result)); }; } @@ -156,15 +156,15 @@ namespace promise { std::is_same::ret_type, promise_t>::value>::type gen_on_rejected(Func on_rejected, const promise_t &npm, function &ret) { - ret = [this, on_rejected, npm]() { + ret = [this, on_rejected, npm]() mutable { on_rejected(reason).then_any( [npm] (pm_any_t result_) { npm.resolve(result_); - return none; + return pm_any_t(none); }, [npm] (pm_any_t reason_) { npm.reject(reason_); - return none; + return pm_any_t(none); }); }; } @@ -174,7 +174,7 @@ namespace promise { std::is_same::ret_type, pm_any_t>::value>::type gen_on_rejected(Func on_rejected, const promise_t &npm, function &ret) { - ret = [this, on_rejected, npm]() { + ret = [this, on_rejected, npm]() mutable { npm.reject(on_rejected(reason)); }; } @@ -367,7 +367,7 @@ namespace promise { using arg_type = typename callback_types::arg_type; using ret_type = typename callback_types::ret_type; return ptr->then( - [on_fulfilled](pm_any_t _result) { + [on_fulfilled](pm_any_t _result) mutable { try { return ret_type(on_fulfilled(std::any_cast(_result))); } catch (std::bad_any_cast e) { PROMISE_ERR_MISMATCH_TYPE; } @@ -382,12 +382,12 @@ namespace promise { using reject_arg_type = typename callback_types::arg_type; using reject_ret_type = typename callback_types::ret_type; return ptr->then( - [on_fulfilled](pm_any_t _result) { + [on_fulfilled](pm_any_t _result) mutable { try { return fulfill_ret_type(on_fulfilled(std::any_cast(_result))); } catch (std::bad_any_cast e) { PROMISE_ERR_MISMATCH_TYPE; } }, - [on_rejected](pm_any_t _reason) { + [on_rejected](pm_any_t _reason) mutable { try { return reject_ret_type(on_rejected(std::any_cast(_reason))); } catch (std::bad_any_cast e) { PROMISE_ERR_MISMATCH_TYPE; } @@ -399,7 +399,7 @@ namespace promise { using arg_type = typename callback_types::arg_type; using ret_type = typename callback_types::ret_type; return ptr->fail( - [on_rejected](pm_any_t _reason) { + [on_rejected](pm_any_t _reason) mutable { try { return ret_type(on_rejected(std::any_cast(_reason))); } catch (std::bad_any_cast e) { PROMISE_ERR_MISMATCH_TYPE; } diff --git a/test.cpp b/test.cpp index df96207..242222d 100644 --- a/test.cpp +++ b/test.cpp @@ -3,9 +3,16 @@ using promise::promise_t; struct A { - promise::None operator()(int y) const { - printf("%d\n", y); - return promise::none; + int operator()(int x) { + printf("%d\n", x); + return 1; + } +}; + +struct B { + promise_t operator()(int x) { + printf("%d\n", x); + return promise_t([](promise_t pm) {pm.resolve(1);}); } }; @@ -14,13 +21,19 @@ int f(int x) { return x + 1; } +promise_t g(int x) { + printf("%d\n", x); + return promise_t([](promise_t pm) {pm.resolve(1);}); +} + int main() { std::function t1; std::function t2; std::function t3; std::function t4; std::function t5; - A a; + A a1, a2, a3; + B b1, b2, b3; auto pm = promise_t([&t1](promise_t pm) { puts("pm1"); //t1 = [pm]() {pm.reject(5);}; @@ -43,7 +56,7 @@ int main() { }).then([](int x) { printf("%d\n", x); return 12; - }).then(f).then(a); + }).then(f).then(a1).fail(a2).then(b1).fail(b2).then(g).then(a3, b3); auto p1 = promise_t([&t4](promise_t pm) { puts("p1"); -- cgit v1.2.3-70-g09d2