callback_helper.h.pump revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
1$$ This is a pump file for generating file templates.  Pump is a python
2$$ script that is part of the Google Test suite of utilities.  Description
3$$ can be found here:
4$$
5$$ http://code.google.com/p/googletest/wiki/PumpManual
6$$
7$$ See comment for MAX_ARITY in base/bind.h.pump.
8$var MAX_ARITY = 7
9
10// Copyright 2014 The Chromium Authors. All rights reserved.
11// Use of this source code is governed by a BSD-style license that can be
12// found in the LICENSE file.
13
14#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_
15#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_
16
17#include "base/bind.h"
18#include "base/location.h"
19#include "base/logging.h"
20#include "base/thread_task_runner_handle.h"
21
22// TODO(tzik): Merge this file to media/base/bind_to_current_loop.h.
23
24namespace sync_file_system {
25namespace drive_backend {
26
27namespace internal {
28
29template <typename T>
30typename base::enable_if<
31    base::internal::IsMoveOnlyType<T>::value,
32    base::internal::PassedWrapper<T> >::type
33RebindForward(T& t) {
34  return base::Passed(&t);
35}
36
37template <typename T>
38typename base::enable_if<
39    !base::internal::IsMoveOnlyType<T>::value,
40    T&>::type
41RebindForward(T& t) {
42  return t;
43}
44
45template <typename>
46struct RelayToTaskRunnerHelper;
47
48$range ARITY 0..MAX_ARITY
49$for ARITY [[
50$range ARG 1..ARITY
51
52template <$for ARG , [[typename A$(ARG)]]>
53struct RelayToTaskRunnerHelper<void($for ARG , [[A$(ARG)]])> {
54  static void Run(base::TaskRunner* task_runner,
55                  const tracked_objects::Location& from_here,
56                  const base::Callback<void($for ARG , [[A$(ARG)]])>& callback
57$if ARITY != 0 [[, ]]
58$for ARG , [[A$(ARG) a$(ARG)]]
59) {
60    task_runner->PostTask(from_here, base::Bind(callback
61$if ARITY != 0 [[, ]]
62$for ARG , [[RebindForward(a$(ARG))]]));
63  }
64};
65
66]]  $$ for ARITY
67
68}  // namespace internal
69
70template <typename T>
71base::Callback<T> RelayCallbackToTaskRunner(
72    base::TaskRunner* task_runner,
73    const tracked_objects::Location& from_here,
74    const base::Callback<T>& callback) {
75  DCHECK(task_runner->RunsTasksOnCurrentThread());
76  return base::Bind(&internal::RelayToTaskRunnerHelper<T>::Run,
77                    make_scoped_refptr(task_runner), from_here,
78                    callback);
79}
80
81template <typename T>
82base::Callback<T> RelayCallbackToCurrentThread(
83    const tracked_objects::Location& from_here,
84    const base::Callback<T>& callback) {
85  return RelayCallbackToTaskRunner(
86      base::ThreadTaskRunnerHandle::Get(),
87      from_here, callback);
88}
89
90}  // namespace drive_backend
91}  // namespace sync_file_system
92
93#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_
94