From 030234c7a6c7f6c5a901dab68591c9cfb2d4828f Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Sat, 3 Sep 2016 13:28:55 -0700 Subject: [core] Rework invokeWithCallback so that the callback is last --- include/mbgl/util/run_loop.hpp | 6 +++--- include/mbgl/util/work_task.hpp | 6 +++--- include/mbgl/util/work_task_impl.hpp | 24 +++++++++++++++++++----- 3 files changed, 25 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp index af1053dfdc..6559b72ef8 100644 --- a/include/mbgl/util/run_loop.hpp +++ b/include/mbgl/util/run_loop.hpp @@ -61,10 +61,10 @@ public: } // Invoke fn(args...) on this RunLoop, then invoke callback(results...) on the current RunLoop. - template + template std::unique_ptr - invokeWithCallback(Fn&& fn, Cb&& callback, Args&&... args) { - std::shared_ptr task = WorkTask::makeWithCallback(std::forward(fn), std::forward(callback), std::forward(args)...); + invokeWithCallback(Fn&& fn, Args&&... args) { + std::shared_ptr task = WorkTask::makeWithCallback(std::forward(fn), std::forward(args)...); push(task); return std::make_unique(task); } diff --git a/include/mbgl/util/work_task.hpp b/include/mbgl/util/work_task.hpp index e9c2062d9c..dda8e5d00f 100644 --- a/include/mbgl/util/work_task.hpp +++ b/include/mbgl/util/work_task.hpp @@ -17,10 +17,10 @@ public: virtual void cancel() = 0; template - static std::shared_ptr make(Fn&& fn, Args&&... args); + static std::shared_ptr make(Fn&&, Args&&...); - template - static std::shared_ptr makeWithCallback(Fn&& fn, Cb&& callback, Args&&... args); + template + static std::shared_ptr makeWithCallback(Fn&&, Args&&...); }; } // namespace mbgl diff --git a/include/mbgl/util/work_task_impl.hpp b/include/mbgl/util/work_task_impl.hpp index 2c016601bb..a2f55e00c5 100644 --- a/include/mbgl/util/work_task_impl.hpp +++ b/include/mbgl/util/work_task_impl.hpp @@ -62,10 +62,12 @@ std::shared_ptr WorkTask::make(Fn&& fn, Args&&... args) { flag); } -template -std::shared_ptr WorkTask::makeWithCallback(Fn&& fn, Cb&& callback, Args&&... args) { - auto flag = std::make_shared>(); - *flag = false; +namespace detail { +template +auto packageArgumentsAndCallback(std::shared_ptr> flag, + Tuple&& args, + std::index_sequence) { + auto callback = std::get(args); // Create a lambda L1 that invokes another lambda L2 on the current RunLoop R, that calls // the callback C. Both lambdas check the flag before proceeding. L1 needs to check the flag @@ -82,7 +84,19 @@ std::shared_ptr WorkTask::makeWithCallback(Fn&& fn, Cb&& callback, Arg } }; - auto tuple = std::make_tuple(std::move(args)..., after); + return std::make_tuple(std::move(std::get(args))..., after); +} +} // namespace detail + +template +std::shared_ptr WorkTask::makeWithCallback(Fn&& fn, Args&&... args) { + auto flag = std::make_shared>(); + *flag = false; + + auto tuple = detail::packageArgumentsAndCallback(flag, + std::forward_as_tuple(std::forward(args)...), + std::make_index_sequence()); + return std::make_shared>( std::move(fn), std::move(tuple), -- cgit v1.2.1