Home | History | Annotate | Download | only in base
      1 // 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_
      7 
      8 #include <utility>
      9 
     10 #include "base/bind.h"
     11 #include "base/bind_helpers.h"
     12 #include "base/callback.h"
     13 #include "base/logging.h"
     14 #include "base/post_task_and_reply_with_result_internal.h"
     15 #include "base/task_runner.h"
     16 
     17 namespace base {
     18 
     19 // When you have these methods
     20 //
     21 //   R DoWorkAndReturn();
     22 //   void Callback(const R& result);
     23 //
     24 // and want to call them in a PostTaskAndReply kind of fashion where the
     25 // result of DoWorkAndReturn is passed to the Callback, you can use
     26 // PostTaskAndReplyWithResult as in this example:
     27 //
     28 // PostTaskAndReplyWithResult(
     29 //     target_thread_.task_runner(),
     30 //     FROM_HERE,
     31 //     Bind(&DoWorkAndReturn),
     32 //     Bind(&Callback));
     33 template <typename TaskReturnType, typename ReplyArgType>
     34 bool PostTaskAndReplyWithResult(TaskRunner* task_runner,
     35                                 const tracked_objects::Location& from_here,
     36                                 Callback<TaskReturnType()> task,
     37                                 Callback<void(ReplyArgType)> reply) {
     38   DCHECK(task);
     39   DCHECK(reply);
     40   TaskReturnType* result = new TaskReturnType();
     41   return task_runner->PostTaskAndReply(
     42       from_here, base::Bind(&internal::ReturnAsParamAdapter<TaskReturnType>,
     43                             std::move(task), result),
     44       base::Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
     45                  std::move(reply), base::Owned(result)));
     46 }
     47 
     48 }  // namespace base
     49 
     50 #endif  // BASE_TASK_RUNNER_UTIL_H_
     51