Index: base/callback_list.h |
diff --git a/base/callback_list.h b/base/callback_list.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..defdfc2d5d7534fb9a6c9ce3c7dd2e76bcf1aa7a |
--- /dev/null |
+++ b/base/callback_list.h |
@@ -0,0 +1,140 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_CALLBACK_LIST_H_ |
+#define BASE_CALLBACK_LIST_H_ |
+ |
+#include <algorithm> |
+#include <limits> |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/callback_list_internal.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
+ |
+// OVERVIEW: |
+// |
+// A container for a list of callbacks. Unlike a normal STL vector or list, |
+// this container can be modified during iteration without invalidating the |
+// iterator. So, it safely handles the case of an callback removing itself |
awong
2013/09/04 17:44:35
So, it safely -> It safely
Cait (Slow)
2013/09/04 22:09:25
Done.
|
+// or other callback from the list while callbacks are being run. |
+// |
+// TYPICAL USAGE: |
+// |
+// class MyWidget { |
+// public: |
+// ... |
+// |
+// typedef base::Callback<void(const Foo&)> OnFooCallback; |
+// |
+// base::Closure RegisterCallback(const OnFooCallback& cb) { |
+// return callback_list_.Add(cb); |
+// } |
+// |
+// private: |
+// void NotifyFoo(const Foo& foo) { |
+// callback_list_.Run(foo); |
+// } |
+// |
+// CallbackList<Foo> callback_list_(CallbackList<Foo>::NOTIFY_ALL); |
+// }; |
+// |
+// |
+// class MyWidgetListener { |
+// public: |
+// MyWidgetListener::MyWidgetListener() { |
+// // TODO (caitkp): A less clunky approach here? |
Avi (use Gerrit)
2013/08/30 19:49:48
How would this be less clunky?
ScopedClosureRunne
awong
2013/09/04 17:44:35
Agreed...I think we can remove this comment.
Cait (Slow)
2013/09/04 22:09:25
Done.
|
+// remove_foo_callback_.Reset( |
+// MyWidget::GetCurrent()->RegisterCallback( |
+// base::Bind(&MyWidgetListener::OnFoo, this))); |
+// } |
+// |
+// MyWidgetListener::~MyWidgetListener() { |
+// // ScopedClosureRunner runs its closure automatically on deletion. |
+// } |
+// |
+// void OnFoo(const Foo& foo) { |
+// // Do something. |
+// } |
+// |
+// private: |
+// base::ScopedClosureRunner remove_foo_callback_; |
+// }; |
+ |
+namespace base { |
+template <typename Detail> |
+class CallbackListWithDetails { |
+ public: |
+ typedef base::Callback<void(const Detail&)> CallbackType; |
+ |
+ explicit CallbackListWithDetails(CallbackNotificationType type) |
+ : list_impl_(new base::internal::CallbackListImpl(type)) {} |
+ |
+ ~CallbackListWithDetails() {} |
+ |
+ // Add a callback to the list. A callback should not be added to |
+ // the same list more than once. The returned closure (used to remove the |
+ // callback from the list) is guaranteed to be safe to run. |
+ base::Closure Add(const CallbackType& cb) { |
+ DCHECK(!cb.is_null()); |
+ return list_impl_->AddCallback(new CallbackType(cb)); |
+ } |
+ |
+ // Execute all active (non-null) callbacks with |details| parameter. |
+ void Run(const Detail& details) { |
+ if (might_have_callbacks()) { |
+ internal::CallbackListImpl::Iterator it(list_impl_->GetWeakPtr()); |
awong
2013/09/04 17:44:35
I still feel like this should just be:
list_imp
Cait (Slow)
2013/09/04 22:09:25
Done.
|
+ CallbackType* cb; |
+ while((cb = static_cast<CallbackType*>(it.GetNext())) != NULL) { |
awong
2013/09/04 17:44:35
Add a comment explaining why we know this static_c
Cait (Slow)
2013/09/04 22:09:25
Done.
|
+ cb->Run(details); |
+ } |
+ } |
+ } |
+ |
+ // Delete (or nullify, if called during iteration), all callbacks in the list. |
+ void Clear() { |
+ list_impl_->Clear(); |
+ } |
+ |
+ bool might_have_callbacks() { |
+ return list_impl_->might_have_callbacks(); |
+ } |
+ |
+ private: |
+ scoped_ptr<base::internal::CallbackListImpl> list_impl_; |
awong
2013/09/04 17:44:35
This doesn't seem like it needs to be dynamically
Cait (Slow)
2013/09/04 22:09:25
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(CallbackListWithDetails); |
+}; |
+ |
+class CallbackList { |
+ public: |
+ explicit CallbackList(CallbackNotificationType type); |
+ |
+ ~CallbackList(); |
+ |
+ // Add a callback to the list. A callback should not be added to |
+ // the same list more than once. The returned closure (used to remove the |
+ // callback from the list) is guaranteed to be safe to run. |
+ base::Closure Add(const base::Closure& cb); |
+ |
+ // Execute all active (non-null) callbacks. |
+ void Run(); |
+ |
+ // Delete (or nullify, if called during iteration), all callbacks in the list. |
+ void Clear(); |
+ |
+ bool might_have_callbacks(); |
+ |
+ private: |
+ scoped_ptr<base::internal::CallbackListImpl> list_impl_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CallbackList); |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_CALLBACK_LIST_H_ |