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

Side by Side Diff: base/callback_list_internal.cc

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
« no previous file with comments | « base/callback_list_internal.h ('k') | base/callback_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/callback_list_internal.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11
12 namespace base {
13 namespace internal {
14
15 CallbackListImpl::Iterator::Iterator(
16 const base::WeakPtr<CallbackListImpl>& list)
17 : list_(list),
18 index_(0) {
19 ++list_->active_iterator_count_;
20 }
21
22 CallbackListImpl::Iterator::Iterator(const Iterator& iter)
23 : list_(iter.list_),
24 index_(iter.index_) {
25 ++list_->active_iterator_count_;
26 }
27
28 CallbackListImpl::Iterator::~Iterator() {
29 if (list_ && --list_->active_iterator_count_ == 0) {
30 list_->Compact();
31 }
32 }
33
34 CallbackBase* CallbackListImpl::Iterator::GetNext() {
35 if (!list_)
36 return NULL;
37 size_t max_index = list_->callbacks_.size();
38 while (index_ < max_index && !list_->callbacks_[index_])
39 ++index_;
40 return index_ < max_index ? list_->callbacks_[index_++] : NULL;
41 }
42
43 // static
44 void CallbackListImpl::CheckedRemove(
45 const base::WeakPtr<CallbackListImpl>& list,
46 base::internal::CallbackBase* cb) {
47 list->Remove(cb);
48 }
49
50 CallbackListImpl::CallbackListImpl()
51 : active_iterator_count_(0),
52 weak_factory_(this) {}
53
54 CallbackListImpl::~CallbackListImpl() {
55 Clear();
56 }
57
58 void CallbackListImpl::Clear() {
59 if (active_iterator_count_) {
60 for (size_t i = 0; i < callbacks_.size(); ++i) {
61 CallbackBase* tmp = callbacks_[i];
62 delete tmp;
63 callbacks_[i] = NULL;
64 }
65 } else {
66 for (size_t i = 0; i < callbacks_.size(); ++i)
67 delete callbacks_[i];
68 callbacks_.clear();
69 }
70 }
71
72 void CallbackListImpl::AssertEmpty() {
73 DCHECK(active_iterator_count_ == 0 && callbacks_.size() == 0);
awong 2013/09/05 22:34:58 This should be a CHECK(). In production, if this i
74 }
75
76 void CallbackListImpl::Remove(base::internal::CallbackBase* cb) {
77 for (size_t i = 0; i < callbacks_.size(); i++) {
78 if (callbacks_[i] == cb) {
79 CallbackBase* tmp = callbacks_[i];
80 delete tmp;
81 if (active_iterator_count_) {
82 callbacks_[i] = NULL;
83 } else {
84 callbacks_.erase(callbacks_.begin() + i);
85 }
86 return;
87 }
88 }
89 }
90
91 base::Closure CallbackListImpl::Add(base::internal::CallbackBase* cb) {
92 callbacks_.push_back(cb);
93 return base::Bind(&CallbackListImpl::CheckedRemove,
94 weak_factory_.GetWeakPtr(), cb);
95 }
96
97 CallbackListImpl::Iterator CallbackListImpl::GetIterator() {
98 return Iterator(weak_factory_.GetWeakPtr());
99 }
100
101 void CallbackListImpl::Compact() {
102 callbacks_.erase(
103 std::remove(callbacks_.begin(), callbacks_.end(),
104 static_cast<base::internal::CallbackBase*>(NULL)),
105 callbacks_.end());
106 }
107
108 } // namespace internal
109 } // namespace base
OLDNEW
« no previous file with comments | « base/callback_list_internal.h ('k') | base/callback_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698