OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef BASE_CALLBACK_LIST_H_ | 5 #ifndef BASE_CALLBACK_LIST_H_ |
6 #define BASE_CALLBACK_LIST_H_ | 6 #define BASE_CALLBACK_LIST_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
| 9 #include <memory> |
9 | 10 |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/callback_internal.h" | 12 #include "base/callback_internal.h" |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 | 16 |
17 // OVERVIEW: | 17 // OVERVIEW: |
18 // | 18 // |
19 // A container for a list of callbacks. Unlike a normal STL vector or list, | 19 // A container for a list of callbacks. Unlike a normal STL vector or list, |
20 // this container can be modified during iteration without invalidating the | 20 // this container can be modified during iteration without invalidating the |
21 // iterator. It safely handles the case of a callback removing itself | 21 // iterator. It safely handles the case of a callback removing itself |
22 // or another callback from the list while callbacks are being run. | 22 // or another callback from the list while callbacks are being run. |
23 // | 23 // |
24 // TYPICAL USAGE: | 24 // TYPICAL USAGE: |
25 // | 25 // |
26 // class MyWidget { | 26 // class MyWidget { |
27 // public: | 27 // public: |
28 // ... | 28 // ... |
29 // | 29 // |
30 // typedef base::Callback<void(const Foo&)> OnFooCallback; | 30 // typedef base::Callback<void(const Foo&)> OnFooCallback; |
31 // | 31 // |
32 // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription> | 32 // std::unique_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
33 // RegisterCallback(const OnFooCallback& cb) { | 33 // RegisterCallback(const OnFooCallback& cb) { |
34 // return callback_list_.Add(cb); | 34 // return callback_list_.Add(cb); |
35 // } | 35 // } |
36 // | 36 // |
37 // private: | 37 // private: |
38 // void NotifyFoo(const Foo& foo) { | 38 // void NotifyFoo(const Foo& foo) { |
39 // callback_list_.Notify(foo); | 39 // callback_list_.Notify(foo); |
40 // } | 40 // } |
41 // | 41 // |
42 // base::CallbackList<void(const Foo&)> callback_list_; | 42 // base::CallbackList<void(const Foo&)> callback_list_; |
(...skipping 12 matching lines...) Expand all Loading... |
55 // MyWidgetListener::~MyWidgetListener() { | 55 // MyWidgetListener::~MyWidgetListener() { |
56 // // Subscription gets deleted automatically and will deregister | 56 // // Subscription gets deleted automatically and will deregister |
57 // // the callback in the process. | 57 // // the callback in the process. |
58 // } | 58 // } |
59 // | 59 // |
60 // private: | 60 // private: |
61 // void OnFoo(const Foo& foo) { | 61 // void OnFoo(const Foo& foo) { |
62 // // Do something. | 62 // // Do something. |
63 // } | 63 // } |
64 // | 64 // |
65 // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription> | 65 // std::unique_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
66 // foo_subscription_; | 66 // foo_subscription_; |
67 // | 67 // |
68 // DISALLOW_COPY_AND_ASSIGN(MyWidgetListener); | 68 // DISALLOW_COPY_AND_ASSIGN(MyWidgetListener); |
69 // }; | 69 // }; |
70 | 70 |
71 namespace base { | 71 namespace base { |
72 | 72 |
73 namespace internal { | 73 namespace internal { |
74 | 74 |
75 template <typename CallbackType> | 75 template <typename CallbackType> |
(...skipping 20 matching lines...) Expand all Loading... |
96 private: | 96 private: |
97 CallbackListBase<CallbackType>* list_; | 97 CallbackListBase<CallbackType>* list_; |
98 typename std::list<CallbackType>::iterator iter_; | 98 typename std::list<CallbackType>::iterator iter_; |
99 | 99 |
100 DISALLOW_COPY_AND_ASSIGN(Subscription); | 100 DISALLOW_COPY_AND_ASSIGN(Subscription); |
101 }; | 101 }; |
102 | 102 |
103 // Add a callback to the list. The callback will remain registered until the | 103 // Add a callback to the list. The callback will remain registered until the |
104 // returned Subscription is destroyed, which must occur before the | 104 // returned Subscription is destroyed, which must occur before the |
105 // CallbackList is destroyed. | 105 // CallbackList is destroyed. |
106 scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { | 106 std::unique_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { |
107 DCHECK(!cb.is_null()); | 107 DCHECK(!cb.is_null()); |
108 return scoped_ptr<Subscription>( | 108 return std::unique_ptr<Subscription>( |
109 new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); | 109 new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); |
110 } | 110 } |
111 | 111 |
112 // Sets a callback which will be run when a subscription list is changed. | 112 // Sets a callback which will be run when a subscription list is changed. |
113 void set_removal_callback(const Closure& callback) { | 113 void set_removal_callback(const Closure& callback) { |
114 removal_callback_ = callback; | 114 removal_callback_ = callback; |
115 } | 115 } |
116 | 116 |
117 // Returns true if there are no subscriptions. This is only valid to call when | 117 // Returns true if there are no subscriptions. This is only valid to call when |
118 // not looping through the list. | 118 // not looping through the list. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 } | 221 } |
222 } | 222 } |
223 | 223 |
224 private: | 224 private: |
225 DISALLOW_COPY_AND_ASSIGN(CallbackList); | 225 DISALLOW_COPY_AND_ASSIGN(CallbackList); |
226 }; | 226 }; |
227 | 227 |
228 } // namespace base | 228 } // namespace base |
229 | 229 |
230 #endif // BASE_CALLBACK_LIST_H_ | 230 #endif // BASE_CALLBACK_LIST_H_ |
OLD | NEW |