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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_CALLBACK_LIST_INTERNAL_H_
6 #define BASE_CALLBACK_LIST_INTERNAL_H_
7
8 #include <limits>
9 #include <vector>
10
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/callback_internal.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17
18 namespace base {
19 namespace internal {
20
21 // Holds the CallbackList methods that do not require specialization to reduce
22 // template bloat.
23 //
24 // This class is meant as an implementation detail for CallbackList and
25 // CallbackListBase. Do not use it directly.
26
27 class CallbackListHandle {
28
29 private:
30
31 };
32
33 class CallbackListImpl {
34 public:
35 // An iterator class that can be used to access the list of callbacks.
36 class Iterator {
37 public:
38 explicit Iterator(const base::WeakPtr<CallbackListImpl>& list);
39
40 Iterator(const Iterator& iter);
41
42 ~Iterator();
43
44 base::internal::CallbackBase* GetNext();
45
46 private:
47 base::WeakPtr<CallbackListImpl> list_;
48 size_t index_;
49 };
50
51 CallbackListImpl();
52 ~CallbackListImpl();
53
54 // Add a callback to the list, if it is not already in there. This method
55 // takes ownership of |cb|, and will handle deleting it upon removal from the
56 // list. The returned closure is used to remove the callback from the list and
57 // must be run on the same thread as the CallbackList.
58 base::Closure Add(base::internal::CallbackBase* cb);
59
60 // Delete all callbacks in the list.
61 void Clear();
62
63 // Assert that the list is empty and no iterators are active.
64 void AssertEmpty();
65
66 // Get an iterator to the CallbackList.
67 CallbackListImpl::Iterator GetIterator();
68
69 // Compact the list (remove any elements which were NULLed out during
70 // iteration).
71 void Compact();
72
73 private:
74 typedef std::vector<base::internal::CallbackBase*> ListType;
75
76 // Use a static function to cancel callbacks. This provides strict enforcement
77 // that the caller must deregister the callback before the CallbackList is
78 // deleted.
79 static void CheckedRemove(const base::WeakPtr<CallbackListImpl>& list,
80 base::internal::CallbackBase* cb);
81
82
83 // Remove a callback from the list if it is in the list.
84 void Remove(base::internal::CallbackBase* cb);
85
86 ListType callbacks_;
87 int active_iterator_count_;
88 base::WeakPtrFactory<CallbackListImpl> weak_factory_;
89
90 DISALLOW_COPY_AND_ASSIGN(CallbackListImpl);
91 };
92
93 // Holds the methods shared by all specializations of CallbackList. To avoid
94 // code duplication.
95 //
96 // This class is meant as an implementation detail for CallbackList do not use
97 // it directly.
98
99 template <typename CallbackType>
100 class BASE_EXPORT CallbackListBase {
101 public:
102 // Add a callback to the list. A callback should not be added to
103 // the same list more than once. The returned closure (used to remove the
104 // callback from the list) is guaranteed to be safe to run.
105 base::Closure Add(const CallbackType& cb) WARN_UNUSED_RESULT {
106 DCHECK(!cb.is_null());
107 return list_impl_.Add(new CallbackType(cb));
108 }
109
110 // Delete all callbacks in the list.
111 void Clear() {
112 list_impl_.Clear();
113 }
114
115 // Assert that the list is empty and no iterators are active.
116 void AssertEmpty() {
117 list_impl_.AssertEmpty();
118 }
119
120 protected:
121 CallbackListBase() {}
122
123 // Get an iterator to the CallbackList.
124 internal::CallbackListImpl::Iterator GetIterator() {
125 return list_impl_.GetIterator();
126 }
127
128 private:
129 CallbackListImpl list_impl_;
130
131 DISALLOW_COPY_AND_ASSIGN(CallbackListBase);
132 };
133
134 } // namespace internal
135 } // namespace base
136
137 #endif // BASE_CALLBACK_LIST_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698