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