blob: ba8e120c6f4393496130d9bef69a275b28533752 [file] [log] [blame]
[email protected]95991b12012-04-17 02:48:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_TASK_RUNNER_UTIL_H_
6#define BASE_TASK_RUNNER_UTIL_H_
[email protected]95991b12012-04-17 02:48:067
8#include "base/bind.h"
9#include "base/bind_helpers.h"
10#include "base/logging.h"
11#include "base/task_runner.h"
12
13namespace base {
14
15namespace internal {
16
[email protected]895906812012-11-28 03:29:0117// Adapts a function that produces a result via a return value to
18// one that returns via an output parameter.
[email protected]95991b12012-04-17 02:48:0619template <typename ReturnType>
20void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func,
21 ReturnType* result) {
[email protected]895906812012-11-28 03:29:0122 *result = func.Run();
[email protected]95991b12012-04-17 02:48:0623}
24
[email protected]895906812012-11-28 03:29:0125// Adapts a T* result to a callblack that expects a T.
26template <typename TaskReturnType, typename ReplyArgType>
27void ReplyAdapter(const Callback<void(ReplyArgType)>& callback,
28 TaskReturnType* result) {
29 // TODO(ajwong): Remove this conditional and add a DCHECK to enforce that
30 // |reply| must be non-null in PostTaskAndReplyWithResult() below after
31 // current code that relies on this API softness has been removed.
32 // http://crbug.com/162712
33 if (!callback.is_null())
tzikd2046ce32016-04-14 21:44:3934 callback.Run(std::move(*result));
[email protected]95991b12012-04-17 02:48:0635}
36
[email protected]95991b12012-04-17 02:48:0637} // namespace internal
38
39// When you have these methods
40//
41// R DoWorkAndReturn();
42// void Callback(const R& result);
43//
44// and want to call them in a PostTaskAndReply kind of fashion where the
45// result of DoWorkAndReturn is passed to the Callback, you can use
46// PostTaskAndReplyWithResult as in this example:
47//
48// PostTaskAndReplyWithResult(
skyostilb1f02992015-06-19 17:22:5449// target_thread_.task_runner(),
[email protected]95991b12012-04-17 02:48:0650// FROM_HERE,
51// Bind(&DoWorkAndReturn),
52// Bind(&Callback));
[email protected]895906812012-11-28 03:29:0153template <typename TaskReturnType, typename ReplyArgType>
[email protected]95991b12012-04-17 02:48:0654bool PostTaskAndReplyWithResult(
55 TaskRunner* task_runner,
56 const tracked_objects::Location& from_here,
[email protected]895906812012-11-28 03:29:0157 const Callback<TaskReturnType(void)>& task,
58 const Callback<void(ReplyArgType)>& reply) {
59 TaskReturnType* result = new TaskReturnType();
[email protected]95991b12012-04-17 02:48:0660 return task_runner->PostTaskAndReply(
61 from_here,
[email protected]895906812012-11-28 03:29:0162 base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task,
63 result),
64 base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply,
65 base::Owned(result)));
[email protected]95991b12012-04-17 02:48:0666}
67
68} // namespace base
69
70#endif // BASE_TASK_RUNNER_UTIL_H_