| 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. |
| 9 |
| 10 | #ifndef BASE_CALLBACK_HELPERS_H_ |
| 11 | #define BASE_CALLBACK_HELPERS_H_ |
| 12 |
| 13 | #include <type_traits> |
| 14 | #include <utility> |
| 15 |
| 16 | #include "base/atomicops.h" |
| 17 | #include "base/bind.h" |
| 18 | #include "base/callback.h" |
| 19 | #include "base/compiler_specific.h" |
| 20 | #include "base/macros.h" |
| 21 | #include "base/memory/ptr_util.h" |
| 22 |
| 23 | namespace base { |
| 24 |
| 25 | namespace internal { |
| 26 |
| 27 | template <typename T> |
| 28 | struct IsBaseCallbackImpl : std::false_type {}; |
| 29 |
| 30 | template <typename R, typename... Args> |
| 31 | struct IsBaseCallbackImpl<OnceCallback<R(Args...)>> : std::true_type {}; |
| 32 |
| 33 | template <typename R, typename... Args> |
| 34 | struct IsBaseCallbackImpl<RepeatingCallback<R(Args...)>> : std::true_type {}; |
| 35 |
| 36 | } // namespace internal |
| 37 |
| 38 | template <typename T> |
| 39 | using 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, ...); |
| 49 | template <template <typename> class CallbackType> |
| 50 | using EnableIfIsBaseCallback = |
| 51 | std::enable_if_t<IsBaseCallback<CallbackType<void()>>::value>; |
| 52 |
| 53 | namespace internal { |
| 54 |
| 55 | template <typename... Args> |
| 56 | class 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. |
| 82 | // |
| 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 |
| 86 | template <typename... Args> |
| 87 | RepeatingCallback<void(Args...)> AdaptCallbackForRepeating( |
| 88 | OnceCallback<void(Args...)> callback) { |
| 89 | using Helper = internal::AdaptCallbackForRepeatingHelper<Args...>; |
| 90 | return base::BindRepeating(&Helper::Run, |
| 91 | std::make_unique<Helper>(std::move(callback))); |
| 92 | } |
| 93 |
| 94 | // ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures |
| 95 | // that the Closure is executed no matter how the current scope exits. |
| 96 | // If you are looking for "ScopedCallback", "CallbackRunner", or |
| 97 | // "CallbackScoper" this is the class you want. |
| 98 | class BASE_EXPORT ScopedClosureRunner { |
| 99 | public: |
| 100 | ScopedClosureRunner(); |
| 101 | explicit ScopedClosureRunner(OnceClosure closure); |
| 102 | ~ScopedClosureRunner(); |
| 103 |
| 104 | ScopedClosureRunner(ScopedClosureRunner&& other); |
| 105 |
| 106 | // Releases the current closure if it's set and replaces it with the closure |
| 107 | // from |other|. |
| 108 | ScopedClosureRunner& operator=(ScopedClosureRunner&& other); |
| 109 |
| 110 | // Calls the current closure and resets it, so it wont be called again. |
| 111 | void RunAndReset(); |
| 112 |
| 113 | // Replaces closure with the new one releasing the old one without calling it. |
| 114 | void ReplaceClosure(OnceClosure closure); |
| 115 |
| 116 | // Releases the Closure without calling. |
| 117 | OnceClosure Release() WARN_UNUSED_RESULT; |
| 118 |
| 119 | private: |
| 120 | OnceClosure closure_; |
| 121 |
| 122 | DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner); |
| 123 | }; |
| 124 |
| 125 | } // namespace base |
| 126 |
| 127 | #endif // BASE_CALLBACK_HELPERS_H_ |
| 128 |