OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 10 #pragma once |
(...skipping 12 matching lines...) Expand all Loading... |
23 // behaves as an existential type that is used by a corresponding | 23 // behaves as an existential type that is used by a corresponding |
24 // DoInvoke function to perform the function execution. This allows | 24 // DoInvoke function to perform the function execution. This allows |
25 // us to shield the Callback class from the types of the bound argument via | 25 // us to shield the Callback class from the types of the bound argument via |
26 // "type erasure." | 26 // "type erasure." |
27 class BindStateBase : public RefCountedThreadSafe<BindStateBase> { | 27 class BindStateBase : public RefCountedThreadSafe<BindStateBase> { |
28 protected: | 28 protected: |
29 friend class RefCountedThreadSafe<BindStateBase>; | 29 friend class RefCountedThreadSafe<BindStateBase>; |
30 virtual ~BindStateBase() {} | 30 virtual ~BindStateBase() {} |
31 }; | 31 }; |
32 | 32 |
33 // This structure exists purely to pass the returned |bind_state_| from | |
34 // Bind() to Callback while avoiding an extra AddRef/Release() pair. | |
35 // | |
36 // To do this, the constructor of Callback<> must take a const-ref. The | |
37 // reference must be to a const object otherwise the compiler will emit a | |
38 // warning about taking a reference to a temporary. | |
39 // | |
40 // Unfortunately, this means that the internal |bind_state_| field must | |
41 // be made mutable. | |
42 template <typename T> | |
43 struct BindStateHolder { | |
44 explicit BindStateHolder(T* bind_state) | |
45 : bind_state_(bind_state) { | |
46 } | |
47 | |
48 mutable scoped_refptr<BindStateBase> bind_state_; | |
49 }; | |
50 | |
51 template <typename T> | |
52 BindStateHolder<T> MakeBindStateHolder(T* o) { | |
53 return BindStateHolder<T>(o); | |
54 } | |
55 | |
56 // Holds the Callback methods that don't require specialization to reduce | 33 // Holds the Callback methods that don't require specialization to reduce |
57 // template bloat. | 34 // template bloat. |
58 class BASE_EXPORT CallbackBase { | 35 class BASE_EXPORT CallbackBase { |
59 public: | 36 public: |
60 // Returns true if Callback is null (doesn't refer to anything). | 37 // Returns true if Callback is null (doesn't refer to anything). |
61 bool is_null() const; | 38 bool is_null() const; |
62 | 39 |
63 // Returns the Callback into an uninitialized state. | 40 // Returns the Callback into an uninitialized state. |
64 void Reset(); | 41 void Reset(); |
65 | 42 |
66 protected: | 43 protected: |
67 // In C++, it is safe to cast function pointers to function pointers of | 44 // In C++, it is safe to cast function pointers to function pointers of |
68 // another type. It is not okay to use void*. We create a InvokeFuncStorage | 45 // another type. It is not okay to use void*. We create a InvokeFuncStorage |
69 // that that can store our function pointer, and then cast it back to | 46 // that that can store our function pointer, and then cast it back to |
70 // the original type on usage. | 47 // the original type on usage. |
71 typedef void(*InvokeFuncStorage)(void); | 48 typedef void(*InvokeFuncStorage)(void); |
72 | 49 |
73 // Returns true if this callback equals |other|. |other| may be null. | 50 // Returns true if this callback equals |other|. |other| may be null. |
74 bool Equals(const CallbackBase& other) const; | 51 bool Equals(const CallbackBase& other) const; |
75 | 52 |
76 CallbackBase(InvokeFuncStorage polymorphic_invoke, | 53 // Allow initializing of |bind_state_| via the constructor to avoid default |
77 scoped_refptr<BindStateBase>* bind_state); | 54 // initialization of the scoped_refptr. We do not also initialize |
| 55 // |polymorphic_invoke_| here because doing a normal assignment in the |
| 56 // derived Callback templates makes for much nicer compiler errors. |
| 57 explicit CallbackBase(BindStateBase* bind_state); |
78 | 58 |
79 // Force the destructor to be instantiated inside this translation unit so | 59 // Force the destructor to be instantiated inside this translation unit so |
80 // that our subclasses will not get inlined versions. Avoids more template | 60 // that our subclasses will not get inlined versions. Avoids more template |
81 // bloat. | 61 // bloat. |
82 ~CallbackBase(); | 62 ~CallbackBase(); |
83 | 63 |
84 scoped_refptr<BindStateBase> bind_state_; | 64 scoped_refptr<BindStateBase> bind_state_; |
85 InvokeFuncStorage polymorphic_invoke_; | 65 InvokeFuncStorage polymorphic_invoke_; |
86 }; | 66 }; |
87 | 67 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 template <typename T> | 166 template <typename T> |
187 scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); } | 167 scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); } |
188 | 168 |
189 template <typename T> | 169 template <typename T> |
190 scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); } | 170 scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); } |
191 | 171 |
192 } // namespace internal | 172 } // namespace internal |
193 } // namespace base | 173 } // namespace base |
194 | 174 |
195 #endif // BASE_CALLBACK_INTERNAL_H_ | 175 #endif // BASE_CALLBACK_INTERNAL_H_ |
OLD | NEW |