blob: 1db35ee0c2203ce70e773b1c0cfcc4e5f2db26bd [file] [log] [blame]
[email protected]1192339c2012-03-24 20:37:271// 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// This defines helpful methods for dealing with Callbacks. Because Callbacks
6// are implemented using templates, with a class per callback signature, adding
7// methods to Callback<> itself is unattractive (lots of extra code gets
8// generated). Instead, consider adding methods here.
[email protected]1192339c2012-03-24 20:37:279
10#ifndef BASE_CALLBACK_HELPERS_H_
11#define BASE_CALLBACK_HELPERS_H_
12
jdoerrie19cf5212019-04-26 09:50:4713#include <type_traits>
tzikfaa34952017-04-20 12:19:5014#include <utility>
15
16#include "base/atomicops.h"
17#include "base/bind.h"
[email protected]1192339c2012-03-24 20:37:2718#include "base/callback.h"
[email protected]1c232c22013-08-30 02:04:0419#include "base/compiler_specific.h"
avi9b6f42932015-12-26 22:15:1420#include "base/macros.h"
tzikfaa34952017-04-20 12:19:5021#include "base/memory/ptr_util.h"
[email protected]1192339c2012-03-24 20:37:2722
23namespace base {
24
jdoerrie19cf5212019-04-26 09:50:4725namespace internal {
Daniel Chengbbd47292019-04-25 02:20:1526
jdoerrie19cf5212019-04-26 09:50:4727template <typename T>
28struct IsBaseCallbackImpl : std::false_type {};
29
30template <typename R, typename... Args>
31struct IsBaseCallbackImpl<OnceCallback<R(Args...)>> : std::true_type {};
32
33template <typename R, typename... Args>
34struct IsBaseCallbackImpl<RepeatingCallback<R(Args...)>> : std::true_type {};
35
36} // namespace internal
37
38template <typename T>
39using IsBaseCallback = internal::IsBaseCallbackImpl<std::decay_t<T>>;
40
41// SFINAE friendly enabler allowing to overload methods for both Repeating and
42// OnceCallbacks.
43//
44// Usage:
45// template <template <typename> class CallbackType,
46// ... other template args ...,
47// typename = EnableIfIsBaseCallback<CallbackType>>
48// void DoStuff(CallbackType<...> cb, ...);
49template <template <typename> class CallbackType>
50using EnableIfIsBaseCallback =
51 std::enable_if_t<IsBaseCallback<CallbackType<void()>>::value>;
52
tzikfaa34952017-04-20 12:19:5053namespace internal {
54
55template <typename... Args>
56class AdaptCallbackForRepeatingHelper final {
57 public:
58 explicit AdaptCallbackForRepeatingHelper(OnceCallback<void(Args...)> callback)
59 : callback_(std::move(callback)) {
60 DCHECK(callback_);
61 }
62
63 void Run(Args... args) {
64 if (subtle::NoBarrier_AtomicExchange(&has_run_, 1))
65 return;
66 DCHECK(callback_);
67 std::move(callback_).Run(std::forward<Args>(args)...);
68 }
69
70 private:
71 volatile subtle::Atomic32 has_run_ = 0;
72 base::OnceCallback<void(Args...)> callback_;
73
74 DISALLOW_COPY_AND_ASSIGN(AdaptCallbackForRepeatingHelper);
75};
76
77} // namespace internal
78
79// Wraps the given OnceCallback into a RepeatingCallback that relays its
80// invocation to the original OnceCallback on the first invocation. The
81// following invocations are just ignored.
Avi Drissmanaed82232018-03-20 13:36:5782//
83// Note that this deliberately subverts the Once/Repeating paradigm of Callbacks
84// but helps ease the migration from old-style Callbacks. Avoid if possible; use
85// if necessary for migration. TODO(tzik): Remove it. https://crbug.com/730593
tzikfaa34952017-04-20 12:19:5086template <typename... Args>
87RepeatingCallback<void(Args...)> AdaptCallbackForRepeating(
88 OnceCallback<void(Args...)> callback) {
89 using Helper = internal::AdaptCallbackForRepeatingHelper<Args...>;
90 return base::BindRepeating(&Helper::Run,
Jeremy Roman9532f252017-08-16 23:27:2491 std::make_unique<Helper>(std::move(callback)));
tzikfaa34952017-04-20 12:19:5092}
93
sergeyue4be1912016-06-25 00:51:0994// ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures
95// that the Closure is executed no matter how the current scope exits.
[email protected]1c232c22013-08-30 02:04:0496class BASE_EXPORT ScopedClosureRunner {
97 public:
98 ScopedClosureRunner();
tzik398065c2017-08-08 05:19:4099 explicit ScopedClosureRunner(OnceClosure closure);
[email protected]1c232c22013-08-30 02:04:04100 ~ScopedClosureRunner();
101
sergeyue4be1912016-06-25 00:51:09102 ScopedClosureRunner(ScopedClosureRunner&& other);
103
sergeyu668613aa2016-07-08 00:34:27104 // Releases the current closure if it's set and replaces it with the closure
105 // from |other|.
sergeyue4be1912016-06-25 00:51:09106 ScopedClosureRunner& operator=(ScopedClosureRunner&& other);
107
108 // Calls the current closure and resets it, so it wont be called again.
sergeyu668613aa2016-07-08 00:34:27109 void RunAndReset();
sergeyue4be1912016-06-25 00:51:09110
sergeyu668613aa2016-07-08 00:34:27111 // Replaces closure with the new one releasing the old one without calling it.
tzik398065c2017-08-08 05:19:40112 void ReplaceClosure(OnceClosure closure);
sergeyue4be1912016-06-25 00:51:09113
114 // Releases the Closure without calling.
tzik398065c2017-08-08 05:19:40115 OnceClosure Release() WARN_UNUSED_RESULT;
[email protected]1c232c22013-08-30 02:04:04116
117 private:
tzik398065c2017-08-08 05:19:40118 OnceClosure closure_;
[email protected]1c232c22013-08-30 02:04:04119
120 DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner);
121};
122
[email protected]1192339c2012-03-24 20:37:27123} // namespace base
124
125#endif // BASE_CALLBACK_HELPERS_H_