• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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_FUNCTIONAL_CALLBACK_INTERNAL_H_
9 #define BASE_FUNCTIONAL_CALLBACK_INTERNAL_H_
10 
11 #include <type_traits>
12 #include <utility>
13 
14 #include "base/base_export.h"
15 #include "base/compiler_specific.h"
16 #include "base/functional/callback_forward.h"
17 #include "base/memory/ref_counted.h"
18 
19 namespace base {
20 
21 struct FakeBindState;
22 
23 namespace internal {
24 
25 class BindStateBase;
26 
27 template <typename Functor, typename... BoundArgs>
28 struct BindState;
29 
30 struct BASE_EXPORT BindStateBaseRefCountTraits {
31   static void Destruct(const BindStateBase*);
32 };
33 
34 template <typename T>
35 using PassingType = std::conditional_t<std::is_scalar_v<T>, T, T&&>;
36 
37 // BindStateBase is used to provide an opaque handle that the Callback
38 // class can use to represent a function object with bound arguments.  It
39 // behaves as an existential type that is used by a corresponding
40 // DoInvoke function to perform the function execution.  This allows
41 // us to shield the Callback class from the types of the bound argument via
42 // "type erasure."
43 // At the base level, the only task is to add reference counting data. Avoid
44 // using or inheriting any virtual functions. Creating a vtable for every
45 // BindState template instantiation results in a lot of bloat. Its only task is
46 // to call the destructor which can be done with a function pointer.
47 class BASE_EXPORT BindStateBase
48     : public RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits> {
49  public:
50   REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
51 
52   enum CancellationQueryMode {
53     IS_CANCELLED,
54     MAYBE_VALID,
55   };
56 
57   using InvokeFuncStorage = void (*)();
58 
59   BindStateBase(const BindStateBase&) = delete;
60   BindStateBase& operator=(const BindStateBase&) = delete;
61 
62  private:
63   BindStateBase(InvokeFuncStorage polymorphic_invoke,
64                 void (*destructor)(const BindStateBase*));
65   BindStateBase(InvokeFuncStorage polymorphic_invoke,
66                 void (*destructor)(const BindStateBase*),
67                 bool (*query_cancellation_traits)(const BindStateBase*,
68                                                   CancellationQueryMode mode));
69 
70   ~BindStateBase() = default;
71 
72   friend struct BindStateBaseRefCountTraits;
73   friend class RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits>;
74 
75   friend class BindStateHolder;
76 
77   // Allowlist subclasses that access the destructor of BindStateBase.
78   template <typename Functor, typename... BoundArgs>
79   friend struct BindState;
80   friend struct ::base::FakeBindState;
81 
IsCancelled()82   bool IsCancelled() const {
83     return query_cancellation_traits_(this, IS_CANCELLED);
84   }
85 
MaybeValid()86   bool MaybeValid() const {
87     return query_cancellation_traits_(this, MAYBE_VALID);
88   }
89 
90   // In C++, it is safe to cast function pointers to function pointers of
91   // another type. It is not okay to use void*. We create a InvokeFuncStorage
92   // that that can store our function pointer, and then cast it back to
93   // the original type on usage.
94   InvokeFuncStorage polymorphic_invoke_;
95 
96   // Pointer to a function that will properly destroy |this|.
97   void (*destructor_)(const BindStateBase*);
98   bool (*query_cancellation_traits_)(const BindStateBase*,
99                                      CancellationQueryMode mode);
100 };
101 
102 // Minimal wrapper around a `scoped_refptr<BindStateBase>`. It allows more
103 // expensive operations (such as ones that destroy `BindStateBase` or manipulate
104 // refcounts) to be defined out-of-line to reduce binary size.
105 class BASE_EXPORT TRIVIAL_ABI BindStateHolder {
106  public:
107   using InvokeFuncStorage = BindStateBase::InvokeFuncStorage;
108 
109   // Used to construct a null callback.
110   inline constexpr BindStateHolder() noexcept;
111 
112   // Used to construct a callback by `base::BindOnce()`/`base::BindRepeating().
113   inline explicit BindStateHolder(BindStateBase* bind_state);
114 
115   // BindStateHolder is always copyable so it can be used by `OnceCallback` and
116   // `RepeatingCallback`. `OnceCallback` restricts copies so a `BindStateHolder`
117   // used with a `OnceCallback will never be copied.
118   BindStateHolder(const BindStateHolder&);
119   BindStateHolder& operator=(const BindStateHolder&);
120 
121   // Subtle: since `this` is marked as TRIVIAL_ABI, the move operations must
122   // leave a moved-from `BindStateHolder` in a trivially destructible state.
123   inline BindStateHolder(BindStateHolder&&) noexcept;
124   BindStateHolder& operator=(BindStateHolder&&) noexcept;
125 
126   ~BindStateHolder();
127 
is_null()128   bool is_null() const { return !bind_state_; }
129   explicit operator bool() const { return !is_null(); }
130 
131   bool IsCancelled() const;
132   bool MaybeValid() const;
133 
134   void Reset();
135 
136   friend bool operator==(const BindStateHolder&,
137                          const BindStateHolder&) = default;
138 
bind_state()139   const scoped_refptr<BindStateBase>& bind_state() const { return bind_state_; }
140 
polymorphic_invoke()141   InvokeFuncStorage polymorphic_invoke() const {
142     return bind_state_->polymorphic_invoke_;
143   }
144 
145  private:
146   scoped_refptr<BindStateBase> bind_state_;
147 };
148 
149 constexpr BindStateHolder::BindStateHolder() noexcept = default;
150 
151 // TODO(dcheng): Try plumbing a scoped_refptr all the way through, since
152 // scoped_refptr is marked as TRIVIAL_ABI.
BindStateHolder(BindStateBase * bind_state)153 BindStateHolder::BindStateHolder(BindStateBase* bind_state)
154     : bind_state_(AdoptRef(bind_state)) {}
155 
156 // Unlike the copy constructor, copy assignment operator, and move assignment
157 // operator, the move constructor is defaulted in the header because it
158 // generates minimal code: move construction does not change any refcounts, nor
159 // does it potentially destroy `BindStateBase`.
160 BindStateHolder::BindStateHolder(BindStateHolder&&) noexcept = default;
161 
162 // Helpers for the `Then()` implementation.
163 template <typename OriginalCallback, typename ThenCallback>
164 struct ThenHelper;
165 
166 // Specialization when original callback returns `void`.
167 template <template <typename> class OriginalCallback,
168           template <typename>
169           class ThenCallback,
170           typename... OriginalArgs,
171           typename ThenR,
172           typename... ThenArgs>
173 struct ThenHelper<OriginalCallback<void(OriginalArgs...)>,
174                   ThenCallback<ThenR(ThenArgs...)>> {
175  private:
176   // For context on this "templated struct with a lambda that asserts" pattern,
177   // see comments in MakeBindStateTypeImpl.
178   template <bool v = sizeof...(ThenArgs) == 0>
179   struct CorrectNumberOfArgs {
180     static constexpr bool value = [] {
181       static_assert(v,
182                     "|then| callback cannot accept parameters if |this| has a "
183                     "void return type.");
184       return v;
185     }();
186   };
187 
188  public:
189   static auto CreateTrampoline() {
190     return [](OriginalCallback<void(OriginalArgs...)> c1,
191               ThenCallback<ThenR(ThenArgs...)> c2,
192               OriginalArgs... c1_args) -> ThenR {
193       if constexpr (CorrectNumberOfArgs<>::value) {
194         std::move(c1).Run(std::forward<OriginalArgs>(c1_args)...);
195         return std::move(c2).Run();
196       }
197     };
198   }
199 };
200 
201 // Specialization when original callback returns a non-void type.
202 template <template <typename> class OriginalCallback,
203           template <typename>
204           class ThenCallback,
205           typename OriginalR,
206           typename... OriginalArgs,
207           typename ThenR,
208           typename... ThenArgs>
209 struct ThenHelper<OriginalCallback<OriginalR(OriginalArgs...)>,
210                   ThenCallback<ThenR(ThenArgs...)>> {
211  private:
212   template <bool v = sizeof...(ThenArgs) == 1>
213   struct CorrectNumberOfArgs {
214     static constexpr bool value = [] {
215       static_assert(
216           v,
217           "|then| callback must accept exactly one parameter if |this| has a "
218           "non-void return type.");
219       return v;
220     }();
221   };
222 
223   template <bool v =
224                 // TODO(dcheng): This should probably check is_convertible as
225                 // well (same with `AssertBindArgsValidity`).
226             std::is_constructible_v<ThenArgs..., OriginalR&&>>
227   struct ArgsAreConvertible {
228     static constexpr bool value = [] {
229       static_assert(v,
230                     "|then| callback's parameter must be constructible from "
231                     "return type of |this|.");
232       return v;
233     }();
234   };
235 
236  public:
237   static auto CreateTrampoline() {
238     return [](OriginalCallback<OriginalR(OriginalArgs...)> c1,
239               ThenCallback<ThenR(ThenArgs...)> c2,
240               OriginalArgs... c1_args) -> ThenR {
241       if constexpr (std::conjunction_v<CorrectNumberOfArgs<>,
242                                        ArgsAreConvertible<>>) {
243         return std::move(c2).Run(
244             std::move(c1).Run(std::forward<OriginalArgs>(c1_args)...));
245       }
246     };
247   }
248 };
249 
250 }  // namespace internal
251 }  // namespace base
252 
253 #endif  // BASE_FUNCTIONAL_CALLBACK_INTERNAL_H_
254