From 42b76a0e722237567720fea53ba2a14b8d81faf8 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 1 Feb 2018 01:22:16 -0500 Subject: ... --- promise.hpp | 48 ++++++++++++++++++++++++++++++++---------------- test.cpp | 17 +++++++++++++++-- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/promise.hpp b/promise.hpp index b9f5cfc..4f1ea06 100644 --- a/promise.hpp +++ b/promise.hpp @@ -6,17 +6,6 @@ #include #include -template -struct function_traits: - public function_traits {}; - -template -struct function_traits -{ - using ret_type = ReturnType; - using arg_type = ArgType; -}; - /** Implement type-safe Promise primitives similar to the ones specified by * Javascript A+. */ namespace promise { @@ -27,6 +16,33 @@ namespace promise { const auto none = nullptr; const auto do_nothing = [](){}; + /* match lambdas */ + template + struct function_traits: + public function_traits {}; + + /* match plain functions */ + template + struct function_traits { + using ret_type = ReturnType; + using arg_type = ArgType; + }; + + /* match function pointers */ + template + struct function_traits: + public function_traits {}; + + /* match const member functions */ + template + struct function_traits: + public function_traits {}; + + /* match member functions */ + template + struct function_traits: + public function_traits {}; + class Promise; class promise_t { std::shared_ptr ptr; @@ -285,11 +301,11 @@ namespace promise { }; template promise_t all(PList promise_list) { - auto size = std::make_shared(promise_list.size()); - auto results = std::make_shared(); - if (!size) PROMISE_ERR_MISMATCH_TYPE; - results->resize(*size); - return promise_t([=] (promise_t npm) { + return promise_t([promise_list] (promise_t npm) { + auto size = std::make_shared(promise_list.size()); + auto results = std::make_shared(); + if (!size) PROMISE_ERR_MISMATCH_TYPE; + results->resize(*size); size_t idx = 0; for (const auto &pm: promise_list) { pm.then_any( diff --git a/test.cpp b/test.cpp index 2719340..df96207 100644 --- a/test.cpp +++ b/test.cpp @@ -2,12 +2,25 @@ #include "promise.hpp" using promise::promise_t; +struct A { + promise::None operator()(int y) const { + printf("%d\n", y); + return promise::none; + } +}; + +int f(int x) { + printf("%d\n", x); + return x + 1; +} + int main() { std::function t1; std::function t2; std::function t3; std::function t4; std::function t5; + A a; auto pm = promise_t([&t1](promise_t pm) { puts("pm1"); //t1 = [pm]() {pm.reject(5);}; @@ -29,8 +42,8 @@ int main() { return 10; }).then([](int x) { printf("%d\n", x); - return promise::none; - }); + return 12; + }).then(f).then(a); auto p1 = promise_t([&t4](promise_t pm) { puts("p1"); -- cgit v1.2.3-70-g09d2