123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #ifndef BASE_CALLBACK_H_
- #define BASE_CALLBACK_H_
- #include <stddef.h>
- #include "base/callback_forward.h"
- #include "base/callback_internal.h"
- #include "base/notreached.h"
- namespace base {
- template <typename R, typename... Args>
- class OnceCallback<R(Args...)> : public internal::CallbackBase {
- public:
- using ResultType = R;
- using RunType = R(Args...);
- using PolymorphicInvoke = R (*)(internal::BindStateBase*,
- internal::PassingType<Args>...);
- constexpr OnceCallback() = default;
- OnceCallback(std::nullptr_t) = delete;
- explicit OnceCallback(internal::BindStateBase* bind_state)
- : internal::CallbackBase(bind_state) {}
- OnceCallback(const OnceCallback&) = delete;
- OnceCallback& operator=(const OnceCallback&) = delete;
- OnceCallback(OnceCallback&&) noexcept = default;
- OnceCallback& operator=(OnceCallback&&) noexcept = default;
- OnceCallback(RepeatingCallback<RunType> other)
- : internal::CallbackBase(std::move(other)) {}
- OnceCallback& operator=(RepeatingCallback<RunType> other) {
- static_cast<internal::CallbackBase&>(*this) = std::move(other);
- return *this;
- }
- R Run(Args... args) const & {
- static_assert(!sizeof(*this),
- "OnceCallback::Run() may only be invoked on a non-const "
- "rvalue, i.e. std::move(callback).Run().");
- NOTREACHED();
- }
- R Run(Args... args) && {
-
-
-
-
- OnceCallback cb = std::move(*this);
- PolymorphicInvoke f =
- reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke());
- return f(cb.bind_state_.get(), std::forward<Args>(args)...);
- }
- };
- template <typename R, typename... Args>
- class RepeatingCallback<R(Args...)> : public internal::CallbackBaseCopyable {
- public:
- using ResultType = R;
- using RunType = R(Args...);
- using PolymorphicInvoke = R (*)(internal::BindStateBase*,
- internal::PassingType<Args>...);
- constexpr RepeatingCallback() = default;
- RepeatingCallback(std::nullptr_t) = delete;
- explicit RepeatingCallback(internal::BindStateBase* bind_state)
- : internal::CallbackBaseCopyable(bind_state) {}
-
- RepeatingCallback(const RepeatingCallback&) = default;
- RepeatingCallback& operator=(const RepeatingCallback&) = default;
- RepeatingCallback(RepeatingCallback&&) noexcept = default;
- RepeatingCallback& operator=(RepeatingCallback&&) noexcept = default;
- bool operator==(const RepeatingCallback& other) const {
- return EqualsInternal(other);
- }
- bool operator!=(const RepeatingCallback& other) const {
- return !operator==(other);
- }
- R Run(Args... args) const & {
- PolymorphicInvoke f =
- reinterpret_cast<PolymorphicInvoke>(this->polymorphic_invoke());
- return f(this->bind_state_.get(), std::forward<Args>(args)...);
- }
- R Run(Args... args) && {
-
-
-
-
- RepeatingCallback cb = std::move(*this);
- PolymorphicInvoke f =
- reinterpret_cast<PolymorphicInvoke>(cb.polymorphic_invoke());
- return f(std::move(cb).bind_state_.get(), std::forward<Args>(args)...);
- }
- };
- }
- #endif
|