aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--promise.hpp48
-rw-r--r--test.cpp17
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 <any>
#include <functional>
-template <typename T>
-struct function_traits:
- public function_traits<decltype(&T::operator())> {};
-
-template <typename ClassType, typename ReturnType, typename ArgType>
-struct function_traits<ReturnType(ClassType::*)(ArgType) const>
-{
- 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<typename T>
+ struct function_traits:
+ public function_traits<decltype(&T::operator())> {};
+
+ /* match plain functions */
+ template<typename ReturnType, typename ArgType>
+ struct function_traits<ReturnType(ArgType)> {
+ using ret_type = ReturnType;
+ using arg_type = ArgType;
+ };
+
+ /* match function pointers */
+ template<typename ReturnType, typename ArgType>
+ struct function_traits<ReturnType(*)(ArgType)>:
+ public function_traits<ReturnType(ArgType)> {};
+
+ /* match const member functions */
+ template<typename ClassType, typename ReturnType, typename ArgType>
+ struct function_traits<ReturnType(ClassType::*)(ArgType) const>:
+ public function_traits<ReturnType(ArgType)> {};
+
+ /* match member functions */
+ template<typename ClassType, typename ReturnType, typename ArgType>
+ struct function_traits<ReturnType(ClassType::*)(ArgType)>:
+ public function_traits<ReturnType(ArgType)> {};
+
class Promise;
class promise_t {
std::shared_ptr<Promise> ptr;
@@ -285,11 +301,11 @@ namespace promise {
};
template<typename PList> promise_t all(PList promise_list) {
- auto size = std::make_shared<size_t>(promise_list.size());
- auto results = std::make_shared<values_t>();
- 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<size_t>(promise_list.size());
+ auto results = std::make_shared<values_t>();
+ 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<void()> t1;
std::function<void()> t2;
std::function<void()> t3;
std::function<void()> t4;
std::function<void()> 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");