caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 5 | #ifndef BASE_CALLBACK_LIST_H_ |
| 6 | #define BASE_CALLBACK_LIST_H_ |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 7 | |
| 8 | #include <list> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include <memory> |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 10 | |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 11 | #include "base/callback.h" |
| 12 | #include "base/compiler_specific.h" |
| 13 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "base/macros.h" |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 15 | |
| 16 | // OVERVIEW: |
| 17 | // |
| 18 | // A container for a list of callbacks. Unlike a normal STL vector or list, |
| 19 | // this container can be modified during iteration without invalidating the |
| 20 | // iterator. It safely handles the case of a callback removing itself |
| 21 | // or another callback from the list while callbacks are being run. |
| 22 | // |
| 23 | // TYPICAL USAGE: |
| 24 | // |
| 25 | // class MyWidget { |
| 26 | // public: |
| 27 | // ... |
| 28 | // |
| 29 | // typedef base::Callback<void(const Foo&)> OnFooCallback; |
| 30 | // |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 31 | // std::unique_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
caitkp@google.com | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 32 | // RegisterCallback(const OnFooCallback& cb) { |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 33 | // return callback_list_.Add(cb); |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 34 | // } |
| 35 | // |
| 36 | // private: |
| 37 | // void NotifyFoo(const Foo& foo) { |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 38 | // callback_list_.Notify(foo); |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 39 | // } |
| 40 | // |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 41 | // base::CallbackList<void(const Foo&)> callback_list_; |
caitkp@chromium.org | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 42 | // |
| 43 | // DISALLOW_COPY_AND_ASSIGN(MyWidget); |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 44 | // }; |
| 45 | // |
| 46 | // |
| 47 | // class MyWidgetListener { |
| 48 | // public: |
| 49 | // MyWidgetListener::MyWidgetListener() { |
| 50 | // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback( |
| 51 | // base::Bind(&MyWidgetListener::OnFoo, this))); |
| 52 | // } |
| 53 | // |
| 54 | // MyWidgetListener::~MyWidgetListener() { |
| 55 | // // Subscription gets deleted automatically and will deregister |
| 56 | // // the callback in the process. |
| 57 | // } |
| 58 | // |
| 59 | // private: |
| 60 | // void OnFoo(const Foo& foo) { |
| 61 | // // Do something. |
| 62 | // } |
| 63 | // |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 64 | // std::unique_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
caitkp@google.com | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 65 | // foo_subscription_; |
caitkp@chromium.org | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 66 | // |
| 67 | // DISALLOW_COPY_AND_ASSIGN(MyWidgetListener); |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 68 | // }; |
| 69 | |
| 70 | namespace base { |
| 71 | |
| 72 | namespace internal { |
| 73 | |
| 74 | template <typename CallbackType> |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 75 | class CallbackListBase { |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 76 | public: |
| 77 | class Subscription { |
| 78 | public: |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 79 | Subscription(CallbackListBase<CallbackType>* list, |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 80 | typename std::list<CallbackType>::iterator iter) |
| 81 | : list_(list), |
caitkp@chromium.org | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 82 | iter_(iter) { |
| 83 | } |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 84 | |
| 85 | ~Subscription() { |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 86 | if (list_->active_iterator_count_) { |
caitkp@chromium.org | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 87 | iter_->Reset(); |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 88 | } else { |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 89 | list_->callbacks_.erase(iter_); |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 90 | if (!list_->removal_callback_.is_null()) |
| 91 | list_->removal_callback_.Run(); |
| 92 | } |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 96 | CallbackListBase<CallbackType>* list_; |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 97 | typename std::list<CallbackType>::iterator iter_; |
| 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(Subscription); |
| 100 | }; |
| 101 | |
| 102 | // Add a callback to the list. The callback will remain registered until the |
| 103 | // returned Subscription is destroyed, which must occur before the |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 104 | // CallbackList is destroyed. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 105 | std::unique_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 106 | DCHECK(!cb.is_null()); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 107 | return std::unique_ptr<Subscription>( |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 108 | new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Sets a callback which will be run when a subscription list is changed. |
| 112 | void set_removal_callback(const Closure& callback) { |
| 113 | removal_callback_ = callback; |
| 114 | } |
| 115 | |
| 116 | // Returns true if there are no subscriptions. This is only valid to call when |
| 117 | // not looping through the list. |
| 118 | bool empty() { |
| 119 | DCHECK_EQ(0, active_iterator_count_); |
| 120 | return callbacks_.empty(); |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | protected: |
| 124 | // An iterator class that can be used to access the list of callbacks. |
| 125 | class Iterator { |
| 126 | public: |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 127 | explicit Iterator(CallbackListBase<CallbackType>* list) |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 128 | : list_(list), |
| 129 | list_iter_(list_->callbacks_.begin()) { |
| 130 | ++list_->active_iterator_count_; |
| 131 | } |
| 132 | |
| 133 | Iterator(const Iterator& iter) |
| 134 | : list_(iter.list_), |
| 135 | list_iter_(iter.list_iter_) { |
| 136 | ++list_->active_iterator_count_; |
| 137 | } |
| 138 | |
| 139 | ~Iterator() { |
| 140 | if (list_ && --list_->active_iterator_count_ == 0) { |
| 141 | list_->Compact(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | CallbackType* GetNext() { |
| 146 | while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null()) |
| 147 | ++list_iter_; |
| 148 | |
caitkp@google.com | da04fbf | 2013-09-11 16:44:56 | [diff] [blame] | 149 | CallbackType* cb = NULL; |
| 150 | if (list_iter_ != list_->callbacks_.end()) { |
| 151 | cb = &(*list_iter_); |
| 152 | ++list_iter_; |
| 153 | } |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 154 | return cb; |
| 155 | } |
| 156 | |
| 157 | private: |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 158 | CallbackListBase<CallbackType>* list_; |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 159 | typename std::list<CallbackType>::iterator list_iter_; |
| 160 | }; |
| 161 | |
caitkp@chromium.org | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 162 | CallbackListBase() : active_iterator_count_(0) {} |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 163 | |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 164 | ~CallbackListBase() { |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 165 | DCHECK_EQ(0, active_iterator_count_); |
| 166 | DCHECK_EQ(0U, callbacks_.size()); |
| 167 | } |
| 168 | |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 169 | // Returns an instance of a CallbackListBase::Iterator which can be used |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 170 | // to run callbacks. |
| 171 | Iterator GetIterator() { |
| 172 | return Iterator(this); |
| 173 | } |
| 174 | |
| 175 | // Compact the list: remove any entries which were NULLed out during |
| 176 | // iteration. |
| 177 | void Compact() { |
| 178 | typename std::list<CallbackType>::iterator it = callbacks_.begin(); |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 179 | bool updated = false; |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 180 | while (it != callbacks_.end()) { |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 181 | if ((*it).is_null()) { |
| 182 | updated = true; |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 183 | it = callbacks_.erase(it); |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 184 | } else { |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 185 | ++it; |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 186 | } |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 187 | } |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 188 | |
| 189 | if (updated && !removal_callback_.is_null()) |
| 190 | removal_callback_.Run(); |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | private: |
| 194 | std::list<CallbackType> callbacks_; |
| 195 | int active_iterator_count_; |
jam@chromium.org | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 196 | Closure removal_callback_; |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 197 | |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } // namespace internal |
| 202 | |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 203 | template <typename Sig> class CallbackList; |
caitkp@google.com | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 204 | |
thakis | 6117a47 | 2014-11-20 01:15:14 | [diff] [blame] | 205 | template <typename... Args> |
| 206 | class CallbackList<void(Args...)> |
| 207 | : public internal::CallbackListBase<Callback<void(Args...)> > { |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 208 | public: |
thakis | 6117a47 | 2014-11-20 01:15:14 | [diff] [blame] | 209 | typedef Callback<void(Args...)> CallbackType; |
caitkp@google.com | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 210 | |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 211 | CallbackList() {} |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 212 | |
tzik | d2046ce3 | 2016-04-14 21:44:39 | [diff] [blame] | 213 | template <typename... RunArgs> |
| 214 | void Notify(RunArgs&&... args) { |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 215 | typename internal::CallbackListBase<CallbackType>::Iterator it = |
caitkp@google.com | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 216 | this->GetIterator(); |
| 217 | CallbackType* cb; |
caitkp@chromium.org | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 218 | while ((cb = it.GetNext()) != NULL) { |
thakis | 6117a47 | 2014-11-20 01:15:14 | [diff] [blame] | 219 | cb->Run(args...); |
caitkp@google.com | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | private: |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 224 | DISALLOW_COPY_AND_ASSIGN(CallbackList); |
caitkp@google.com | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 225 | }; |
| 226 | |
caitkp@chromium.org | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 227 | } // namespace base |
| 228 | |
caitkp@chromium.org | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 229 | #endif // BASE_CALLBACK_LIST_H_ |