From 89165b6ab0c365eb23cb17bc56a788c7dddc1867 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 20 Nov 2018 11:22:05 -0500 Subject: update cppromise header file --- include/hotstuff/promise.hpp | 140 +++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 65 deletions(-) (limited to 'include/hotstuff') diff --git a/include/hotstuff/promise.hpp b/include/hotstuff/promise.hpp index 593d5c1..0902801 100644 --- a/include/hotstuff/promise.hpp +++ b/include/hotstuff/promise.hpp @@ -68,11 +68,11 @@ namespace promise { /* match lambdas */ template - struct function_traits: - public function_traits {}; + struct function_traits_impl: + public function_traits_impl {}; template - struct function_traits { + struct function_traits_impl { using ret_type = ReturnType; using arg_type = void; using empty_arg = void; @@ -80,7 +80,7 @@ namespace promise { /* match plain functions */ template - struct function_traits { + struct function_traits_impl { using ret_type = ReturnType; using arg_type = ArgType; using non_empty_arg = void; @@ -88,39 +88,48 @@ namespace promise { /* match function pointers */ template - struct function_traits: - public function_traits {}; + struct function_traits_impl: + public function_traits_impl {}; /* match const member functions */ template - struct function_traits: - public function_traits {}; + struct function_traits_impl: + public function_traits_impl {}; /* match member functions */ template - struct function_traits: - public function_traits {}; + struct function_traits_impl: + public function_traits_impl {}; + + template + struct function_traits: + public function_traits_impl> {}; template - using enable_if_return = typename std::enable_if< + using enable_if_return = typename std::enable_if_t< std::is_same::ret_type, ReturnType>::value>; template - using disable_if_return = typename std::enable_if< + using disable_if_return = typename std::enable_if_t< !std::is_same::ret_type, ReturnType>::value>; template - using enable_if_arg = typename std::enable_if< + using enable_if_arg = typename std::enable_if_t< std::is_same::arg_type, ArgType>::value>; template - using disable_if_arg = typename std::enable_if< + using disable_if_arg = typename std::enable_if_t< !std::is_same::arg_type, ArgType>::value>; + template + using disable_if_same_ref = typename std::enable_if_t< + !std::is_same< + std::remove_cv_t>, U>::value>; + class Promise; //class promise_t: public std::shared_ptr { class promise_t { @@ -133,7 +142,8 @@ namespace promise { inline promise_t(); inline ~promise_t(); - template inline promise_t(Func callback); + template * = nullptr> + inline promise_t(Func &&callback); void swap(promise_t &other) { std::swap(pm, other.pm); @@ -180,14 +190,14 @@ namespace promise { inline void reject() const; template - inline promise_t then(FuncFulfilled on_fulfilled) const; + inline promise_t then(FuncFulfilled &&on_fulfilled) const; template - inline promise_t then(FuncFulfilled on_fulfilled, - FuncRejected on_rejected) const; + inline promise_t then(FuncFulfilled &&on_fulfilled, + FuncRejected &&on_rejected) const; template - inline promise_t fail(FuncRejected on_rejected) const; + inline promise_t fail(FuncRejected &&on_rejected) const; }; #define PROMISE_ERR_INVALID_STATE do {throw std::runtime_error("invalid promise state");} while (0) @@ -225,7 +235,7 @@ namespace promise { template::non_empty_arg * = nullptr> static constexpr auto cps_transform( - Func f, const pm_any_t &result, const promise_t &npm) { + Func &&f, const pm_any_t &result, const promise_t &npm) { return [&result, npm, f = std::forward(f)]() mutable { #ifndef CPPROMISE_USE_STACK_FREE f(result)->then( @@ -249,7 +259,7 @@ namespace promise { template::empty_arg * = nullptr> static constexpr auto cps_transform( - Func f, const pm_any_t &, const promise_t &npm) { + Func &&f, const pm_any_t &, const promise_t &npm) { return [npm, f = std::forward(f)]() mutable { #ifndef CPPROMISE_USE_STACK_FREE f()->then( @@ -271,22 +281,22 @@ namespace promise { } template::type * = nullptr> - constexpr auto gen_on_fulfilled(Func on_fulfilled, const promise_t &npm) { + enable_if_return * = nullptr> + constexpr auto gen_on_fulfilled(Func &&on_fulfilled, const promise_t &npm) { return cps_transform(std::forward(on_fulfilled), this->result, npm); } template::type * = nullptr> - constexpr auto gen_on_rejected(Func on_rejected, const promise_t &npm) { + enable_if_return * = nullptr> + constexpr auto gen_on_rejected(Func &&on_rejected, const promise_t &npm) { return cps_transform(std::forward(on_rejected), this->reason, npm); } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_on_fulfilled(Func on_fulfilled, const promise_t &npm) { + constexpr auto gen_on_fulfilled(Func &&on_fulfilled, const promise_t &npm) { return [this, npm, on_fulfilled = std::forward(on_fulfilled)]() mutable { on_fulfilled(result); @@ -295,9 +305,9 @@ namespace promise { } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::empty_arg * = nullptr> - constexpr auto gen_on_fulfilled(Func on_fulfilled, const promise_t &npm) { + constexpr auto gen_on_fulfilled(Func &&on_fulfilled, const promise_t &npm) { return [on_fulfilled = std::forward(on_fulfilled), npm]() mutable { on_fulfilled(); npm->_resolve(); @@ -305,9 +315,9 @@ namespace promise { } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_on_rejected(Func on_rejected, const promise_t &npm) { + constexpr auto gen_on_rejected(Func &&on_rejected, const promise_t &npm) { return [this, npm, on_rejected = std::forward(on_rejected)]() mutable { on_rejected(reason); @@ -316,9 +326,9 @@ namespace promise { } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::empty_arg * = nullptr> - constexpr auto gen_on_rejected(Func on_rejected, const promise_t &npm) { + constexpr auto gen_on_rejected(Func &&on_rejected, const promise_t &npm) { return [npm, on_rejected = std::forward(on_rejected)]() mutable { on_rejected(); @@ -327,9 +337,9 @@ namespace promise { } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_on_fulfilled(Func on_fulfilled, const promise_t &npm) { + constexpr auto gen_on_fulfilled(Func &&on_fulfilled, const promise_t &npm) { return [this, npm, on_fulfilled = std::forward(on_fulfilled)]() mutable { npm->_resolve(on_fulfilled(result)); @@ -337,27 +347,27 @@ namespace promise { } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::empty_arg * = nullptr> - constexpr auto gen_on_fulfilled(Func on_fulfilled, const promise_t &npm) { + constexpr auto gen_on_fulfilled(Func &&on_fulfilled, const promise_t &npm) { return [npm, on_fulfilled = std::forward(on_fulfilled)]() mutable { npm->_resolve(on_fulfilled()); }; } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_on_rejected(Func on_rejected, const promise_t &npm) { + constexpr auto gen_on_rejected(Func &&on_rejected, const promise_t &npm) { return [this, npm, on_rejected = std::forward(on_rejected)]() mutable { npm->_reject(on_rejected(reason)); }; } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::empty_arg * = nullptr> - constexpr auto gen_on_rejected(Func on_rejected, const promise_t &npm) { + constexpr auto gen_on_rejected(Func &&on_rejected, const promise_t &npm) { return [npm, on_rejected = std::forward(on_rejected)]() mutable { npm->_reject(on_rejected()); }; @@ -477,8 +487,8 @@ namespace promise { ~Promise() {} template - promise_t then(FuncFulfilled on_fulfilled, - FuncRejected on_rejected) { + promise_t then(FuncFulfilled &&on_fulfilled, + FuncRejected &&on_rejected) { switch (state) { case State::Pending: @@ -626,8 +636,8 @@ namespace promise { }); } - template - inline promise_t::promise_t(Func callback): + template *> + inline promise_t::promise_t(Func &&callback): pm(new Promise()), ref_cnt(new size_t(1)) { callback(*this); @@ -664,10 +674,10 @@ namespace promise { }; template::type * = nullptr, - typename enable_if_return::type * = nullptr, + disable_if_arg * = nullptr, + enable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_any_callback(Func f) { + constexpr auto gen_any_callback(Func &&f) { using func_t = callback_types; return [f = std::forward(f)](pm_any_t v) mutable { try { @@ -677,23 +687,23 @@ namespace promise { } template::type * = nullptr, - typename enable_if_return::type * = nullptr, + enable_if_arg * = nullptr, + enable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_any_callback(Func f) { + constexpr auto gen_any_callback(Func &&f) { return [f = std::forward(f)](pm_any_t v) mutable {f(v);}; } template::type * = nullptr, + enable_if_return * = nullptr, typename function_traits::empty_arg * = nullptr> - constexpr auto gen_any_callback(Func f) { return std::forward(f); } + constexpr auto gen_any_callback(Func &&f) { return std::forward(f); } template::type * = nullptr, - typename disable_if_return::type * = nullptr, + enable_if_arg * = nullptr, + disable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_any_callback(Func f) { + constexpr auto gen_any_callback(Func &&f) { using func_t = callback_types; return [f = std::forward(f)](pm_any_t v) mutable { return typename func_t::ret_type(f(v)); @@ -701,10 +711,10 @@ namespace promise { } template::type * = nullptr, - typename disable_if_return::type * = nullptr, + disable_if_arg * = nullptr, + disable_if_return * = nullptr, typename function_traits::non_empty_arg * = nullptr> - constexpr auto gen_any_callback(Func f) { + constexpr auto gen_any_callback(Func &&f) { using func_t = callback_types; return [f = std::forward(f)](pm_any_t v) mutable { try { @@ -715,9 +725,9 @@ namespace promise { } template::type * = nullptr, + disable_if_return * = nullptr, typename function_traits::empty_arg * = nullptr> - constexpr auto gen_any_callback(Func f) { + constexpr auto gen_any_callback(Func &&f) { using func_t = callback_types; return [f = std::forward(f)]() mutable { return typename func_t::ret_type(f()); @@ -725,19 +735,19 @@ namespace promise { } template - inline promise_t promise_t::then(FuncFulfilled on_fulfilled) const { + inline promise_t promise_t::then(FuncFulfilled &&on_fulfilled) const { return (*this)->then(gen_any_callback(std::forward(on_fulfilled))); } template - inline promise_t promise_t::then(FuncFulfilled on_fulfilled, - FuncRejected on_rejected) const { + inline promise_t promise_t::then(FuncFulfilled &&on_fulfilled, + FuncRejected &&on_rejected) const { return (*this)->then(gen_any_callback(std::forward(on_fulfilled)), gen_any_callback(std::forward(on_rejected))); } template - inline promise_t promise_t::fail(FuncRejected on_rejected) const { + inline promise_t promise_t::fail(FuncRejected &&on_rejected) const { return (*this)->fail(gen_any_callback(std::forward(on_rejected))); } } -- cgit v1.2.3-70-g09d2