| 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 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // RefCountedThreadSafe since it requires the destructor to be a virtual method. | 29 // RefCountedThreadSafe since it requires the destructor to be a virtual method. |
| 30 // Creating a vtable for every BindState template instantiation results in a lot | 30 // Creating a vtable for every BindState template instantiation results in a lot |
| 31 // of bloat. Its only task is to call the destructor which can be done with a | 31 // of bloat. Its only task is to call the destructor which can be done with a |
| 32 // function pointer. | 32 // function pointer. |
| 33 class BASE_EXPORT BindStateBase { | 33 class BASE_EXPORT BindStateBase { |
| 34 public: | 34 public: |
| 35 using InvokeFuncStorage = void(*)(); | 35 using InvokeFuncStorage = void(*)(); |
| 36 | 36 |
| 37 protected: | 37 protected: |
| 38 BindStateBase(InvokeFuncStorage polymorphic_invoke, | 38 BindStateBase(InvokeFuncStorage polymorphic_invoke, |
| 39 void (*destructor)(BindStateBase*)); | 39 void (*destructor)(BindStateBase*), |
| 40 bool (*is_cancelled)(const BindStateBase*)); |
| 40 ~BindStateBase() = default; | 41 ~BindStateBase() = default; |
| 41 | 42 |
| 42 private: | 43 private: |
| 43 friend class scoped_refptr<BindStateBase>; | 44 friend class scoped_refptr<BindStateBase>; |
| 44 template <CopyMode copy_mode> | 45 template <CopyMode copy_mode> |
| 45 friend class CallbackBase; | 46 friend class CallbackBase; |
| 46 | 47 |
| 48 bool IsCancelled() const { |
| 49 return is_cancelled_(this); |
| 50 } |
| 51 |
| 47 void AddRef(); | 52 void AddRef(); |
| 48 void Release(); | 53 void Release(); |
| 49 | 54 |
| 50 // In C++, it is safe to cast function pointers to function pointers of | 55 // In C++, it is safe to cast function pointers to function pointers of |
| 51 // another type. It is not okay to use void*. We create a InvokeFuncStorage | 56 // another type. It is not okay to use void*. We create a InvokeFuncStorage |
| 52 // that that can store our function pointer, and then cast it back to | 57 // that that can store our function pointer, and then cast it back to |
| 53 // the original type on usage. | 58 // the original type on usage. |
| 54 InvokeFuncStorage polymorphic_invoke_; | 59 InvokeFuncStorage polymorphic_invoke_; |
| 55 | 60 |
| 56 AtomicRefCount ref_count_; | 61 AtomicRefCount ref_count_; |
| 57 | 62 |
| 58 // Pointer to a function that will properly destroy |this|. | 63 // Pointer to a function that will properly destroy |this|. |
| 59 void (*destructor_)(BindStateBase*); | 64 void (*destructor_)(BindStateBase*); |
| 65 bool (*is_cancelled_)(const BindStateBase*); |
| 60 | 66 |
| 61 DISALLOW_COPY_AND_ASSIGN(BindStateBase); | 67 DISALLOW_COPY_AND_ASSIGN(BindStateBase); |
| 62 }; | 68 }; |
| 63 | 69 |
| 64 // Holds the Callback methods that don't require specialization to reduce | 70 // Holds the Callback methods that don't require specialization to reduce |
| 65 // template bloat. | 71 // template bloat. |
| 66 // CallbackBase<MoveOnly> is a direct base class of MoveOnly callbacks, and | 72 // CallbackBase<MoveOnly> is a direct base class of MoveOnly callbacks, and |
| 67 // CallbackBase<Copyable> uses CallbackBase<MoveOnly> for its implementation. | 73 // CallbackBase<Copyable> uses CallbackBase<MoveOnly> for its implementation. |
| 68 template <> | 74 template <> |
| 69 class BASE_EXPORT CallbackBase<CopyMode::MoveOnly> { | 75 class BASE_EXPORT CallbackBase<CopyMode::MoveOnly> { |
| 70 public: | 76 public: |
| 71 CallbackBase(CallbackBase&& c); | 77 CallbackBase(CallbackBase&& c); |
| 72 CallbackBase& operator=(CallbackBase&& c); | 78 CallbackBase& operator=(CallbackBase&& c); |
| 73 | 79 |
| 74 // Returns true if Callback is null (doesn't refer to anything). | 80 // Returns true if Callback is null (doesn't refer to anything). |
| 75 bool is_null() const { return bind_state_.get() == NULL; } | 81 bool is_null() const { return bind_state_.get() == NULL; } |
| 76 explicit operator bool() const { return !is_null(); } | 82 explicit operator bool() const { return !is_null(); } |
| 77 | 83 |
| 84 // Returns true if the callback invocation will be nop due to an cancellation. |
| 85 // It's invalid to call this on uninitialized callback. |
| 86 bool IsCancelled() const; |
| 87 |
| 78 // Returns the Callback into an uninitialized state. | 88 // Returns the Callback into an uninitialized state. |
| 79 void Reset(); | 89 void Reset(); |
| 80 | 90 |
| 81 protected: | 91 protected: |
| 82 using InvokeFuncStorage = BindStateBase::InvokeFuncStorage; | 92 using InvokeFuncStorage = BindStateBase::InvokeFuncStorage; |
| 83 | 93 |
| 84 // Returns true if this callback equals |other|. |other| may be null. | 94 // Returns true if this callback equals |other|. |other| may be null. |
| 85 bool EqualsInternal(const CallbackBase& other) const; | 95 bool EqualsInternal(const CallbackBase& other) const; |
| 86 | 96 |
| 87 // Allow initializing of |bind_state_| via the constructor to avoid default | 97 // Allow initializing of |bind_state_| via the constructor to avoid default |
| (...skipping 27 matching lines...) Expand all Loading... |
| 115 ~CallbackBase() {} | 125 ~CallbackBase() {} |
| 116 }; | 126 }; |
| 117 | 127 |
| 118 extern template class CallbackBase<CopyMode::MoveOnly>; | 128 extern template class CallbackBase<CopyMode::MoveOnly>; |
| 119 extern template class CallbackBase<CopyMode::Copyable>; | 129 extern template class CallbackBase<CopyMode::Copyable>; |
| 120 | 130 |
| 121 } // namespace internal | 131 } // namespace internal |
| 122 } // namespace base | 132 } // namespace base |
| 123 | 133 |
| 124 #endif // BASE_CALLBACK_INTERNAL_H_ | 134 #endif // BASE_CALLBACK_INTERNAL_H_ |
| OLD | NEW |