123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #ifndef BASE_CALLBACK_HELPERS_H_
- #define BASE_CALLBACK_HELPERS_H_
- #include <memory>
- #include <type_traits>
- #include <utility>
- #include "base/atomicops.h"
- #include "base/bind.h"
- #include "base/callback.h"
- #include "base/compiler_specific.h"
- #include "base/memory/ptr_util.h"
- namespace base {
- namespace internal {
- template <typename T>
- struct IsBaseCallbackImpl : std::false_type {};
- template <typename R, typename... Args>
- struct IsBaseCallbackImpl<OnceCallback<R(Args...)>> : std::true_type {};
- template <typename R, typename... Args>
- struct IsBaseCallbackImpl<RepeatingCallback<R(Args...)>> : std::true_type {};
- template <typename T>
- struct IsOnceCallbackImpl : std::false_type {};
- template <typename R, typename... Args>
- struct IsOnceCallbackImpl<OnceCallback<R(Args...)>> : std::true_type {};
- }
- template <typename T>
- using IsBaseCallback = internal::IsBaseCallbackImpl<std::decay_t<T>>;
- template <typename T>
- using IsOnceCallback = internal::IsOnceCallbackImpl<std::decay_t<T>>;
- template <template <typename> class CallbackType>
- using EnableIfIsBaseCallback =
- std::enable_if_t<IsBaseCallback<CallbackType<void()>>::value>;
- namespace internal {
- template <typename... Args>
- class AdaptCallbackForRepeatingHelper final {
- public:
- explicit AdaptCallbackForRepeatingHelper(OnceCallback<void(Args...)> callback)
- : callback_(std::move(callback)) {
- DCHECK(callback_);
- }
- AdaptCallbackForRepeatingHelper(const AdaptCallbackForRepeatingHelper&) =
- delete;
- AdaptCallbackForRepeatingHelper& operator=(
- const AdaptCallbackForRepeatingHelper&) = delete;
- void Run(Args... args) {
- if (subtle::NoBarrier_AtomicExchange(&has_run_, 1))
- return;
- DCHECK(callback_);
- std::move(callback_).Run(std::forward<Args>(args)...);
- }
- private:
- volatile subtle::Atomic32 has_run_ = 0;
- base::OnceCallback<void(Args...)> callback_;
- };
- }
- template <typename... Args>
- RepeatingCallback<void(Args...)> AdaptCallbackForRepeating(
- OnceCallback<void(Args...)> callback) {
- using Helper = internal::AdaptCallbackForRepeatingHelper<Args...>;
- return base::BindRepeating(&Helper::Run,
- std::make_unique<Helper>(std::move(callback)));
- }
- class BASE_EXPORT ScopedClosureRunner {
- public:
- ScopedClosureRunner();
- explicit ScopedClosureRunner(OnceClosure closure);
- ScopedClosureRunner(const ScopedClosureRunner&) = delete;
- ScopedClosureRunner& operator=(const ScopedClosureRunner&) = delete;
- ~ScopedClosureRunner();
- ScopedClosureRunner(ScopedClosureRunner&& other);
-
-
- ScopedClosureRunner& operator=(ScopedClosureRunner&& other);
-
- void RunAndReset();
-
- void ReplaceClosure(OnceClosure closure);
-
- OnceClosure Release() WARN_UNUSED_RESULT;
- private:
- OnceClosure closure_;
- };
- }
- #endif
|