Index: base/task_runner.h |
diff --git a/base/task_runner.h b/base/task_runner.h |
index 894eb878f603d5fa5fe7030704374b5899533221..61b7aaee491fb90dfcfc07b52d7dea83e09f78de 100644 |
--- a/base/task_runner.h |
+++ b/base/task_runner.h |
@@ -8,7 +8,10 @@ |
#include "base/base_export.h" |
#include "base/basictypes.h" |
-#include "base/callback_forward.h" |
+#include "base/bind.h" |
willchan no longer on Chromium
2012/04/04 17:35:49
I have a problem with all these new headers. I see
battre
2012/04/04 19:47:41
Either way is fine with me. I'll follow your decis
willchan no longer on Chromium
2012/04/05 16:53:03
darin/akalin, feel free to chime in, but otherwise
battre
2012/04/12 06:46:33
Done.
|
+#include "base/bind_helpers.h" |
+#include "base/callback.h" |
+#include "base/logging.h" |
#include "base/memory/ref_counted.h" |
#include "base/time.h" |
@@ -18,6 +21,41 @@ class Location; |
namespace base { |
+namespace internal { |
+ |
+// Helper class for TaskRunner::PostTaskAndReplyWithResult. |
+template <typename ReturnType> |
+void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func, |
+ ReturnType* result) { |
+ if (!func.is_null()) |
+ *result = func.Run(); |
+} |
+ |
+// Helper class for TaskRunner::PostTaskAndReplyWithResult. |
+template <typename ReturnType> |
+Closure ReturnAsParam(const Callback<ReturnType(void)>& func, |
+ ReturnType* result) { |
+ DCHECK(result); |
+ return Bind(&ReturnAsParamAdapter<ReturnType>, func, result); |
+} |
+ |
+// Helper class for TaskRunner::PostTaskAndReplyWithResult. |
+template <typename ReturnType> |
+void ReplyAdapter(const Callback<void(ReturnType)>& callback, |
+ ReturnType* result) { |
+ DCHECK(result); |
+ if(!callback.is_null()) |
+ callback.Run(*result); |
+} |
+ |
+// Helper class for TaskRunner::PostTaskAndReplyWithResult. |
+template <typename ReturnType, typename OWNED> |
willchan no longer on Chromium
2012/04/04 17:35:49
s/OWNED/OwnedType/
battre
2012/04/12 06:46:33
Done.
|
+Closure ReplyHelper(const Callback<void(ReturnType)>& callback, OWNED result) { |
+ return Bind(&ReplyAdapter<ReturnType>, callback, result); |
+} |
+ |
+} // namespace internal |
+ |
struct TaskRunnerTraits; |
// A TaskRunner is an object that runs posted tasks (in the form of |
@@ -134,6 +172,31 @@ class BASE_EXPORT TaskRunner |
const Closure& task, |
const Closure& reply); |
+ // When you have these methods |
+ // |
+ // R DoWorkAndReturn(); |
+ // void Callback(const R& result); |
+ // |
+ // and want to call them in a PostTaskAndReply kind of fashion where the |
+ // result of DoWorkAndReturn is passed to the Callback, you can use |
+ // PostTaskAndReplyWithResult as in this example: |
+ // |
+ // target_thread_.message_loop_proxy()->PostTaskAndReplyWithResult( |
+ // FROM_HERE, |
+ // Bind(&DoWorkAndReturn), |
+ // Bind(&Callback)); |
+ template <typename ReturnType> |
+ bool PostTaskAndReplyWithResult( |
+ const tracked_objects::Location& from_here, |
+ const Callback<ReturnType(void)>& task, |
+ const Callback<void(ReturnType)>& reply) { |
+ ReturnType* result = new ReturnType; |
+ return PostTaskAndReply( |
+ from_here, |
+ internal::ReturnAsParam<ReturnType>(task, result), |
+ internal::ReplyHelper(reply, Owned(result))); |
+ } |
+ |
protected: |
friend struct TaskRunnerTraits; |