PROTON-1482: [C++ binding] Added convenience overload to proton::defer for scheduled deferred work - Used new conveniences in scheduled_send_03 example
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/1e2efdb0 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/1e2efdb0 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/1e2efdb0 Branch: refs/heads/master Commit: 1e2efdb01c8ebf863626150c9c14eae600c4739c Parents: 88c2d7d Author: Andrew Stitcher <[email protected]> Authored: Fri May 19 01:38:18 2017 -0400 Committer: Andrew Stitcher <[email protected]> Committed: Fri Jul 21 12:50:06 2017 -0400 ---------------------------------------------------------------------- examples/cpp/scheduled_send_03.cpp | 50 +++----------------- .../bindings/cpp/include/proton/work_queue.hpp | 31 ++++++++++++ 2 files changed, 37 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e2efdb0/examples/cpp/scheduled_send_03.cpp ---------------------------------------------------------------------- diff --git a/examples/cpp/scheduled_send_03.cpp b/examples/cpp/scheduled_send_03.cpp index c3c63c8..8ac46a1 100644 --- a/examples/cpp/scheduled_send_03.cpp +++ b/examples/cpp/scheduled_send_03.cpp @@ -46,49 +46,13 @@ class scheduled_sender : public proton::messaging_handler { proton::work_queue *work_queue; bool ready, canceled; - struct cancel_fn : public proton::void_function0 { - scheduled_sender* parent; - proton::sender sender; - cancel_fn(): parent(0) {} - cancel_fn(scheduled_sender& ss, proton::sender& s) : parent(&ss), sender(s) {} - void operator()() { if (parent) parent->cancel(sender); } - }; - - struct tick_fn : public proton::void_function0 { - scheduled_sender* parent; - proton::sender sender; - tick_fn(): parent(0) {} - tick_fn(scheduled_sender& ss, proton::sender& s) : parent(&ss), sender(s) {} - void operator()() { if (parent) parent->tick(sender); } - }; - - struct defer_cancel_fn : public proton::void_function0 { - scheduled_sender& parent; - defer_cancel_fn(scheduled_sender& ss) : parent(ss) {} - void operator()() { parent.work_queue->add(parent.do_cancel); } - }; - - struct defer_tick_fn : public proton::void_function0 { - scheduled_sender& parent; - defer_tick_fn(scheduled_sender& ss) : parent(ss) {} - void operator()() { parent.work_queue->add(parent.do_tick); } - }; - - tick_fn do_tick; - cancel_fn do_cancel; - defer_tick_fn defer_tick; - defer_cancel_fn defer_cancel; - public: - scheduled_sender(const std::string &s, double d, double t) : url(s), interval(int(d*proton::duration::SECOND.milliseconds())), // Send interval. timeout(int(t*proton::duration::SECOND.milliseconds())), // Cancel after timeout. ready(true), // Ready to send. - canceled(false), // Canceled. - defer_tick(*this), - defer_cancel(*this) + canceled(false) // Canceled. {} void on_container_start(proton::container &c) OVERRIDE { @@ -98,20 +62,18 @@ class scheduled_sender : public proton::messaging_handler { void on_sender_open(proton::sender & s) OVERRIDE { work_queue = &s.work_queue(); - do_cancel = cancel_fn(*this, s); - do_tick = tick_fn(*this, s); - s.container().schedule(timeout, defer_cancel); // Call this->cancel after timeout. - s.container().schedule(interval, defer_tick); // Start regular ticks every interval. + proton::defer(work_queue, timeout, &scheduled_sender::cancel, this, s); + proton::defer(work_queue, interval, &scheduled_sender::tick, this, s); } - void cancel(proton::sender& sender) { + void cancel(proton::sender sender) { canceled = true; sender.connection().close(); } - void tick(proton::sender& sender) { + void tick(proton::sender sender) { if (!canceled) { - sender.container().schedule(interval, defer_tick); // Next tick + proton::defer(work_queue, interval, &scheduled_sender::tick, this, sender); // Next tick if (sender.credit() > 0) // Only send if we have credit send(sender); else http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/1e2efdb0/proton-c/bindings/cpp/include/proton/work_queue.hpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/include/proton/work_queue.hpp b/proton-c/bindings/cpp/include/proton/work_queue.hpp index bef041c..61567b8 100644 --- a/proton-c/bindings/cpp/include/proton/work_queue.hpp +++ b/proton-c/bindings/cpp/include/proton/work_queue.hpp @@ -22,6 +22,7 @@ * */ +#include "./duration.hpp" #include "./fwd.hpp" #include "./function.hpp" #include "./internal/config.hpp" @@ -317,6 +318,31 @@ bool defer(WQ wq, F f, A a, B b, C c, D d) { return defer_helper(wq, make_work(f, a, b, c, d)); } +template <class WQ, class F> +void defer(WQ wq, duration dn, F f) { + wq->schedule(dn, make_work(f)); +} + +template <class WQ, class F, class A> +void defer(WQ wq, duration dn, F f, A a) { + wq->schedule(dn, make_work(f, a)); +} + +template <class WQ, class F, class A, class B> +void defer(WQ wq, duration dn, F f, A a, B b) { + wq->schedule(dn, make_work(f, a, b)); +} + +template <class WQ, class F, class A, class B, class C> +void defer(WQ wq, duration dn, F f, A a, B b, C c) { + wq->schedule(dn, make_work(f, a, b, c)); +} + +template <class WQ, class F, class A, class B, class C, class D> +void defer(WQ wq, duration dn, F f, A a, B b, C c, D d) { + wq->schedule(dn, make_work(f, a, b, c, d)); +} + /// This version of proton::defer defers calling a free function to an arbitrary work queue #else // The C++11 version is *much* simpler and even so more general! @@ -326,6 +352,11 @@ bool defer(WQ wq, Rest&&... r) { return wq->add(std::bind(std::forward<Rest>(r)...)); } +template <class WQ, class... Rest> +void defer(WQ wq, duration d, Rest&&... r) { + wq->schedule(d, std::bind(std::forward<Rest>(r)...)); +} + template <class... Rest> work make_work(Rest&&... r) { return std::bind(std::forward<Rest>(r)...); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
