diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-09-03 13:28:55 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-09-08 16:36:47 -0700 |
commit | 030234c7a6c7f6c5a901dab68591c9cfb2d4828f (patch) | |
tree | 669e831aecdd9cd74173cb93088b13979887e81e /include/mbgl/util | |
parent | e4995e8d3a38dfd2ed64337e712ecf701af37454 (diff) | |
download | qtlocation-mapboxgl-030234c7a6c7f6c5a901dab68591c9cfb2d4828f.tar.gz |
[core] Rework invokeWithCallback so that the callback is last
Diffstat (limited to 'include/mbgl/util')
-rw-r--r-- | include/mbgl/util/run_loop.hpp | 6 | ||||
-rw-r--r-- | include/mbgl/util/work_task.hpp | 6 | ||||
-rw-r--r-- | include/mbgl/util/work_task_impl.hpp | 24 |
3 files changed, 25 insertions, 11 deletions
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 <class Fn, class Cb, class... Args> + template <class Fn, class... Args> std::unique_ptr<AsyncRequest> - invokeWithCallback(Fn&& fn, Cb&& callback, Args&&... args) { - std::shared_ptr<WorkTask> task = WorkTask::makeWithCallback(std::forward<Fn>(fn), std::forward<Cb>(callback), std::forward<Args>(args)...); + invokeWithCallback(Fn&& fn, Args&&... args) { + std::shared_ptr<WorkTask> task = WorkTask::makeWithCallback(std::forward<Fn>(fn), std::forward<Args>(args)...); push(task); return std::make_unique<WorkRequest>(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 <class Fn, class... Args> - static std::shared_ptr<WorkTask> make(Fn&& fn, Args&&... args); + static std::shared_ptr<WorkTask> make(Fn&&, Args&&...); - template <class Fn, class Cb, class... Args> - static std::shared_ptr<WorkTask> makeWithCallback(Fn&& fn, Cb&& callback, Args&&... args); + template <class Fn, class... Args> + static std::shared_ptr<WorkTask> 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> WorkTask::make(Fn&& fn, Args&&... args) { flag); } -template <class Fn, class Cb, class... Args> -std::shared_ptr<WorkTask> WorkTask::makeWithCallback(Fn&& fn, Cb&& callback, Args&&... args) { - auto flag = std::make_shared<std::atomic<bool>>(); - *flag = false; +namespace detail { +template <class Tuple, size_t... Indexes> +auto packageArgumentsAndCallback(std::shared_ptr<std::atomic<bool>> flag, + Tuple&& args, + std::index_sequence<Indexes...>) { + auto callback = std::get<sizeof...(Indexes)>(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> WorkTask::makeWithCallback(Fn&& fn, Cb&& callback, Arg } }; - auto tuple = std::make_tuple(std::move(args)..., after); + return std::make_tuple(std::move(std::get<Indexes>(args))..., after); +} +} // namespace detail + +template <class Fn, class... Args> +std::shared_ptr<WorkTask> WorkTask::makeWithCallback(Fn&& fn, Args&&... args) { + auto flag = std::make_shared<std::atomic<bool>>(); + *flag = false; + + auto tuple = detail::packageArgumentsAndCallback(flag, + std::forward_as_tuple(std::forward<Args>(args)...), + std::make_index_sequence<sizeof...(Args) - 1>()); + return std::make_shared<WorkTaskImpl<Fn, decltype(tuple)>>( std::move(fn), std::move(tuple), |