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 CallbackBase;
23 class CallbackBaseCopyable;
24
25 class BindStateBase;
26
27 template <typename Functor, typename... BoundArgs>
28 struct BindState;
29
30 struct BindStateBaseRefCountTraits {
31 static void Destruct(const BindStateBase*);
32 };
33
34 template <typename T>
35 using PassingType = std::conditional_t<std::is_scalar<T>::value, 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. Don't use
44 // RefCountedThreadSafe since it requires the destructor to be a virtual method.
45 // Creating a vtable for every BindState template instantiation results in a lot
46 // of bloat. Its only task is to call the destructor which can be done with a
47 // function pointer.
48 class BASE_EXPORT BindStateBase
49 : public RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits> {
50 public:
51 REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
52
53 using InvokeFuncStorage = void(*)();
54
55 private:
56 BindStateBase(InvokeFuncStorage polymorphic_invoke,
57 void (*destructor)(const BindStateBase*));
58 BindStateBase(InvokeFuncStorage polymorphic_invoke,
59 void (*destructor)(const BindStateBase*),
60 bool (*is_cancelled)(const BindStateBase*));
61
62 ~BindStateBase() = default;
63
64 friend struct BindStateBaseRefCountTraits;
65 friend class RefCountedThreadSafe<BindStateBase, BindStateBaseRefCountTraits>;
66
67 friend class CallbackBase;
68 friend class CallbackBaseCopyable;
69
70 // Whitelist subclasses that access the destructor of BindStateBase.
71 template <typename Functor, typename... BoundArgs>
72 friend struct BindState;
73 friend struct ::base::FakeBindState;
74
IsCancelled()75 bool IsCancelled() const {
76 return is_cancelled_(this);
77 }
78
79 // In C++, it is safe to cast function pointers to function pointers of
80 // another type. It is not okay to use void*. We create a InvokeFuncStorage
81 // that that can store our function pointer, and then cast it back to
82 // the original type on usage.
83 InvokeFuncStorage polymorphic_invoke_;
84
85 // Pointer to a function that will properly destroy |this|.
86 void (*destructor_)(const BindStateBase*);
87 bool (*is_cancelled_)(const BindStateBase*);
88
89 DISALLOW_COPY_AND_ASSIGN(BindStateBase);
90 };
91
92 // Holds the Callback methods that don't require specialization to reduce
93 // template bloat.
94 // CallbackBase<MoveOnly> is a direct base class of MoveOnly callbacks, and
95 // CallbackBase<Copyable> uses CallbackBase<MoveOnly> for its implementation.
96 class BASE_EXPORT CallbackBase {
97 public:
98 inline CallbackBase(CallbackBase&& c) noexcept;
99 CallbackBase& operator=(CallbackBase&& c) noexcept;
100
101 explicit CallbackBase(const CallbackBaseCopyable& c);
102 CallbackBase& operator=(const CallbackBaseCopyable& c);
103
104 explicit CallbackBase(CallbackBaseCopyable&& c) noexcept;
105 CallbackBase& operator=(CallbackBaseCopyable&& c) noexcept;
106
107 // Returns true if Callback is null (doesn't refer to anything).
is_null()108 bool is_null() const { return !bind_state_; }
109 explicit operator bool() const { return !is_null(); }
110
111 // Returns true if the callback invocation will be nop due to an cancellation.
112 // It's invalid to call this on uninitialized callback.
113 bool IsCancelled() const;
114
115 // Returns the Callback into an uninitialized state.
116 void Reset();
117
118 protected:
119 using InvokeFuncStorage = BindStateBase::InvokeFuncStorage;
120
121 // Returns true if this callback equals |other|. |other| may be null.
122 bool EqualsInternal(const CallbackBase& other) const;
123
124 constexpr inline CallbackBase();
125
126 // Allow initializing of |bind_state_| via the constructor to avoid default
127 // initialization of the scoped_refptr.
128 explicit inline CallbackBase(BindStateBase* bind_state);
129
polymorphic_invoke()130 InvokeFuncStorage polymorphic_invoke() const {
131 return bind_state_->polymorphic_invoke_;
132 }
133
134 // Force the destructor to be instantiated inside this translation unit so
135 // that our subclasses will not get inlined versions. Avoids more template
136 // bloat.
137 ~CallbackBase();
138
139 scoped_refptr<BindStateBase> bind_state_;
140 };
141
142 constexpr CallbackBase::CallbackBase() = default;
143 CallbackBase::CallbackBase(CallbackBase&&) noexcept = default;
CallbackBase(BindStateBase * bind_state)144 CallbackBase::CallbackBase(BindStateBase* bind_state)
145 : bind_state_(AdoptRef(bind_state)) {}
146
147 // CallbackBase<Copyable> is a direct base class of Copyable Callbacks.
148 class BASE_EXPORT CallbackBaseCopyable : public CallbackBase {
149 public:
150 CallbackBaseCopyable(const CallbackBaseCopyable& c);
151 CallbackBaseCopyable(CallbackBaseCopyable&& c) noexcept = default;
152 CallbackBaseCopyable& operator=(const CallbackBaseCopyable& c);
153 CallbackBaseCopyable& operator=(CallbackBaseCopyable&& c) noexcept;
154
155 protected:
156 constexpr CallbackBaseCopyable() = default;
CallbackBaseCopyable(BindStateBase * bind_state)157 explicit CallbackBaseCopyable(BindStateBase* bind_state)
158 : CallbackBase(bind_state) {}
159 ~CallbackBaseCopyable() = default;
160 };
161
162 } // namespace internal
163 } // namespace base
164
165 #endif // BASE_CALLBACK_INTERNAL_H_
166