Commit 0241e849 authored by Lukasz Anforowicz's avatar Lukasz Anforowicz Committed by Commit Bot

CheckedPtr support for *post*-increment and *post*-decrement operators.

This CL helps with:

1. Meeting one of the requirements that says "Ideal proposal should be
   as close to a drop-in replacement for raw pointers as possible"

2. Compiling a handful of places in Chromium that depend on these
   operators.  So far we know about:
     .../blink/renderer/core/css/parser/css_parser_token_range.h:45
     third_party/blink/renderer/platform/json/json_parser.cc:187
     third_party/blink/renderer/platform/json/json_parser.cc:85
     components/metrics/file_metrics_provider_unittest.cc:252

Bug: 1073933
Change-Id: Icb69c906dd1cfad2cc6242e81517969ff0da344c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2199177Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Bartek Nowierski <bartekn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#769607}
parent 0965220f
......@@ -159,6 +159,18 @@ class CheckedPtr {
return *this;
}
ALWAYS_INLINE CheckedPtr<T> operator++(int /* post_increment */) {
CheckedPtr<T> result = *this;
++(*this);
return result;
}
ALWAYS_INLINE CheckedPtr<T> operator--(int /* post_decrement */) {
CheckedPtr<T> result = *this;
--(*this);
return result;
}
ALWAYS_INLINE CheckedPtr& operator+=(ptrdiff_t delta_elems) {
wrapped_ptr_ = Impl::Advance(wrapped_ptr_, delta_elems * sizeof(T));
return *this;
......
......@@ -397,32 +397,49 @@ TEST(CheckedPtr, StdSwap) {
EXPECT_EQ(g_checked_ptr_swap_cnt, 0);
}
TEST(CheckedPtr, AdvanceIntArray) {
// operator++
TEST(CheckedPtr, PostIncrementOperator) {
int foo[] = {42, 43, 44, 45};
CheckedPtr<int> ptr = foo;
for (int i = 0; i < 4; ++i, ++ptr) {
ASSERT_EQ(*ptr, 42 + i);
for (int i = 0; i < 4; ++i) {
ASSERT_EQ(*ptr++, 42 + i);
}
ptr = &foo[1];
for (int i = 1; i < 4; ++i, ++ptr) {
}
TEST(CheckedPtr, PostDecrementOperator) {
int foo[] = {42, 43, 44, 45};
CheckedPtr<int> ptr = &foo[3];
for (int i = 3; i >= 0; --i) {
ASSERT_EQ(*ptr--, 42 + i);
}
}
TEST(CheckedPtr, PreIncrementOperator) {
int foo[] = {42, 43, 44, 45};
CheckedPtr<int> ptr = foo;
for (int i = 0; i < 4; ++i, ++ptr) {
ASSERT_EQ(*ptr, 42 + i);
}
}
// operator--
ptr = &foo[3];
TEST(CheckedPtr, PreDecrementOperator) {
int foo[] = {42, 43, 44, 45};
CheckedPtr<int> ptr = &foo[3];
for (int i = 3; i >= 0; --i, --ptr) {
ASSERT_EQ(*ptr, 42 + i);
}
}
// operator+=
ptr = foo;
TEST(CheckedPtr, PlusEqualsOperator) {
int foo[] = {42, 43, 44, 45};
CheckedPtr<int> ptr = foo;
for (int i = 0; i < 4; i += 2, ptr += 2) {
ASSERT_EQ(*ptr, 42 + i);
}
}
// operator-=
ptr = &foo[3];
TEST(CheckedPtr, PlusMinusOperator) {
int foo[] = {42, 43, 44, 45};
CheckedPtr<int> ptr = &foo[3];
for (int i = 3; i >= 0; i -= 2, ptr -= 2) {
ASSERT_EQ(*ptr, 42 + i);
}
......
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