Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2460)

Unified Diff: base/callback_list.h

Issue 22877038: Add a CallbackRegistry class to base/ to manage callbacks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/callback_list.h
diff --git a/base/callback_list.h b/base/callback_list.h
new file mode 100644
index 0000000000000000000000000000000000000000..aae10252d98a39ca4c8168813901fbd53b116c22
--- /dev/null
+++ b/base/callback_list.h
@@ -0,0 +1,114 @@
+// 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/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. 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;
+//
+// 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() {
+// 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 Details>
+class CallbackList
+ : public base::internal::CallbackListBase<Callback<void(const Details&)> > {
+ public:
+ typedef base::Callback<void(const Details&)> CallbackType;
+
+ CallbackList() {}
+
+ // Execute all active callbacks with |details| parameter.
+ void Run(const Details& details) {
+ internal::CallbackListImpl::Iterator it = this->GetIterator();
+ CallbackType* cb;
+ // static_cast is safe here. All callbacks were added via
+ // CallbackListBase::Add() and so must be of type
+ // Callback<void(const Details&)>.
+ while((cb = static_cast<CallbackType*>(it.GetNext())) != NULL) {
+ cb->Run(details);
+ }
+ }
+
+private:
+ DISALLOW_COPY_AND_ASSIGN(CallbackList);
+};
+
+template <> class CallbackList<void>
+ : public base::internal::CallbackListBase<Closure> {
+ public:
+ CallbackList() {}
+
+ // Execute all active callbacks.
+ void Run() {
+ internal::CallbackListImpl::Iterator it = this->GetIterator();
+ Closure* cb;
+ // static_cast is safe here. All callbacks were added via
+ // CallbackListBase::Add() and so must be Closures.
+ while((cb = static_cast<Closure*>(it.GetNext())) != NULL) {
+ cb->Run();
+ }
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(CallbackList);
+};
+
+} // namespace base
+
+#endif // BASE_CALLBACK_LIST_H_

Powered by Google App Engine
This is Rietveld 408576698