Commit b425288a authored by ruuda's avatar ruuda Committed by Commit bot

[StyleGuide] Allow begin and end non-member functions

Allow usage of |std::begin| and |std::end|. This is useful for iterating
fixed-size arrays.

Discussion thread: https://groups.google.com/a/chromium.org/d/topic/cxx/5iFNE8P5qT4/discussion

Furthermore, this CL replaces home-brewn |std::end|-like functions in
base/trace_event with |std::end| to verify that <iterator> is supported
on all platforms.

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

Cr-Commit-Position: refs/heads/master@{#361192}
parent 97b98515
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "base/trace_event/heap_profiler_allocation_context_tracker.h" #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
#include <algorithm> #include <algorithm>
#include <iterator>
#include "base/atomicops.h" #include "base/atomicops.h"
#include "base/threading/thread_local_storage.h" #include "base/threading/thread_local_storage.h"
...@@ -25,13 +26,6 @@ void DestructAllocationContextTracker(void* alloc_ctx_tracker) { ...@@ -25,13 +26,6 @@ void DestructAllocationContextTracker(void* alloc_ctx_tracker) {
delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker); delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker);
} }
// Returns a pointer past the end of the fixed-size array |array| of |T| of
// length |N|, identical to C++11 |std::end|.
template <typename T, int N>
T* End(T(&array)[N]) {
return array + N;
}
} // namespace } // namespace
AllocationContextTracker::AllocationContextTracker() {} AllocationContextTracker::AllocationContextTracker() {}
...@@ -99,9 +93,9 @@ AllocationContext AllocationContextTracker::GetContextSnapshot() { ...@@ -99,9 +93,9 @@ AllocationContext AllocationContextTracker::GetContextSnapshot() {
// Fill the backtrace. // Fill the backtrace.
{ {
auto src = tracker->pseudo_stack_.begin(); auto src = tracker->pseudo_stack_.begin();
auto dst = ctx.backtrace.frames; auto dst = std::begin(ctx.backtrace.frames);
auto src_end = tracker->pseudo_stack_.end(); auto src_end = tracker->pseudo_stack_.end();
auto dst_end = End(ctx.backtrace.frames); auto dst_end = std::end(ctx.backtrace.frames);
// Copy as much of the bottom of the pseudo stack into the backtrace as // Copy as much of the bottom of the pseudo stack into the backtrace as
// possible. // possible.
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <iterator>
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/trace_event/heap_profiler_allocation_context.h" #include "base/trace_event/heap_profiler_allocation_context.h"
#include "base/trace_event/heap_profiler_allocation_context_tracker.h" #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
...@@ -19,23 +21,16 @@ const char kEclair[] = "Eclair"; ...@@ -19,23 +21,16 @@ const char kEclair[] = "Eclair";
const char kFroyo[] = "Froyo"; const char kFroyo[] = "Froyo";
const char kGingerbread[] = "Gingerbread"; const char kGingerbread[] = "Gingerbread";
// Returns a pointer past the end of the fixed-size array |array| of |T| of
// length |N|, identical to C++11 |std::end|.
template <typename T, int N>
const T* End(const T(&array)[N]) {
return array + N;
}
// Asserts that the fixed-size array |expected_backtrace| matches the backtrace // Asserts that the fixed-size array |expected_backtrace| matches the backtrace
// in |AllocationContextTracker::GetContextSnapshot|. // in |AllocationContextTracker::GetContextSnapshot|.
template <size_t N> template <size_t N>
void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) { void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) {
AllocationContext ctx = AllocationContextTracker::GetContextSnapshot(); AllocationContext ctx = AllocationContextTracker::GetContextSnapshot();
auto actual = ctx.backtrace.frames; auto actual = std::begin(ctx.backtrace.frames);
auto actual_bottom = End(ctx.backtrace.frames); auto actual_bottom = std::end(ctx.backtrace.frames);
auto expected = expected_backtrace; auto expected = std::begin(expected_backtrace);
auto expected_bottom = End(expected_backtrace); auto expected_bottom = std::end(expected_backtrace);
// Note that this requires the pointers to be equal, this is not doing a deep // Note that this requires the pointers to be equal, this is not doing a deep
// string comparison. // string comparison.
......
...@@ -297,6 +297,18 @@ Parameter pack</a></td> ...@@ -297,6 +297,18 @@ Parameter pack</a></td>
<td><a href="https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16V7fmtbzok">Discussion thread</a>.</td> <td><a href="https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16V7fmtbzok">Discussion thread</a>.</td>
</tr> </tr>
<tr>
<td>Begin and End Non-Member Functions</td>
<td><code>std::begin()</code> and <code>std::end()</code></td>
<td>Allows use of free functions on any container, including
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>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>
<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>
...@@ -681,18 +693,6 @@ std::array</a></td> ...@@ -681,18 +693,6 @@ std::array</a></td>
<td></td> <td></td>
</tr> </tr>
<tr>
<td>Begin and End Non-Member Functions</td>
<td><code>std::begin()</code> and <code>std::end()</code></td>
<td>Allows use of free functions on any container, including
built-in 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>Useful for built-in arrays.</td>
</tr>
<tr> <tr>
<td>Bind Operations</td> <td>Bind Operations</td>
<td><code>std::bind(<i>function</i>, <i>args</i>, ...)</code></td> <td><code>std::bind(<i>function</i>, <i>args</i>, ...)</code></td>
......
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