aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2018-02-01 14:14:32 -0500
committerDeterminant <[email protected]>2018-02-01 14:14:32 -0500
commite609deb93ca365b253efa2173ce83046835a8d84 (patch)
treecd7be132b88b210d05c30bc636cb64e0c81fbaf9
parent42b76a0e722237567720fea53ba2a14b8d81faf8 (diff)
...
-rw-r--r--promise.hpp20
-rw-r--r--test.cpp23
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<typename function_traits<Func>::ret_type,
promise_t>::value>::type
gen_on_fulfilled(Func on_fulfilled, const promise_t &npm, function<void()> &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<typename function_traits<Func>::ret_type,
pm_any_t>::value>::type
gen_on_fulfilled(Func on_fulfilled, const promise_t &npm, function<void()> &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<typename function_traits<Func>::ret_type,
promise_t>::value>::type
gen_on_rejected(Func on_rejected, const promise_t &npm, function<void()> &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<typename function_traits<Func>::ret_type,
pm_any_t>::value>::type
gen_on_rejected(Func on_rejected, const promise_t &npm, function<void()> &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<FuncFulfilled>::arg_type;
using ret_type = typename callback_types<FuncFulfilled>::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<arg_type>(_result)));
} catch (std::bad_any_cast e) { PROMISE_ERR_MISMATCH_TYPE; }
@@ -382,12 +382,12 @@ namespace promise {
using reject_arg_type = typename callback_types<FuncRejected>::arg_type;
using reject_ret_type = typename callback_types<FuncRejected>::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<fulfill_arg_type>(_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<reject_arg_type>(_reason)));
} catch (std::bad_any_cast e) { PROMISE_ERR_MISMATCH_TYPE; }
@@ -399,7 +399,7 @@ namespace promise {
using arg_type = typename callback_types<FuncRejected>::arg_type;
using ret_type = typename callback_types<FuncRejected>::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<arg_type>(_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<void()> t1;
std::function<void()> t2;
std::function<void()> t3;
std::function<void()> t4;
std::function<void()> 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");