Commit d787de63 authored by Bartek Nowierski's avatar Bartek Nowierski Committed by Commit Bot

Add operator== for nullptr_t

This allows to compare wrapped pointer directly against 0, saving us trouble to extract the pointer.

Bug: 1073933
Change-Id: I071ddff9aaea723c13fcb09acb91dc4c139d7554
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2223694Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Bartek Nowierski <bartekn@chromium.org>
Auto-Submit: Bartek Nowierski <bartekn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#773958}
parent 3e61366f
...@@ -415,7 +415,6 @@ class CheckedPtr { ...@@ -415,7 +415,6 @@ class CheckedPtr {
wrapped_ptr_ = Impl::WrapRawPtr(p); wrapped_ptr_ = Impl::WrapRawPtr(p);
return *this; return *this;
} }
ALWAYS_INLINE CheckedPtr& operator=(std::nullptr_t) noexcept { ALWAYS_INLINE CheckedPtr& operator=(std::nullptr_t) noexcept {
wrapped_ptr_ = Impl::GetWrappedNullPtr(); wrapped_ptr_ = Impl::GetWrappedNullPtr();
return *this; return *this;
...@@ -529,6 +528,21 @@ class CheckedPtr { ...@@ -529,6 +528,21 @@ class CheckedPtr {
friend ALWAYS_INLINE bool operator!=(U* lhs, const CheckedPtr& rhs) { friend ALWAYS_INLINE bool operator!=(U* lhs, const CheckedPtr& rhs) {
return rhs != lhs; // Reverse order to call the operator above. return rhs != lhs; // Reverse order to call the operator above.
} }
// Needed for comparisons against nullptr. Without these, a slightly more
// costly version would be called that extracts wrapped pointer, as opposed
// to plain comparison against 0.
friend ALWAYS_INLINE bool operator==(const CheckedPtr& lhs, nullptr_t) {
return !lhs;
}
friend ALWAYS_INLINE bool operator!=(const CheckedPtr& lhs, nullptr_t) {
return !!lhs; // Use !! otherwise the costly implicit cast will be used.
}
friend ALWAYS_INLINE bool operator==(nullptr_t, const CheckedPtr& rhs) {
return !rhs;
}
friend ALWAYS_INLINE bool operator!=(nullptr_t, const CheckedPtr& rhs) {
return !!rhs; // Use !! otherwise the costly implicit cast will be used.
}
friend ALWAYS_INLINE void swap(CheckedPtr& lhs, CheckedPtr& rhs) noexcept { friend ALWAYS_INLINE void swap(CheckedPtr& lhs, CheckedPtr& rhs) noexcept {
Impl::IncrementSwapCountForTest(); Impl::IncrementSwapCountForTest();
......
...@@ -137,6 +137,7 @@ TEST_F(CheckedPtrTest, NullArrowDereference) { ...@@ -137,6 +137,7 @@ TEST_F(CheckedPtrTest, NullArrowDereference) {
TEST_F(CheckedPtrTest, NullExtractNoDereference) { TEST_F(CheckedPtrTest, NullExtractNoDereference) {
CountingCheckedPtr<int> ptr = nullptr; CountingCheckedPtr<int> ptr = nullptr;
// No dereference hence shouldn't crash.
int* raw = ptr; int* raw = ptr;
std::ignore = raw; std::ignore = raw;
EXPECT_EQ(g_get_for_comparison_cnt, 0); EXPECT_EQ(g_get_for_comparison_cnt, 0);
...@@ -144,6 +145,28 @@ TEST_F(CheckedPtrTest, NullExtractNoDereference) { ...@@ -144,6 +145,28 @@ TEST_F(CheckedPtrTest, NullExtractNoDereference) {
EXPECT_EQ(g_get_for_dereference_cnt, 0); EXPECT_EQ(g_get_for_dereference_cnt, 0);
} }
TEST_F(CheckedPtrTest, NullCmpExplicit) {
CountingCheckedPtr<int> ptr = nullptr;
EXPECT_TRUE(ptr == nullptr);
EXPECT_TRUE(nullptr == ptr);
EXPECT_FALSE(ptr != nullptr);
EXPECT_FALSE(nullptr != ptr);
// No need to unwrap pointer, just compare against 0.
EXPECT_EQ(g_get_for_comparison_cnt, 0);
EXPECT_EQ(g_get_for_extraction_cnt, 0);
EXPECT_EQ(g_get_for_dereference_cnt, 0);
}
TEST_F(CheckedPtrTest, NullCmpBool) {
CountingCheckedPtr<int> ptr = nullptr;
EXPECT_FALSE(ptr);
EXPECT_TRUE(!ptr);
// No need to unwrap pointer, just compare against 0.
EXPECT_EQ(g_get_for_comparison_cnt, 0);
EXPECT_EQ(g_get_for_extraction_cnt, 0);
EXPECT_EQ(g_get_for_dereference_cnt, 0);
}
TEST_F(CheckedPtrTest, StarDereference) { TEST_F(CheckedPtrTest, StarDereference) {
int foo = 42; int foo = 42;
CountingCheckedPtr<int> ptr = &foo; CountingCheckedPtr<int> ptr = &foo;
......
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