caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 1 | $$ This is a pump file for generating file templates. Pump is a python |
| 2 | $$ script that is part of the Google Test suite of utilities. Description |
| 3 | $$ can be found here: |
| 4 | $$ |
| 5 | $$ http://code.google.com/p/googletest/wiki/PumpManual |
| 6 | $$ |
| 7 | |
| 8 | $$ See comment for MAX_ARITY in base/bind.h.pump. |
| 9 | $var MAX_ARITY = 7 |
| 10 | |
| 11 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 12 | // Use of this source code is governed by a BSD-style license that can be |
| 13 | // found in the LICENSE file. |
| 14 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 15 | #ifndef BASE_CALLBACK_LIST_H_ |
| 16 | #define BASE_CALLBACK_LIST_H_ |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 17 | |
| 18 | #include <list> |
| 19 | |
| 20 | #include "base/basictypes.h" |
| 21 | #include "base/callback.h" |
| 22 | #include "base/callback_internal.h" |
| 23 | #include "base/compiler_specific.h" |
| 24 | #include "base/logging.h" |
| 25 | #include "base/memory/scoped_ptr.h" |
| 26 | |
| 27 | // OVERVIEW: |
| 28 | // |
| 29 | // A container for a list of callbacks. Unlike a normal STL vector or list, |
| 30 | // this container can be modified during iteration without invalidating the |
| 31 | // iterator. It safely handles the case of a callback removing itself |
| 32 | // or another callback from the list while callbacks are being run. |
| 33 | // |
| 34 | // TYPICAL USAGE: |
| 35 | // |
| 36 | // class MyWidget { |
| 37 | // public: |
| 38 | // ... |
| 39 | // |
| 40 | // typedef base::Callback<void(const Foo&)> OnFooCallback; |
| 41 | // |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 42 | // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 43 | // RegisterCallback(const OnFooCallback& cb) { |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 44 | // return callback_list_.Add(cb); |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 45 | // } |
| 46 | // |
| 47 | // private: |
| 48 | // void NotifyFoo(const Foo& foo) { |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 49 | // callback_list_.Notify(foo); |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 50 | // } |
| 51 | // |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 52 | // base::CallbackList<void(const Foo&)> callback_list_; |
caitkp@chromium.org | 0b4414d | 2013-10-10 08:28:21 +0900 | [diff] [blame] | 53 | // |
| 54 | // DISALLOW_COPY_AND_ASSIGN(MyWidget); |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 55 | // }; |
| 56 | // |
| 57 | // |
| 58 | // class MyWidgetListener { |
| 59 | // public: |
| 60 | // MyWidgetListener::MyWidgetListener() { |
| 61 | // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback( |
| 62 | // base::Bind(&MyWidgetListener::OnFoo, this))); |
| 63 | // } |
| 64 | // |
| 65 | // MyWidgetListener::~MyWidgetListener() { |
| 66 | // // Subscription gets deleted automatically and will deregister |
| 67 | // // the callback in the process. |
| 68 | // } |
| 69 | // |
| 70 | // private: |
| 71 | // void OnFoo(const Foo& foo) { |
| 72 | // // Do something. |
| 73 | // } |
| 74 | // |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 75 | // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
| 76 | // foo_subscription_; |
caitkp@chromium.org | 0b4414d | 2013-10-10 08:28:21 +0900 | [diff] [blame] | 77 | // |
| 78 | // DISALLOW_COPY_AND_ASSIGN(MyWidgetListener); |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 79 | // }; |
| 80 | |
| 81 | namespace base { |
| 82 | |
| 83 | namespace internal { |
| 84 | |
| 85 | template <typename CallbackType> |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 86 | class CallbackListBase { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 87 | public: |
| 88 | class Subscription { |
| 89 | public: |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 90 | Subscription(CallbackListBase<CallbackType>* list, |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 91 | typename std::list<CallbackType>::iterator iter) |
| 92 | : list_(list), |
caitkp@chromium.org | 0b4414d | 2013-10-10 08:28:21 +0900 | [diff] [blame] | 93 | iter_(iter) { |
| 94 | } |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 95 | |
| 96 | ~Subscription() { |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 97 | if (list_->active_iterator_count_) { |
caitkp@chromium.org | 0b4414d | 2013-10-10 08:28:21 +0900 | [diff] [blame] | 98 | iter_->Reset(); |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 99 | } else { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 100 | list_->callbacks_.erase(iter_); |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 101 | if (!list_->removal_callback_.is_null()) |
| 102 | list_->removal_callback_.Run(); |
| 103 | } |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private: |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 107 | CallbackListBase<CallbackType>* list_; |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 108 | typename std::list<CallbackType>::iterator iter_; |
| 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(Subscription); |
| 111 | }; |
| 112 | |
| 113 | // Add a callback to the list. The callback will remain registered until the |
| 114 | // returned Subscription is destroyed, which must occur before the |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 115 | // CallbackList is destroyed. |
bauerb@chromium.org | 69cfb18 | 2013-11-16 00:54:36 +0900 | [diff] [blame] | 116 | scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 117 | DCHECK(!cb.is_null()); |
| 118 | return scoped_ptr<Subscription>( |
| 119 | new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); |
| 120 | } |
| 121 | |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 122 | // Sets a callback which will be run when a subscription list is changed. |
| 123 | void set_removal_callback(const Closure& callback) { |
| 124 | removal_callback_ = callback; |
| 125 | } |
| 126 | |
| 127 | // Returns true if there are no subscriptions. This is only valid to call when |
| 128 | // not looping through the list. |
| 129 | bool empty() { |
| 130 | DCHECK_EQ(0, active_iterator_count_); |
| 131 | return callbacks_.empty(); |
| 132 | } |
| 133 | |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 134 | protected: |
| 135 | // An iterator class that can be used to access the list of callbacks. |
| 136 | class Iterator { |
| 137 | public: |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 138 | explicit Iterator(CallbackListBase<CallbackType>* list) |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 139 | : list_(list), |
| 140 | list_iter_(list_->callbacks_.begin()) { |
| 141 | ++list_->active_iterator_count_; |
| 142 | } |
| 143 | |
| 144 | Iterator(const Iterator& iter) |
| 145 | : list_(iter.list_), |
| 146 | list_iter_(iter.list_iter_) { |
| 147 | ++list_->active_iterator_count_; |
| 148 | } |
| 149 | |
| 150 | ~Iterator() { |
| 151 | if (list_ && --list_->active_iterator_count_ == 0) { |
| 152 | list_->Compact(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | CallbackType* GetNext() { |
| 157 | while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null()) |
| 158 | ++list_iter_; |
| 159 | |
| 160 | CallbackType* cb = NULL; |
| 161 | if (list_iter_ != list_->callbacks_.end()) { |
| 162 | cb = &(*list_iter_); |
| 163 | ++list_iter_; |
| 164 | } |
| 165 | return cb; |
| 166 | } |
| 167 | |
| 168 | private: |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 169 | CallbackListBase<CallbackType>* list_; |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 170 | typename std::list<CallbackType>::iterator list_iter_; |
| 171 | }; |
| 172 | |
caitkp@chromium.org | 0b4414d | 2013-10-10 08:28:21 +0900 | [diff] [blame] | 173 | CallbackListBase() : active_iterator_count_(0) {} |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 174 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 175 | ~CallbackListBase() { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 176 | DCHECK_EQ(0, active_iterator_count_); |
| 177 | DCHECK_EQ(0U, callbacks_.size()); |
| 178 | } |
| 179 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 180 | // Returns an instance of a CallbackListBase::Iterator which can be used |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 181 | // to run callbacks. |
| 182 | Iterator GetIterator() { |
| 183 | return Iterator(this); |
| 184 | } |
| 185 | |
| 186 | // Compact the list: remove any entries which were NULLed out during |
| 187 | // iteration. |
| 188 | void Compact() { |
| 189 | typename std::list<CallbackType>::iterator it = callbacks_.begin(); |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 190 | bool updated = false; |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 191 | while (it != callbacks_.end()) { |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 192 | if ((*it).is_null()) { |
| 193 | updated = true; |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 194 | it = callbacks_.erase(it); |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 195 | } else { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 196 | ++it; |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (updated && !removal_callback_.is_null()) |
| 200 | removal_callback_.Run(); |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | private: |
| 205 | std::list<CallbackType> callbacks_; |
| 206 | int active_iterator_count_; |
jam@chromium.org | 63a01af | 2014-05-13 01:36:10 +0900 | [diff] [blame] | 207 | Closure removal_callback_; |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 208 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 209 | DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | } // namespace internal |
| 213 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 214 | template <typename Sig> class CallbackList; |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 215 | |
| 216 | |
| 217 | $range ARITY 0..MAX_ARITY |
| 218 | $for ARITY [[ |
| 219 | $range ARG 1..ARITY |
| 220 | |
| 221 | $if ARITY == 0 [[ |
| 222 | template <> |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 223 | class CallbackList<void(void)> |
| 224 | : public internal::CallbackListBase<Callback<void(void)> > { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 225 | ]] $else [[ |
| 226 | template <$for ARG , [[typename A$(ARG)]]> |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 227 | class CallbackList<void($for ARG , [[A$(ARG)]])> |
caitkp@chromium.org | 0b4414d | 2013-10-10 08:28:21 +0900 | [diff] [blame] | 228 | : public internal::CallbackListBase<Callback<void($for ARG , [[A$(ARG)]])> > { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 229 | ]] |
| 230 | |
| 231 | public: |
| 232 | $if ARITY == 0 [[ |
| 233 | |
| 234 | typedef Callback<void(void)> CallbackType; |
| 235 | ]] $else [[ |
| 236 | |
| 237 | typedef Callback<void($for ARG , [[A$(ARG)]])> CallbackType; |
| 238 | ]] |
| 239 | |
| 240 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 241 | CallbackList() {} |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 242 | |
| 243 | void Notify($for ARG , |
| 244 | [[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) { |
| 245 | $if ARITY == 0 [[ |
| 246 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 247 | internal::CallbackListBase<CallbackType>::Iterator it = |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 248 | this->GetIterator(); |
| 249 | ]] $else [[ |
| 250 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 251 | typename internal::CallbackListBase<CallbackType>::Iterator it = |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 252 | this->GetIterator(); |
| 253 | ]] |
| 254 | |
| 255 | CallbackType* cb; |
caitkp@chromium.org | 0b4414d | 2013-10-10 08:28:21 +0900 | [diff] [blame] | 256 | while ((cb = it.GetNext()) != NULL) { |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 257 | cb->Run($for ARG , [[a$(ARG)]]); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private: |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 262 | DISALLOW_COPY_AND_ASSIGN(CallbackList); |
caitkp@google.com | 2b10b74 | 2013-09-25 22:56:23 +0900 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | |
| 266 | ]] $$ for ARITY |
| 267 | } // namespace base |
| 268 | |
caitkp@chromium.org | 4964732 | 2013-09-27 04:20:18 +0900 | [diff] [blame] | 269 | #endif // BASE_CALLBACK_LIST_H_ |