| 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 | |
| 55 // Holds the Callback methods that don't require specialization to reduce | 32 // Holds the Callback methods that don't require specialization to reduce |
| 56 // template bloat. | 33 // template bloat. |
| 57 class BASE_EXPORT CallbackBase { | 34 class BASE_EXPORT CallbackBase { |
| 58 public: | 35 public: |
| 59 // Returns true if Callback is null (doesn't refer to anything). | 36 // Returns true if Callback is null (doesn't refer to anything). |
| 60 bool is_null() const; | 37 bool is_null() const; |
| 61 | 38 |
| 62 // Returns the Callback into an uninitialized state. | 39 // Returns the Callback into an uninitialized state. |
| 63 void Reset(); | 40 void Reset(); |
| 64 | 41 |
| 65 protected: | 42 protected: |
| 66 // In C++, it is safe to cast function pointers to function pointers of | 43 // In C++, it is safe to cast function pointers to function pointers of |
| 67 // another type. It is not okay to use void*. We create a InvokeFuncStorage | 44 // another type. It is not okay to use void*. We create a InvokeFuncStorage |
| 68 // that that can store our function pointer, and then cast it back to | 45 // that that can store our function pointer, and then cast it back to |
| 69 // the original type on usage. | 46 // the original type on usage. |
| 70 typedef void(*InvokeFuncStorage)(void); | 47 typedef void(*InvokeFuncStorage)(void); |
| 71 | 48 |
| 72 // Returns true if this callback equals |other|. |other| may be null. | 49 // Returns true if this callback equals |other|. |other| may be null. |
| 73 bool Equals(const CallbackBase& other) const; | 50 bool Equals(const CallbackBase& other) const; |
| 74 | 51 |
| 75 CallbackBase(InvokeFuncStorage polymorphic_invoke, | 52 // Allow initializing of |bind_state_| via the constructor to avoid default |
| 76 scoped_refptr<BindStateBase>* bind_state); | 53 // initialization of the scoped_refptr. We do not also initialize |
| 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); |
| 77 | 57 |
| 78 // Force the destructor to be instantiated inside this translation unit so | 58 // Force the destructor to be instantiated inside this translation unit so |
| 79 // that our subclasses will not get inlined versions. Avoids more template | 59 // that our subclasses will not get inlined versions. Avoids more template |
| 80 // bloat. | 60 // bloat. |
| 81 ~CallbackBase(); | 61 ~CallbackBase(); |
| 82 | 62 |
| 83 scoped_refptr<BindStateBase> bind_state_; | 63 scoped_refptr<BindStateBase> bind_state_; |
| 84 InvokeFuncStorage polymorphic_invoke_; | 64 InvokeFuncStorage polymorphic_invoke_; |
| 85 }; | 65 }; |
| 86 | 66 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 template <typename T> | 107 template <typename T> |
| 128 struct CallbackParamTraits<T[]> { | 108 struct CallbackParamTraits<T[]> { |
| 129 typedef const T* ForwardType; | 109 typedef const T* ForwardType; |
| 130 typedef const T* StorageType; | 110 typedef const T* StorageType; |
| 131 }; | 111 }; |
| 132 | 112 |
| 133 } // namespace internal | 113 } // namespace internal |
| 134 } // namespace base | 114 } // namespace base |
| 135 | 115 |
| 136 #endif // BASE_CALLBACK_INTERNAL_H_ | 116 #endif // BASE_CALLBACK_INTERNAL_H_ |
| OLD | NEW |