Commit a234a077 authored by pkasting's avatar pkasting Committed by Commit bot

Allow use of container cbegin() and cend() methods, plus sample uses.

This also fixes a small bug in the histogram recording of TabStripModelStatsRecorder::ActiveTabChanged() I spotted while doing this.

BUG=none
TEST=none

Review URL: https://codereview.chromium.org/1675483002

Cr-Commit-Position: refs/heads/master@{#374827}
parent 0cb852e8
...@@ -168,14 +168,14 @@ void TabStripModelStatsRecorder::ActiveTabChanged( ...@@ -168,14 +168,14 @@ void TabStripModelStatsRecorder::ActiveTabChanged(
// A UMA Histogram must be bounded by some number. // A UMA Histogram must be bounded by some number.
// We chose 64 as our bound as 99.5% of the users open <64 tabs. // We chose 64 as our bound as 99.5% of the users open <64 tabs.
const int kMaxTabHistory = 64; const int kMaxTabHistory = 64;
auto it = std::find(active_tab_history_.begin(), active_tab_history_.end(), auto it = std::find(active_tab_history_.cbegin(), active_tab_history_.cend(),
new_contents); new_contents);
int age = it != active_tab_history_.end() ? (it - active_tab_history_.begin()) int age = (it != active_tab_history_.cend()) ?
: (kMaxTabHistory - 1); (it - active_tab_history_.cbegin()) : (kMaxTabHistory - 1);
if (was_inactive) { if (was_inactive) {
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"Tabs.StateTransfer.NumberOfOtherTabsActivatedBeforeMadeActive", "Tabs.StateTransfer.NumberOfOtherTabsActivatedBeforeMadeActive",
std::min(age, kMaxTabHistory), kMaxTabHistory); std::min(age, kMaxTabHistory - 1), kMaxTabHistory);
} }
active_tab_history_.insert(active_tab_history_.begin(), new_contents); active_tab_history_.insert(active_tab_history_.begin(), new_contents);
......
...@@ -588,11 +588,10 @@ void BrowserActionsContainer::WriteDragDataForView(View* sender, ...@@ -588,11 +588,10 @@ void BrowserActionsContainer::WriteDragDataForView(View* sender,
toolbar_actions_bar_->OnDragStarted(); toolbar_actions_bar_->OnDragStarted();
DCHECK(data); DCHECK(data);
ToolbarActionViews::iterator iter = std::find(toolbar_action_views_.begin(), auto it = std::find(toolbar_action_views_.cbegin(),
toolbar_action_views_.end(), toolbar_action_views_.cend(), sender);
sender); DCHECK(it != toolbar_action_views_.cend());
DCHECK(iter != toolbar_action_views_.end()); ToolbarActionViewController* view_controller = (*it)->view_controller();
ToolbarActionViewController* view_controller = (*iter)->view_controller();
gfx::Size size(ToolbarActionsBar::IconWidth(false), gfx::Size size(ToolbarActionsBar::IconWidth(false),
ToolbarActionsBar::IconHeight()); ToolbarActionsBar::IconHeight());
drag_utils::SetDragImageOnDataObject( drag_utils::SetDragImageOnDataObject(
...@@ -601,7 +600,7 @@ void BrowserActionsContainer::WriteDragDataForView(View* sender, ...@@ -601,7 +600,7 @@ void BrowserActionsContainer::WriteDragDataForView(View* sender,
data); data);
// Fill in the remaining info. // Fill in the remaining info.
BrowserActionDragData drag_data(view_controller->GetId(), BrowserActionDragData drag_data(view_controller->GetId(),
iter - toolbar_action_views_.begin()); it - toolbar_action_views_.cbegin());
drag_data.Write(browser_->profile(), data); drag_data.Write(browser_->profile(), data);
} }
......
...@@ -357,13 +357,10 @@ Note that &lt;algorithm&gt; contains a range-based <code>move</code> method. Th ...@@ -357,13 +357,10 @@ Note that &lt;algorithm&gt; contains a range-based <code>move</code> method. Th
<tr> <tr>
<td>Begin and End Non-Member Functions</td> <td>Begin and End Non-Member Functions</td>
<td><code>std::begin()</code> and <code>std::end()</code></td> <td><code>std::begin()</code> and <code>std::end()</code></td>
<td>Allows use of free functions on any container, including <td>Allows use of free functions on any container, including fixed-size arrays</td>
fixed-size arrays</td> <td><a href="http://en.cppreference.com/w/cpp/iterator/begin">std::begin</a> and <a href="http://en.cppreference.com/w/cpp/iterator/end">std::end</a></td>
<td><a href="http://en.cppreference.com/w/cpp/iterator/begin"> <td>Useful for fixed-size arrays. <a href="https://groups.google.com/a/chromium.org/d/topic/cxx/5iFNE8P5qT4/discussion">Discussion thread</a><br>
std::begin</a> and Note that non-member <code>cbegin()</code> and <code>cend()</code> are not available until C++14.</td>
<a href="http://en.cppreference.com/w/cpp/iterator/end">
std::end</a></td>
<td>Useful for fixed-size arrays. <a href="https://groups.google.com/a/chromium.org/d/topic/cxx/5iFNE8P5qT4/discussion">Discussion thread</a></td>
</tr> </tr>
<tr> <tr>
...@@ -374,6 +371,14 @@ std::end</a></td> ...@@ -374,6 +371,14 @@ std::end</a></td>
<td>Usage should be rare. <a href='https://groups.google.com/a/chromium.org/forum/#!topic/cxx/vCxo4tZNd_M'>Discussion thread</a></td> <td>Usage should be rare. <a href='https://groups.google.com/a/chromium.org/forum/#!topic/cxx/vCxo4tZNd_M'>Discussion thread</a></td>
</tr> </tr>
<tr>
<td>Constant Iterator Methods on Containers</td>
<td><code>std::vector::cbegin()</code>, <code>std::vector::cend()</code></td>
<td>Allows more widespread use of <code>const_iterator</code></td>
<td>E.g. <a href="http://en.cppreference.com/w/cpp/container/vector/begin">std::vector::cbegin<a> and <a href="http://en.cppreference.com/w/cpp/container/vector/end">std::vector::cend<a></td>
<td>Applies to all containers, not just <code>vector</code>. Consider using <code>const_iterator</code> over <code>iterator</code> where possible for the same reason as using <code>const</code> variables and functions where possible; see Effective Modern C++ item 13 for motivation. <a href="https://groups.google.com/a/chromium.org/d/topic/cxx/cS83F_buqLM/discussion">Discussion thread</a></td>
</tr>
<tr> <tr>
<td>Containers containing movable types</td> <td>Containers containing movable types</td>
<td><code>vector&lt;scoped_ptr&gt;</code></td> <td><code>vector&lt;scoped_ptr&gt;</code></td>
...@@ -845,14 +850,6 @@ the <code>&lt;complex&gt;</code> library.</td> ...@@ -845,14 +850,6 @@ the <code>&lt;complex&gt;</code> library.</td>
<td></td> <td></td>
</tr> </tr>
<tr>
<td>Constant Iterator Methods on Containers</td>
<td><code>std::vector::cbegin()</code> and <code>std::vector::cend()</code></td>
<td>Enforces iteration methods that don't change container contents</td>
<td><a href="http://en.cppreference.com/w/cpp/container/vector/begin">std::vector::cbegin<a></td>
<td>This applies to all containers, std::vector is just an example. <a href="https://groups.google.com/a/chromium.org/d/topic/cxx/cS83F_buqLM/discussion">Discussion thread</a></td>
</tr>
<tr> <tr>
<td>Container Compaction Functions</td> <td>Container Compaction Functions</td>
<td><code>std::vector::shrink_to_fit()</code>, <td><code>std::vector::shrink_to_fit()</code>,
......
...@@ -3505,12 +3505,8 @@ TEST_F(ViewTest, GetViewByID) { ...@@ -3505,12 +3505,8 @@ TEST_F(ViewTest, GetViewByID) {
View::Views views; View::Views views;
v1.GetViewsInGroup(kGroup, &views); v1.GetViewsInGroup(kGroup, &views);
EXPECT_EQ(2U, views.size()); EXPECT_EQ(2U, views.size());
EXPECT_NE(views.cend(), std::find(views.cbegin(), views.cend(), &v3));
View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3)); EXPECT_NE(views.cend(), std::find(views.cbegin(), views.cend(), &v4));
EXPECT_NE(views.end(), i);
i = std::find(views.begin(), views.end(), &v4);
EXPECT_NE(views.end(), i);
} }
TEST_F(ViewTest, AddExistingChild) { TEST_F(ViewTest, AddExistingChild) {
......
...@@ -68,11 +68,9 @@ class WMStateWaiter : public X11PropertyChangeWaiter { ...@@ -68,11 +68,9 @@ class WMStateWaiter : public X11PropertyChangeWaiter {
bool ShouldKeepOnWaiting(const ui::PlatformEvent& event) override { bool ShouldKeepOnWaiting(const ui::PlatformEvent& event) override {
std::vector<Atom> hints; std::vector<Atom> hints;
if (ui::GetAtomArrayProperty(xwindow(), "_NET_WM_STATE", &hints)) { if (ui::GetAtomArrayProperty(xwindow(), "_NET_WM_STATE", &hints)) {
std::vector<Atom>::iterator it = std::find( auto it = std::find(hints.cbegin(), hints.cend(),
hints.begin(), atom_cache_->GetAtom(hint_));
hints.end(), bool hint_set = (it != hints.cend());
atom_cache_->GetAtom(hint_));
bool hint_set = (it != hints.end());
return hint_set != wait_till_set_; return hint_set != wait_till_set_;
} }
return true; return true;
......
...@@ -55,11 +55,9 @@ class MinimizeWaiter : public X11PropertyChangeWaiter { ...@@ -55,11 +55,9 @@ class MinimizeWaiter : public X11PropertyChangeWaiter {
bool ShouldKeepOnWaiting(const ui::PlatformEvent& event) override { bool ShouldKeepOnWaiting(const ui::PlatformEvent& event) override {
std::vector<Atom> wm_states; std::vector<Atom> wm_states;
if (ui::GetAtomArrayProperty(xwindow(), "_NET_WM_STATE", &wm_states)) { if (ui::GetAtomArrayProperty(xwindow(), "_NET_WM_STATE", &wm_states)) {
std::vector<Atom>::iterator it = std::find( auto it = std::find(wm_states.cbegin(), wm_states.cend(),
wm_states.begin(), atom_cache_->GetAtom("_NET_WM_STATE_HIDDEN"));
wm_states.end(), return it == wm_states.cend();
atom_cache_->GetAtom("_NET_WM_STATE_HIDDEN"));
return it == wm_states.end();
} }
return true; return true;
} }
...@@ -97,9 +95,8 @@ class StackingClientListWaiter : public X11PropertyChangeWaiter { ...@@ -97,9 +95,8 @@ class StackingClientListWaiter : public X11PropertyChangeWaiter {
std::vector<XID> stack; std::vector<XID> stack;
ui::GetXWindowStack(ui::GetX11RootWindow(), &stack); ui::GetXWindowStack(ui::GetX11RootWindow(), &stack);
for (size_t i = 0; i < expected_windows_.size(); ++i) { for (size_t i = 0; i < expected_windows_.size(); ++i) {
std::vector<XID>::iterator it = std::find( auto it = std::find(stack.cbegin(), stack.cend(), expected_windows_[i]);
stack.begin(), stack.end(), expected_windows_[i]); if (it == stack.cend())
if (it == stack.end())
return true; return true;
} }
return false; return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment