[go: nahoru, domu]

blob: 05894ab182c444e1dda5a57a44a3fe230428584c [file] [log] [blame]
battre@chromium.org95991b12012-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_
battre@chromium.org95991b12012-04-17 02:48:067
Mikel Astizc076a972018-07-25 13:49:258#include <memory>
tzik03527512017-02-08 12:29:479#include <utility>
10
battre@chromium.org95991b12012-04-17 02:48:0611#include "base/bind.h"
12#include "base/bind_helpers.h"
tzik03527512017-02-08 12:29:4713#include "base/callback.h"
jdoerrie19cf5212019-04-26 09:50:4714#include "base/callback_helpers.h"
battre@chromium.org95991b12012-04-17 02:48:0615#include "base/logging.h"
fdoraye94a9452016-11-30 16:59:2116#include "base/post_task_and_reply_with_result_internal.h"
battre@chromium.org95991b12012-04-17 02:48:0617#include "base/task_runner.h"
18
19namespace base {
20
battre@chromium.org95991b12012-04-17 02:48:0621// When you have these methods
22//
23// R DoWorkAndReturn();
24// void Callback(const R& result);
25//
26// and want to call them in a PostTaskAndReply kind of fashion where the
27// result of DoWorkAndReturn is passed to the Callback, you can use
28// PostTaskAndReplyWithResult as in this example:
29//
30// PostTaskAndReplyWithResult(
skyostilb1f02992015-06-19 17:22:5431// target_thread_.task_runner(),
battre@chromium.org95991b12012-04-17 02:48:0632// FROM_HERE,
tzikd5346b592017-05-02 16:48:1833// BindOnce(&DoWorkAndReturn),
34// BindOnce(&Callback));
jdoerrie19cf5212019-04-26 09:50:4735//
36// Though RepeatingCallback is convertible to OnceCallback, we need a
37// CallbackType template since we can not use template deduction and object
38// conversion at once on the overload resolution.
39// TODO(crbug.com/714018): Update all callers of the RepeatingCallback version
40// to use OnceCallback and remove the CallbackType template.
41template <template <typename> class CallbackType,
42 typename TaskReturnType,
43 typename ReplyArgType,
44 typename = EnableIfIsBaseCallback<CallbackType>>
tzik03527512017-02-08 12:29:4745bool PostTaskAndReplyWithResult(TaskRunner* task_runner,
Brett Wilson8e88b312017-09-12 05:22:1646 const Location& from_here,
jdoerrie19cf5212019-04-26 09:50:4747 CallbackType<TaskReturnType()> task,
48 CallbackType<void(ReplyArgType)> reply) {
fdoray3be0e4b2016-11-30 12:03:2049 DCHECK(task);
50 DCHECK(reply);
Mikel Astizc076a972018-07-25 13:49:2551 // std::unique_ptr used to avoid the need of a default constructor.
52 auto* result = new std::unique_ptr<TaskReturnType>();
battre@chromium.org95991b12012-04-17 02:48:0653 return task_runner->PostTaskAndReply(
tzik92b7a422017-04-11 15:00:4454 from_here,
tzikd5346b592017-05-02 16:48:1855 BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, std::move(task),
56 result),
57 BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
58 std::move(reply), Owned(result)));
59}
60
battre@chromium.org95991b12012-04-17 02:48:0661} // namespace base
62
63#endif // BASE_TASK_RUNNER_UTIL_H_