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.
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.
10 #ifndef BASE_CALLBACK_HELPERS_H_
11 #define BASE_CALLBACK_HELPERS_H_
15 #include "base/atomicops.h"
16 #include "base/bind.h"
17 #include "base/callback.h"
18 #include "base/compiler_specific.h"
19 #include "base/macros.h"
20 #include "base/memory/ptr_util.h"
24 // Prefer std::move() over ResetAndReturn().
25 template <typename CallbackType>
26 CallbackType ResetAndReturn(CallbackType* cb) {
27 CallbackType ret(std::move(*cb));
34 template <typename... Args>
35 class AdaptCallbackForRepeatingHelper final {
37 explicit AdaptCallbackForRepeatingHelper(OnceCallback<void(Args...)> callback)
38 : callback_(std::move(callback)) {
42 void Run(Args... args) {
43 if (subtle::NoBarrier_AtomicExchange(&has_run_, 1))
46 std::move(callback_).Run(std::forward<Args>(args)...);
50 volatile subtle::Atomic32 has_run_ = 0;
51 base::OnceCallback<void(Args...)> callback_;
53 DISALLOW_COPY_AND_ASSIGN(AdaptCallbackForRepeatingHelper);
56 } // namespace internal
58 // Wraps the given OnceCallback into a RepeatingCallback that relays its
59 // invocation to the original OnceCallback on the first invocation. The
60 // following invocations are just ignored.
62 // Note that this deliberately subverts the Once/Repeating paradigm of Callbacks
63 // but helps ease the migration from old-style Callbacks. Avoid if possible; use
64 // if necessary for migration. TODO(tzik): Remove it. https://crbug.com/730593
65 template <typename... Args>
66 RepeatingCallback<void(Args...)> AdaptCallbackForRepeating(
67 OnceCallback<void(Args...)> callback) {
68 using Helper = internal::AdaptCallbackForRepeatingHelper<Args...>;
69 return base::BindRepeating(&Helper::Run,
70 std::make_unique<Helper>(std::move(callback)));
73 // ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures
74 // that the Closure is executed no matter how the current scope exits.
75 class BASE_EXPORT ScopedClosureRunner {
77 ScopedClosureRunner();
78 explicit ScopedClosureRunner(OnceClosure closure);
79 ~ScopedClosureRunner();
81 ScopedClosureRunner(ScopedClosureRunner&& other);
83 // Releases the current closure if it's set and replaces it with the closure
85 ScopedClosureRunner& operator=(ScopedClosureRunner&& other);
87 // Calls the current closure and resets it, so it wont be called again.
90 // Replaces closure with the new one releasing the old one without calling it.
91 void ReplaceClosure(OnceClosure closure);
93 // Releases the Closure without calling.
94 OnceClosure Release() WARN_UNUSED_RESULT;
99 DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner);
104 #endif // BASE_CALLBACK_HELPERS_H_