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

Side by Side Diff: ui/base/accelerators/accelerator_manager.cc

Issue 2930573005: Use ContainsValue() instead of std::find() in ui/android, ui/base and ui/views (Closed)
Patch Set: Changed EXPECT_FALSE to EXPECT_TRUE Created 3 years, 6 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
« no previous file with comments | « ui/android/view_android.cc ('k') | ui/base/clipboard/clipboard.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/accelerators/accelerator_manager.h" 5 #include "ui/base/accelerators/accelerator_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/stl_util.h"
10 #include "ui/base/accelerators/accelerator_manager_delegate.h" 11 #include "ui/base/accelerators/accelerator_manager_delegate.h"
11 12
12 namespace ui { 13 namespace ui {
13 14
14 AcceleratorManager::AcceleratorManager(AcceleratorManagerDelegate* delegate) 15 AcceleratorManager::AcceleratorManager(AcceleratorManagerDelegate* delegate)
15 : delegate_(delegate) {} 16 : delegate_(delegate) {}
16 17
17 AcceleratorManager::~AcceleratorManager() { 18 AcceleratorManager::~AcceleratorManager() {
18 } 19 }
19 20
20 void AcceleratorManager::Register( 21 void AcceleratorManager::Register(
21 const std::vector<ui::Accelerator>& accelerators, 22 const std::vector<ui::Accelerator>& accelerators,
22 HandlerPriority priority, 23 HandlerPriority priority,
23 AcceleratorTarget* target) { 24 AcceleratorTarget* target) {
24 DCHECK(target); 25 DCHECK(target);
25 26
26 // Accelerators which haven't already been registered with any target. 27 // Accelerators which haven't already been registered with any target.
27 std::vector<ui::Accelerator> new_accelerators; 28 std::vector<ui::Accelerator> new_accelerators;
28 29
29 for (const ui::Accelerator& accelerator : accelerators) { 30 for (const ui::Accelerator& accelerator : accelerators) {
30 AcceleratorTargetList& targets = accelerators_[accelerator].second; 31 AcceleratorTargetList& targets = accelerators_[accelerator].second;
31 DCHECK(std::find(targets.begin(), targets.end(), target) == targets.end()) 32 DCHECK(!base::ContainsValue(targets, target))
32 << "Registering the same target multiple times"; 33 << "Registering the same target multiple times";
33 const bool is_first_target_for_accelerator = targets.empty(); 34 const bool is_first_target_for_accelerator = targets.empty();
34 35
35 // All priority accelerators go to the front of the line. 36 // All priority accelerators go to the front of the line.
36 if (priority == kHighPriority) { 37 if (priority == kHighPriority) {
37 DCHECK(!accelerators_[accelerator].first) 38 DCHECK(!accelerators_[accelerator].first)
38 << "Only one high-priority handler can be registered"; 39 << "Only one high-priority handler can be registered";
39 targets.push_front(target); 40 targets.push_front(target);
40 // Mark that we have a priority accelerator at the front. 41 // Mark that we have a priority accelerator at the front.
41 accelerators_[accelerator].first = true; 42 accelerators_[accelerator].first = true;
(...skipping 22 matching lines...) Expand all
64 return; 65 return;
65 } 66 }
66 67
67 UnregisterImpl(map_iter, target); 68 UnregisterImpl(map_iter, target);
68 } 69 }
69 70
70 void AcceleratorManager::UnregisterAll(AcceleratorTarget* target) { 71 void AcceleratorManager::UnregisterAll(AcceleratorTarget* target) {
71 for (AcceleratorMap::iterator map_iter = accelerators_.begin(); 72 for (AcceleratorMap::iterator map_iter = accelerators_.begin();
72 map_iter != accelerators_.end();) { 73 map_iter != accelerators_.end();) {
73 AcceleratorTargetList* targets = &map_iter->second.second; 74 AcceleratorTargetList* targets = &map_iter->second.second;
74 AcceleratorTargetList::iterator target_iter = 75 if (!base::ContainsValue(*targets, target)) {
75 std::find(targets->begin(), targets->end(), target);
76 if (target_iter == targets->end()) {
77 ++map_iter; 76 ++map_iter;
78 } else { 77 } else {
79 auto tmp_iter = map_iter; 78 auto tmp_iter = map_iter;
80 ++map_iter; 79 ++map_iter;
81 UnregisterImpl(tmp_iter, target); 80 UnregisterImpl(tmp_iter, target);
82 } 81 }
83 } 82 }
84 } 83 }
85 84
86 bool AcceleratorManager::IsRegistered(const Accelerator& accelerator) const { 85 bool AcceleratorManager::IsRegistered(const Accelerator& accelerator) const {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 targets->remove(target); 139 targets->remove(target);
141 if (!targets->empty()) 140 if (!targets->empty())
142 return; 141 return;
143 const ui::Accelerator accelerator = map_iter->first; 142 const ui::Accelerator accelerator = map_iter->first;
144 accelerators_.erase(map_iter); 143 accelerators_.erase(map_iter);
145 if (delegate_) 144 if (delegate_)
146 delegate_->OnAcceleratorUnregistered(accelerator); 145 delegate_->OnAcceleratorUnregistered(accelerator);
147 } 146 }
148 147
149 } // namespace ui 148 } // namespace ui
OLDNEW
« no previous file with comments | « ui/android/view_android.cc ('k') | ui/base/clipboard/clipboard.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698