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

Unified Diff: base/callback_list_internal.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_internal.h
diff --git a/base/callback_list_internal.h b/base/callback_list_internal.h
new file mode 100644
index 0000000000000000000000000000000000000000..4b06464d8ad03da6ffa2e195bff1f3901f59c15f
--- /dev/null
+++ b/base/callback_list_internal.h
@@ -0,0 +1,137 @@
+// 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_INTERNAL_H_
+#define BASE_CALLBACK_LIST_INTERNAL_H_
+
+#include <limits>
+#include <vector>
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+#include "base/callback.h"
+#include "base/callback_internal.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+
+namespace base {
+namespace internal {
+
+// Holds the CallbackList methods that do not require specialization to reduce
+// template bloat.
+//
+// This class is meant as an implementation detail for CallbackList and
+// CallbackListBase. Do not use it directly.
+
+class CallbackListHandle {
+
+ private:
+
+};
+
+class CallbackListImpl {
+ public:
+ // An iterator class that can be used to access the list of callbacks.
+ class Iterator {
+ public:
+ explicit Iterator(const base::WeakPtr<CallbackListImpl>& list);
+
+ Iterator(const Iterator& iter);
+
+ ~Iterator();
+
+ base::internal::CallbackBase* GetNext();
+
+ private:
+ base::WeakPtr<CallbackListImpl> list_;
+ size_t index_;
+ };
+
+ CallbackListImpl();
+ ~CallbackListImpl();
+
+ // Add a callback to the list, if it is not already in there. This method
+ // takes ownership of |cb|, and will handle deleting it upon removal from the
+ // list. The returned closure is used to remove the callback from the list and
+ // must be run on the same thread as the CallbackList.
+ base::Closure Add(base::internal::CallbackBase* cb);
+
+ // Delete all callbacks in the list.
+ void Clear();
+
+ // Assert that the list is empty and no iterators are active.
+ void AssertEmpty();
+
+ // Get an iterator to the CallbackList.
+ CallbackListImpl::Iterator GetIterator();
+
+ // Compact the list (remove any elements which were NULLed out during
+ // iteration).
+ void Compact();
+
+ private:
+ typedef std::vector<base::internal::CallbackBase*> ListType;
+
+ // Use a static function to cancel callbacks. This provides strict enforcement
+ // that the caller must deregister the callback before the CallbackList is
+ // deleted.
+ static void CheckedRemove(const base::WeakPtr<CallbackListImpl>& list,
+ base::internal::CallbackBase* cb);
+
+
+ // Remove a callback from the list if it is in the list.
+ void Remove(base::internal::CallbackBase* cb);
+
+ ListType callbacks_;
+ int active_iterator_count_;
+ base::WeakPtrFactory<CallbackListImpl> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(CallbackListImpl);
+};
+
+// Holds the methods shared by all specializations of CallbackList. To avoid
+// code duplication.
+//
+// This class is meant as an implementation detail for CallbackList do not use
+// it directly.
+
+template <typename CallbackType>
+class BASE_EXPORT CallbackListBase {
+ public:
+ // 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) WARN_UNUSED_RESULT {
+ DCHECK(!cb.is_null());
+ return list_impl_.Add(new CallbackType(cb));
+ }
+
+ // Delete all callbacks in the list.
+ void Clear() {
+ list_impl_.Clear();
+ }
+
+ // Assert that the list is empty and no iterators are active.
+ void AssertEmpty() {
+ list_impl_.AssertEmpty();
+ }
+
+ protected:
+ CallbackListBase() {}
+
+ // Get an iterator to the CallbackList.
+ internal::CallbackListImpl::Iterator GetIterator() {
+ return list_impl_.GetIterator();
+ }
+
+ private:
+ CallbackListImpl list_impl_;
+
+ DISALLOW_COPY_AND_ASSIGN(CallbackListBase);
+};
+
+} // namespace internal
+} // namespace base
+
+#endif // BASE_CALLBACK_LIST_INTERNAL_H_

Powered by Google App Engine
This is Rietveld 408576698