Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(827)

Unified Diff: base/bind_to_task_runner.h

Issue 1553593002: Add base::BindToTaskRunner and BindToCurrentThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/base.gyp ('k') | base/bind_to_task_runner_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/bind_to_task_runner.h
diff --git a/base/bind_to_task_runner.h b/base/bind_to_task_runner.h
new file mode 100644
index 0000000000000000000000000000000000000000..cf860dd3e7d24f2e3180a04529e63d256972b6df
--- /dev/null
+++ b/base/bind_to_task_runner.h
@@ -0,0 +1,82 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_BIND_TO_TASK_RUNNER_H_
+#define BASE_BIND_TO_TASK_RUNNER_H_
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/callback.h"
+#include "base/location.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/scoped_vector.h"
+#include "base/task_runner.h"
+#include "base/thread_task_runner_handle.h"
+
+// This is a helper utility for Bind()ing callbacks to a given TaskRunner.
+// The typical use is when |a| (of class |A|) wants to hand a callback such as
+// base::Bind(&A::AMethod, a) to |b|, but needs to ensure that when |b| executes
+// the callback, it does so on a specific TaskRunner (for example, |a|'s current
+// MessageLoop).
+//
+// Typical usage: request to be called back on the current thread:
+// other->StartAsyncProcessAndCallMeBack(
+// base::BindToTaskRunner(my_task_runner_, base::Bind(&MyClass::MyMethod,
+// this)));
+//
+// Note that like base::Bind(), BindToTaskRunner() can't bind non-constant
+// references, and that *unlike* base::Bind(), BindToTaskRunner() makes copies
+// of its arguments, and thus can't be used with arrays. Note that the callback
+// is always posted to the target TaskRunner.
+//
+// As a convenience, you can use base::BindToCurrentThread() to bind to the
+// TaskRunner for the current thread (ie, base::ThreadTaskRunnerHandle::Get()).
+
+namespace base {
+namespace internal {
+
+template <typename T>
+T& TrampolineForward(T& t) {
+ return t;
+}
+
+template <typename T, typename R>
+PassedWrapper<scoped_ptr<T, R>> TrampolineForward(scoped_ptr<T, R>& p) {
+ return Passed(&p);
+}
+
+template <typename T>
+PassedWrapper<ScopedVector<T>> TrampolineForward(ScopedVector<T>& p) {
+ return Passed(&p);
+}
+
+template <typename Sig>
+struct BindToTaskRunnerTrampoline;
+
+template <typename... Args>
+struct BindToTaskRunnerTrampoline<void(Args...)> {
+ static void Run(const scoped_refptr<TaskRunner>& task_runner,
+ const Callback<void(Args...)>& cb,
+ Args... args) {
+ task_runner->PostTask(FROM_HERE, Bind(cb, TrampolineForward(args)...));
+ }
+};
+
+} // namespace internal
+
+template <typename T>
+Callback<T> BindToTaskRunner(const scoped_refptr<TaskRunner>& task_runner,
+ const Callback<T>& cb) {
+ return Bind(&internal::BindToTaskRunnerTrampoline<T>::Run, task_runner, cb);
+}
+
+template <typename T>
+Callback<T> BindToCurrentThread(const Callback<T>& cb) {
+ return BindToTaskRunner(ThreadTaskRunnerHandle::Get(), cb);
+}
+
+} // namespace base
+
+#endif // BASE_BIND_TO_TASK_RUNNER_H_
« no previous file with comments | « base/base.gyp ('k') | base/bind_to_task_runner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698