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

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: Get rid of CallbackListWithDetails, use specialization instead 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_internal.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16
17 namespace base {
18 // Enumeration of which callbacks are called.
19 enum CallbackNotificationType {
erikwright (departed) 2013/09/04 19:35:55 I would strongly prefer that we get opinionated an
Cait (Slow) 2013/09/04 22:09:25 Agree. By default, all of the current Notification
20 // Specifies that any callbacks added during notification are called.
21 CALLBACKS_NOTIFY_ALL,
22
23 // Specifies that callbacks added while sending out notifications are not
24 // called.
25 CALLBACKS_NOTIFY_EXISTING_ONLY
26 };
27
28 namespace internal {
29
30 // Holds the CallbackList methods that don't require specialization to reduce
31 // template bloat.
32 //
33 // This class is meant as an implementation detail for CallbackList and
34 // CallbackListWithDetails. Do not use it directly.
35
36 class BASE_EXPORT CallbackListImpl {
37 public:
38 typedef std::vector<base::internal::CallbackBase*> ListType;
39
40 // An iterator class that can be used to access the list of callbacks.
41 class Iterator {
42 public:
43 explicit Iterator(const base::WeakPtr<CallbackListImpl>& list);
erikwright (departed) 2013/09/04 19:35:55 Instead of exposing GetWeakPtr and allowing client
Cait (Slow) 2013/09/04 22:09:25 Done.
44
45 ~Iterator();
46
47 base::internal::CallbackBase* GetNext();
48
49 private:
50 base::WeakPtr<CallbackListImpl> list_;
51 size_t index_;
52 size_t max_index_;
53 };
54
55 explicit CallbackListImpl(CallbackNotificationType type);
56 ~CallbackListImpl();
57
58 // Add a callback to the list, if it is not already in there. This method
59 // takes ownership of |cb|, and will handle deleting it upon removal from the
60 // list.
61 base::Closure AddCallback(base::internal::CallbackBase* cb);
awong 2013/09/04 18:48:21 Rename AddCallback/RemoveCallback to Add/Remove to
erikwright (departed) 2013/09/04 19:35:55 Take a scoped_ptr. I think that's the new standard
erikwright (departed) 2013/09/04 19:35:55 Document the purpose of the return value.
Cait (Slow) 2013/09/04 22:09:25 CallbackBase has a protected dtor, so a scoped_ptr
Cait (Slow) 2013/09/04 22:09:25 Done.
Cait (Slow) 2013/09/04 22:09:25 Done.
62
63 // Delete (or nullify, if called during iteration), all callbacks in the list.
64 void Clear();
65
66 // Returns true if there is a chance that the list may still contain active
67 // callbacks.
68 bool might_have_callbacks() const { return size() != 0; }
erikwright (departed) 2013/09/04 19:35:55 Why would a client call this?
Cait (Slow) 2013/09/04 22:09:25 removing it
69
70 // Remove a callback from the list if it is in the list.
71 void RemoveCallback(base::internal::CallbackBase* cb);
erikwright (departed) 2013/09/04 19:35:55 Seems this could be private (no reason not to insi
Cait (Slow) 2013/09/04 22:09:25 Done.
72
73 // Get a weak pointer to the CallbackList.
74 base::WeakPtr<CallbackListImpl> GetWeakPtr();
75
76 // Compact the list (remove any elements which were nulled out during
77 // iteration).
78 void Compact();
erikwright (departed) 2013/09/04 19:35:55 Any reason for a client to call this (as opposed t
Cait (Slow) 2013/09/04 22:09:25 No -- it should only be called by the iterator.
79
80 // Returns the number of callbacks in the list. Note that if the list has not
81 // been compacted, some of these callbacks may be NULL.
82 size_t size() const { return callbacks_.size(); }
awong 2013/09/04 18:48:21 This doesn't seem necessary. Remove?
Cait (Slow) 2013/09/04 22:09:25 Done.
83
84 private:
85 ListType callbacks_;
86 int active_iterator_count_;
87 CallbackNotificationType type_;
88 base::WeakPtrFactory<CallbackListImpl> weak_factory_;
89
90 DISALLOW_COPY_AND_ASSIGN(CallbackListImpl);
91 };
92
93 } // namespace internal
94 } // namespace base
95
96 #endif // BASE_CALLBACK_LIST_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698