Index: base/callback_registry.h |
diff --git a/base/callback_registry.h b/base/callback_registry.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..85d1ef90065f26ff5c5bbd19ea2f294964532270 |
--- /dev/null |
+++ b/base/callback_registry.h |
@@ -0,0 +1,113 @@ |
+// 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_REGISTRY_H_ |
+#define BASE_CALLBACK_REGISTRY_H_ |
+ |
+#include <algorithm> |
+#include <limits> |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+#include "base/callback.h" |
+#include "base/callback_registry_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. It safely handles the case of a callback removing itself |
+// or another callback from the list while callbacks are being run. |
+// |
+// TYPICAL USAGE: |
+// |
+// class MyWidget { |
+// public: |
+// ... |
+// |
+// typedef base::Callback<void(const Foo&)> OnFooCallback; |
+// |
+// scoped_ptr<base::CallbackHandle> RegisterCallback( |
+// const OnFooCallback& cb) { |
+// return callback_registry_.Add(cb); |
+// } |
+// |
+// private: |
+// void NotifyFoo(const Foo& foo) { |
+// callback_registry_.Run(foo); |
awong
2013/09/06 19:03:24
I'd rename this to Notify() now. Seems more sensib
Cait (Slow)
2013/09/06 22:17:11
Done.
|
+// } |
+// |
+// CallbackRegistry<Foo> callback_registry_(); |
+// }; |
+// |
+// |
+// class MyWidgetListener { |
+// public: |
+// MyWidgetListener::MyWidgetListener() { |
+// foo_callback_handle_ = MyWidget::GetCurrent()->RegisterCallback( |
+// base::Bind(&MyWidgetListener::OnFoo, this))); |
+// } |
+// |
+// MyWidgetListener::~MyWidgetListener() { |
+// // CallbackHandle gets deleted automatically and will deregister |
+// // the callback in the process. |
+// } |
+// |
+// void OnFoo(const Foo& foo) { |
+// // Do something. |
+// } |
+// |
+// private: |
+// scoped_ptr<base::CallbackHandle> foo_callback_handle_; |
+// }; |
+ |
+namespace base { |
+ |
+template <typename Details> |
+class CallbackRegistry |
+ : public base::internal::CallbackRegistryBase< |
+ Callback<void(const Details&)> > { |
+ public: |
+ typedef base::Callback<void(const Details&)> CallbackType; |
awong
2013/09/06 19:03:24
I don't think you need this CallbackType either do
Cait (Slow)
2013/09/06 22:17:11
Done.
|
+ typedef typename base::internal::CallbackRegistryBase<CallbackType>::Iterator |
awong
2013/09/06 19:03:24
You shouldn't need this typedef since Iterator sho
Cait (Slow)
2013/09/06 22:17:11
I either need to typedef it or refer to it in full
|
+ Iterator; |
+ CallbackRegistry() {} |
+ |
+ // Execute all active callbacks with |details| parameter. |
+ void Run(const Details& details) { |
+ Iterator it = this->GetIterator(); |
+ CallbackType* cb; |
+ while((cb = it.GetNext()) != NULL) { |
+ cb->Run(details); |
+ } |
+ } |
+ |
+private: |
+ DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
+}; |
+ |
+template <> class CallbackRegistry<void> |
+ : public base::internal::CallbackRegistryBase<Closure> { |
+ public: |
+ CallbackRegistry() {} |
+ typedef typename base::internal::CallbackRegistryBase<Closure>::Iterator |
+ Iterator; |
+ // Execute all active callbacks. |
+ void Run() { |
+ Iterator it = this->GetIterator(); |
+ Closure* cb; |
+ while((cb = it.GetNext()) != NULL) { |
+ cb->Run(); |
+ } |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_CALLBACK_REGISTRY_H_ |