OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains utility functions and classes that help the | 5 // This file contains utility functions and classes that help the |
6 // implementation, and management of the Callback objects. | 6 // implementation, and management of the Callback objects. |
7 | 7 |
8 #ifndef BASE_CALLBACK_INTERNAL_H_ | 8 #ifndef BASE_CALLBACK_INTERNAL_H_ |
9 #define BASE_CALLBACK_INTERNAL_H_ | 9 #define BASE_CALLBACK_INTERNAL_H_ |
10 | 10 |
11 #include <stddef.h> | 11 #include <stddef.h> |
12 | 12 |
| 13 #include "base/atomic_ref_count.h" |
13 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 15 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/template_util.h" |
16 | 19 |
17 template <typename T> | 20 template <typename T> |
18 class ScopedVector; | 21 class ScopedVector; |
19 | 22 |
20 namespace base { | 23 namespace base { |
21 namespace internal { | 24 namespace internal { |
| 25 class CallbackBase; |
22 | 26 |
23 // BindStateBase is used to provide an opaque handle that the Callback | 27 // BindStateBase is used to provide an opaque handle that the Callback |
24 // class can use to represent a function object with bound arguments. It | 28 // class can use to represent a function object with bound arguments. It |
25 // behaves as an existential type that is used by a corresponding | 29 // behaves as an existential type that is used by a corresponding |
26 // DoInvoke function to perform the function execution. This allows | 30 // DoInvoke function to perform the function execution. This allows |
27 // us to shield the Callback class from the types of the bound argument via | 31 // us to shield the Callback class from the types of the bound argument via |
28 // "type erasure." | 32 // "type erasure." |
29 class BindStateBase : public RefCountedThreadSafe<BindStateBase> { | 33 // At the base level, the only task is to add reference counting data. Don't use |
| 34 // RefCountedThreadSafe since it requires the destructor to be a virtual method. |
| 35 // Creating a vtable for every BindState template instantiation results in a lot |
| 36 // of bloat. Its only task is to call the destructor which can be done with a |
| 37 // function pointer. |
| 38 class BindStateBase { |
30 protected: | 39 protected: |
31 friend class RefCountedThreadSafe<BindStateBase>; | 40 explicit BindStateBase(void (*destructor)(BindStateBase*)) |
32 virtual ~BindStateBase() {} | 41 : ref_count_(0), destructor_(destructor) {} |
| 42 ~BindStateBase() = default; |
| 43 |
| 44 private: |
| 45 friend class scoped_refptr<BindStateBase>; |
| 46 friend class CallbackBase; |
| 47 |
| 48 void AddRef(); |
| 49 void Release(); |
| 50 |
| 51 AtomicRefCount ref_count_; |
| 52 |
| 53 // Pointer to a function that will properly destroy |this|. |
| 54 void (*destructor_)(BindStateBase*); |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(BindStateBase); |
33 }; | 57 }; |
34 | 58 |
35 // Holds the Callback methods that don't require specialization to reduce | 59 // Holds the Callback methods that don't require specialization to reduce |
36 // template bloat. | 60 // template bloat. |
37 class BASE_EXPORT CallbackBase { | 61 class BASE_EXPORT CallbackBase { |
38 public: | 62 public: |
39 CallbackBase(const CallbackBase& c); | 63 CallbackBase(const CallbackBase& c); |
40 CallbackBase& operator=(const CallbackBase& c); | 64 CallbackBase& operator=(const CallbackBase& c); |
41 | 65 |
42 // Returns true if Callback is null (doesn't refer to anything). | 66 // Returns true if Callback is null (doesn't refer to anything). |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 | 225 |
202 template <typename T> | 226 template <typename T> |
203 typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) { | 227 typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) { |
204 return t.Pass(); | 228 return t.Pass(); |
205 } | 229 } |
206 | 230 |
207 } // namespace internal | 231 } // namespace internal |
208 } // namespace base | 232 } // namespace base |
209 | 233 |
210 #endif // BASE_CALLBACK_INTERNAL_H_ | 234 #endif // BASE_CALLBACK_INTERNAL_H_ |
OLD | NEW |