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 file contains utility functions and classes that help the
6 // implementation, and management of the Callback objects.
8 #ifndef BASE_CALLBACK_INTERNAL_H_
9 #define BASE_CALLBACK_INTERNAL_H_
11 #include "base/base_export.h"
12 #include "base/callback_forward.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
23 class FinallyExecutorCommon;
24 class ThenAndCatchExecutorCommon;
26 template <typename ReturnType>
27 class PostTaskExecutor;
29 template <typename Functor, typename... BoundArgs>
33 class CallbackBaseCopyable;
35 struct BindStateBaseRefCountTraits {
36 static void Destruct(const BindStateBase*);
40 using PassingType = std::conditional_t<std::is_scalar<T>::value, T, T&&>;
42 // BindStateBase is used to provide an opaque handle that the Callback
43 // class can use to represent a function object with bound arguments. It
44 // behaves as an existential type that is used by a corresponding
45 // DoInvoke function to perform the function execution. This allows
46 // us to shield the Callback class from the types of the bound argument via
48 // At the base level, the only task is to add reference counting data. Avoid
49 // using or inheriting any virtual functions. Creating a vtable for every
50 // BindState template instantiation results in a lot of bloat. Its only task is
51 // to call the destructor which can be done with a function pointer.
52 class BASE_EXPORT BindStateBase
53 : public RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits> {
55 REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
57 enum CancellationQueryMode {
62 using InvokeFuncStorage = void(*)();
65 BindStateBase(InvokeFuncStorage polymorphic_invoke,
66 void (*destructor)(const BindStateBase*));
67 BindStateBase(InvokeFuncStorage polymorphic_invoke,
68 void (*destructor)(const BindStateBase*),
69 bool (*query_cancellation_traits)(const BindStateBase*,
70 CancellationQueryMode mode));
72 ~BindStateBase() = default;
74 friend struct BindStateBaseRefCountTraits;
75 friend class RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits>;
77 friend class CallbackBase;
78 friend class CallbackBaseCopyable;
80 // Whitelist subclasses that access the destructor of BindStateBase.
81 template <typename Functor, typename... BoundArgs>
82 friend struct BindState;
83 friend struct ::base::FakeBindState;
85 bool IsCancelled() const {
86 return query_cancellation_traits_(this, IS_CANCELLED);
89 bool MaybeValid() const {
90 return query_cancellation_traits_(this, MAYBE_VALID);
93 // In C++, it is safe to cast function pointers to function pointers of
94 // another type. It is not okay to use void*. We create a InvokeFuncStorage
95 // that that can store our function pointer, and then cast it back to
96 // the original type on usage.
97 InvokeFuncStorage polymorphic_invoke_;
99 // Pointer to a function that will properly destroy |this|.
100 void (*destructor_)(const BindStateBase*);
101 bool (*query_cancellation_traits_)(const BindStateBase*,
102 CancellationQueryMode mode);
104 DISALLOW_COPY_AND_ASSIGN(BindStateBase);
107 // Holds the Callback methods that don't require specialization to reduce
109 // CallbackBase<MoveOnly> is a direct base class of MoveOnly callbacks, and
110 // CallbackBase<Copyable> uses CallbackBase<MoveOnly> for its implementation.
111 class BASE_EXPORT CallbackBase {
113 inline CallbackBase(CallbackBase&& c) noexcept;
114 CallbackBase& operator=(CallbackBase&& c) noexcept;
116 explicit CallbackBase(const CallbackBaseCopyable& c);
117 CallbackBase& operator=(const CallbackBaseCopyable& c);
119 explicit CallbackBase(CallbackBaseCopyable&& c) noexcept;
120 CallbackBase& operator=(CallbackBaseCopyable&& c) noexcept;
122 // Returns true if Callback is null (doesn't refer to anything).
123 bool is_null() const { return !bind_state_; }
124 explicit operator bool() const { return !is_null(); }
126 // Returns true if the callback invocation will be nop due to an cancellation.
127 // It's invalid to call this on uninitialized callback.
129 // Must be called on the Callback's destination sequence.
130 bool IsCancelled() const;
132 // If this returns false, the callback invocation will be a nop due to a
133 // cancellation. This may(!) still return true, even on a cancelled callback.
135 // This function is thread-safe.
136 bool MaybeValid() const;
138 // Returns the Callback into an uninitialized state.
142 friend class FinallyExecutorCommon;
143 friend class ThenAndCatchExecutorCommon;
145 template <typename ReturnType>
146 friend class PostTaskExecutor;
148 using InvokeFuncStorage = BindStateBase::InvokeFuncStorage;
150 // Returns true if this callback equals |other|. |other| may be null.
151 bool EqualsInternal(const CallbackBase& other) const;
153 constexpr inline CallbackBase();
155 // Allow initializing of |bind_state_| via the constructor to avoid default
156 // initialization of the scoped_refptr.
157 explicit inline CallbackBase(BindStateBase* bind_state);
159 InvokeFuncStorage polymorphic_invoke() const {
160 return bind_state_->polymorphic_invoke_;
163 // Force the destructor to be instantiated inside this translation unit so
164 // that our subclasses will not get inlined versions. Avoids more template
168 scoped_refptr<BindStateBase> bind_state_;
171 constexpr CallbackBase::CallbackBase() = default;
172 CallbackBase::CallbackBase(CallbackBase&&) noexcept = default;
173 CallbackBase::CallbackBase(BindStateBase* bind_state)
174 : bind_state_(AdoptRef(bind_state)) {}
176 // CallbackBase<Copyable> is a direct base class of Copyable Callbacks.
177 class BASE_EXPORT CallbackBaseCopyable : public CallbackBase {
179 CallbackBaseCopyable(const CallbackBaseCopyable& c);
180 CallbackBaseCopyable(CallbackBaseCopyable&& c) noexcept = default;
181 CallbackBaseCopyable& operator=(const CallbackBaseCopyable& c);
182 CallbackBaseCopyable& operator=(CallbackBaseCopyable&& c) noexcept;
185 constexpr CallbackBaseCopyable() = default;
186 explicit CallbackBaseCopyable(BindStateBase* bind_state)
187 : CallbackBase(bind_state) {}
188 ~CallbackBaseCopyable() = default;
191 } // namespace internal
194 #endif // BASE_CALLBACK_INTERNAL_H_