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.
4
5 // This file contains utility functions and classes that help the
6 // implementation, and management of the Callback objects.
7
8 #ifndef BASE_CALLBACK_INTERNAL_H_
9 #define BASE_CALLBACK_INTERNAL_H_
10
11 #include "base/base_export.h"
12 #include "base/callback_forward.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15
16 namespace base {
17
18 struct FakeBindState;
19
20 namespace internal {
21
22 class BindStateBase;
23 class FinallyExecutorCommon;
24 class ThenAndCatchExecutorCommon;
25
26 template <typename ReturnType>
27 class PostTaskExecutor;
28
29 template <typename Functor, typename... BoundArgs>
30 struct BindState;
31
32 class CallbackBase;
33 class CallbackBaseCopyable;
34
35 struct BindStateBaseRefCountTraits {
36 static void Destruct(const BindStateBase*);
37 };
38
39 template <typename T>
40 using PassingType = std::conditional_t<std::is_scalar<T>::value, T, T&&>;
41
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
47 // "type erasure."
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> {
54 public:
55 REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
56
57 enum CancellationQueryMode {
58 IS_CANCELLED,
59 MAYBE_VALID,
60 };
61
62 using InvokeFuncStorage = void(*)();
63
64 private:
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));
71
72 ~BindStateBase() = default;
73
74 friend struct BindStateBaseRefCountTraits;
75 friend class RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits>;
76
77 friend class CallbackBase;
78 friend class CallbackBaseCopyable;
79
80 // Whitelist subclasses that access the destructor of BindStateBase.
81 template <typename Functor, typename... BoundArgs>
82 friend struct BindState;
83 friend struct ::base::FakeBindState;
84
IsCancelled()85 bool IsCancelled() const {
86 return query_cancellation_traits_(this, IS_CANCELLED);
87 }
88
MaybeValid()89 bool MaybeValid() const {
90 return query_cancellation_traits_(this, MAYBE_VALID);
91 }
92
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_;
98
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);
103
104 DISALLOW_COPY_AND_ASSIGN(BindStateBase);
105 };
106
107 // Holds the Callback methods that don't require specialization to reduce
108 // template bloat.
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 {
112 public:
113 inline CallbackBase(CallbackBase&& c) noexcept;
114 CallbackBase& operator=(CallbackBase&& c) noexcept;
115
116 explicit CallbackBase(const CallbackBaseCopyable& c);
117 CallbackBase& operator=(const CallbackBaseCopyable& c);
118
119 explicit CallbackBase(CallbackBaseCopyable&& c) noexcept;
120 CallbackBase& operator=(CallbackBaseCopyable&& c) noexcept;
121
122 // Returns true if Callback is null (doesn't refer to anything).
is_null()123 bool is_null() const { return !bind_state_; }
124 explicit operator bool() const { return !is_null(); }
125
126 // Returns true if the callback invocation will be nop due to an cancellation.
127 // It's invalid to call this on uninitialized callback.
128 //
129 // Must be called on the Callback's destination sequence.
130 bool IsCancelled() const;
131
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.
134 //
135 // This function is thread-safe.
136 bool MaybeValid() const;
137
138 // Returns the Callback into an uninitialized state.
139 void Reset();
140
141 protected:
142 friend class FinallyExecutorCommon;
143 friend class ThenAndCatchExecutorCommon;
144
145 template <typename ReturnType>
146 friend class PostTaskExecutor;
147
148 using InvokeFuncStorage = BindStateBase::InvokeFuncStorage;
149
150 // Returns true if this callback equals |other|. |other| may be null.
151 bool EqualsInternal(const CallbackBase& other) const;
152
153 constexpr inline CallbackBase();
154
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);
158
polymorphic_invoke()159 InvokeFuncStorage polymorphic_invoke() const {
160 return bind_state_->polymorphic_invoke_;
161 }
162
163 // Force the destructor to be instantiated inside this translation unit so
164 // that our subclasses will not get inlined versions. Avoids more template
165 // bloat.
166 ~CallbackBase();
167
168 scoped_refptr<BindStateBase> bind_state_;
169 };
170
171 constexpr CallbackBase::CallbackBase() = default;
172 CallbackBase::CallbackBase(CallbackBase&&) noexcept = default;
CallbackBase(BindStateBase * bind_state)173 CallbackBase::CallbackBase(BindStateBase* bind_state)
174 : bind_state_(AdoptRef(bind_state)) {}
175
176 // CallbackBase<Copyable> is a direct base class of Copyable Callbacks.
177 class BASE_EXPORT CallbackBaseCopyable : public CallbackBase {
178 public:
179 CallbackBaseCopyable(const CallbackBaseCopyable& c);
180 CallbackBaseCopyable(CallbackBaseCopyable&& c) noexcept = default;
181 CallbackBaseCopyable& operator=(const CallbackBaseCopyable& c);
182 CallbackBaseCopyable& operator=(CallbackBaseCopyable&& c) noexcept;
183
184 protected:
185 constexpr CallbackBaseCopyable() = default;
CallbackBaseCopyable(BindStateBase * bind_state)186 explicit CallbackBaseCopyable(BindStateBase* bind_state)
187 : CallbackBase(bind_state) {}
188 ~CallbackBaseCopyable() = default;
189 };
190
191 } // namespace internal
192 } // namespace base
193
194 #endif // BASE_CALLBACK_INTERNAL_H_
195