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

Switch to default move constructor and operator=

Change-Id: Iba4b711d600fa25a4207b917cc32933a2787eaf1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2124342
Commit-Queue: Bartek Nowierski <bartekn@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754421}
parent a26a073d
...@@ -992,9 +992,9 @@ class NewLinkedHashSet { ...@@ -992,9 +992,9 @@ class NewLinkedHashSet {
NewLinkedHashSet(); NewLinkedHashSet();
NewLinkedHashSet(const NewLinkedHashSet&) = default; NewLinkedHashSet(const NewLinkedHashSet&) = default;
NewLinkedHashSet(NewLinkedHashSet&&); NewLinkedHashSet(NewLinkedHashSet&&) = default;
NewLinkedHashSet& operator=(const NewLinkedHashSet&) = default; NewLinkedHashSet& operator=(const NewLinkedHashSet&) = default;
NewLinkedHashSet& operator=(NewLinkedHashSet&&); NewLinkedHashSet& operator=(NewLinkedHashSet&&) = default;
~NewLinkedHashSet() = default; ~NewLinkedHashSet() = default;
...@@ -1080,19 +1080,6 @@ inline NewLinkedHashSet<T, Allocator>::NewLinkedHashSet() { ...@@ -1080,19 +1080,6 @@ inline NewLinkedHashSet<T, Allocator>::NewLinkedHashSet() {
"HeapNewLinkedHashSet<Member<T>> instead."); "HeapNewLinkedHashSet<Member<T>> instead.");
} }
template <typename T, typename Allocator>
inline NewLinkedHashSet<T, Allocator>::NewLinkedHashSet(
NewLinkedHashSet&& other) {
Swap(other);
}
template <typename T, typename Allocator>
inline NewLinkedHashSet<T, Allocator>& NewLinkedHashSet<T, Allocator>::
operator=(NewLinkedHashSet&& other) {
Swap(other);
return *this;
}
template <typename T, typename Allocator> template <typename T, typename Allocator>
inline void NewLinkedHashSet<T, Allocator>::Swap(NewLinkedHashSet& other) { inline void NewLinkedHashSet<T, Allocator>::Swap(NewLinkedHashSet& other) {
value_to_index_.swap(other.value_to_index_); value_to_index_.swap(other.value_to_index_);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h" #include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/wtf_test_helper.h"
namespace WTF { namespace WTF {
...@@ -108,6 +109,78 @@ TEST(NewLinkedHashSetTest, CopyConstructAndAssignString) { ...@@ -108,6 +109,78 @@ TEST(NewLinkedHashSetTest, CopyConstructAndAssignString) {
} }
} }
TEST(NewLinkedHashSetTest, MoveConstructAndAssignInt) {
NewLinkedHashSet<ValueInstanceCount<int>> set1;
EXPECT_EQ(set1.size(), 0u);
EXPECT_TRUE(set1.IsEmpty());
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
set1.insert(ValueInstanceCount<int>(&counter1, 1));
set1.insert(ValueInstanceCount<int>(&counter2, 2));
set1.insert(ValueInstanceCount<int>(&counter3, 3));
EXPECT_EQ(set1.size(), 3u);
NewLinkedHashSet<ValueInstanceCount<int>> set2(std::move(set1));
EXPECT_EQ(set2.size(), 3u);
NewLinkedHashSet<ValueInstanceCount<int>> set3;
EXPECT_EQ(set3.size(), 0u);
set3 = std::move(set2);
EXPECT_EQ(set3.size(), 3u);
auto it = set3.begin();
for (int i = 0; i < 3; i++) {
EXPECT_EQ(it->Value(), i + 1);
++it;
}
// Only move constructors were used, each object is only in set3.
// Count 2x because each set uses hash map and vector.
EXPECT_EQ(counter1, 2);
EXPECT_EQ(counter2, 2);
EXPECT_EQ(counter3, 2);
NewLinkedHashSet<ValueInstanceCount<int>> set4(set3);
// Copy constructor was used, each object is in set3 and set4.
EXPECT_EQ(counter1, 4);
EXPECT_EQ(counter2, 4);
EXPECT_EQ(counter3, 4);
}
TEST(NewLinkedHashSetTest, MoveConstructAndAssignString) {
NewLinkedHashSet<ValueInstanceCount<String>> set1;
EXPECT_EQ(set1.size(), 0u);
EXPECT_TRUE(set1.IsEmpty());
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
set1.insert(ValueInstanceCount<String>(&counter1, "1"));
set1.insert(ValueInstanceCount<String>(&counter2, "2"));
set1.insert(ValueInstanceCount<String>(&counter3, "3"));
EXPECT_EQ(set1.size(), 3u);
NewLinkedHashSet<ValueInstanceCount<String>> set2(std::move(set1));
EXPECT_EQ(set2.size(), 3u);
NewLinkedHashSet<ValueInstanceCount<String>> set3;
EXPECT_EQ(set3.size(), 0u);
set3 = std::move(set2);
EXPECT_EQ(set3.size(), 3u);
auto it = set3.begin();
for (int i = 0; i < 3; i++) {
EXPECT_EQ(it->Value(), String(Vector<UChar>({'1' + i})));
++it;
}
// Only move constructors were used, each object is only in set3.
// Count 2x because each set uses hash map and vector.
EXPECT_EQ(counter1, 2);
EXPECT_EQ(counter2, 2);
EXPECT_EQ(counter3, 2);
NewLinkedHashSet<ValueInstanceCount<String>> set4(set3);
// Copy constructor was used, each object is in set3 and set4.
EXPECT_EQ(counter1, 4);
EXPECT_EQ(counter2, 4);
EXPECT_EQ(counter3, 4);
}
TEST(NewLinkedHashSetTest, Iterator) { TEST(NewLinkedHashSetTest, Iterator) {
using Set = NewLinkedHashSet<int>; using Set = NewLinkedHashSet<int>;
Set set; Set set;
......
...@@ -117,9 +117,9 @@ class VectorBackedLinkedList { ...@@ -117,9 +117,9 @@ class VectorBackedLinkedList {
VectorBackedLinkedList(); VectorBackedLinkedList();
VectorBackedLinkedList(const VectorBackedLinkedList&) = default; VectorBackedLinkedList(const VectorBackedLinkedList&) = default;
VectorBackedLinkedList(VectorBackedLinkedList&&); VectorBackedLinkedList(VectorBackedLinkedList&&) = default;
VectorBackedLinkedList& operator=(const VectorBackedLinkedList&) = default; VectorBackedLinkedList& operator=(const VectorBackedLinkedList&) = default;
VectorBackedLinkedList& operator=(VectorBackedLinkedList&&); VectorBackedLinkedList& operator=(VectorBackedLinkedList&&) = default;
~VectorBackedLinkedList() = default; ~VectorBackedLinkedList() = default;
...@@ -468,20 +468,6 @@ VectorBackedLinkedList<T, Allocator>::VectorBackedLinkedList() { ...@@ -468,20 +468,6 @@ VectorBackedLinkedList<T, Allocator>::VectorBackedLinkedList() {
nodes_.push_back(Node(anchor_index_, anchor_index_)); nodes_.push_back(Node(anchor_index_, anchor_index_));
} }
template <typename T, typename Allocator>
inline VectorBackedLinkedList<T, Allocator>::VectorBackedLinkedList(
VectorBackedLinkedList&& other) {
swap(other);
}
template <typename T, typename Allocator>
inline VectorBackedLinkedList<T, Allocator>&
VectorBackedLinkedList<T, Allocator>::operator=(
VectorBackedLinkedList&& other) {
swap(other);
return *this;
}
template <typename T, typename Allocator> template <typename T, typename Allocator>
inline void VectorBackedLinkedList<T, Allocator>::swap( inline void VectorBackedLinkedList<T, Allocator>::swap(
VectorBackedLinkedList& other) { VectorBackedLinkedList& other) {
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_WTF_TEST_HELPER_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_WTF_TEST_HELPER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_WTF_TEST_HELPER_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_WTF_TEST_HELPER_H_
#include <type_traits>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -177,6 +179,75 @@ struct DefaultHash<CountCopy> { ...@@ -177,6 +179,75 @@ struct DefaultHash<CountCopy> {
using Hash = CountCopyHash; using Hash = CountCopyHash;
}; };
template <typename T>
class ValueInstanceCount final {
public:
ValueInstanceCount() : counter_(nullptr), value_(T()) {}
explicit ValueInstanceCount(int* counter, T value = T())
: counter_(counter), value_(value) {
DCHECK(counter_);
*counter = 1;
}
ValueInstanceCount(const ValueInstanceCount& other)
: counter_(other.counter_), value_(other.value_) {
if (counter_)
++*counter_;
}
ValueInstanceCount& operator=(const ValueInstanceCount& other) {
counter_ = other.counter_;
value_ = other.value_;
if (counter_)
++*counter_;
return *this;
}
~ValueInstanceCount() {
if (counter_)
--*counter_;
}
const int* Counter() const { return counter_; }
const T& Value() const { return value_; }
private:
int* counter_;
T value_;
};
template <typename T>
struct ValueInstanceCountHashTraits
: public GenericHashTraits<ValueInstanceCount<T>> {
static const bool kEmptyValueIsZero = false;
static const bool kHasIsEmptyValueFunction = true;
static bool IsEmptyValue(const ValueInstanceCount<T>& value) {
return !value.Counter();
}
static void ConstructDeletedValue(ValueInstanceCount<T>& slot, bool) {}
static bool IsDeletedValue(const ValueInstanceCount<T>& value) {
return false;
}
};
template <typename T>
struct ValueInstanceCountHash : public PtrHash<const int*> {
static unsigned GetHash(const ValueInstanceCount<T>& value) {
return PtrHash<const int>::GetHash(value.Counter());
}
static bool Equal(const ValueInstanceCount<T>& left,
const ValueInstanceCount<T>& right) {
return PtrHash<const int>::Equal(left.Counter(), right.Counter());
}
static const bool safe_to_compare_to_empty_or_deleted = true;
};
template <typename T>
struct HashTraits<ValueInstanceCount<T>>
: public ValueInstanceCountHashTraits<T> {};
template <typename T>
struct DefaultHash<ValueInstanceCount<T>> {
using Hash = ValueInstanceCountHash<T>;
};
class DummyRefCounted : public RefCounted<DummyRefCounted> { class DummyRefCounted : public RefCounted<DummyRefCounted> {
public: public:
DummyRefCounted(bool& is_deleted) : is_deleted_(is_deleted) { DummyRefCounted(bool& is_deleted) : is_deleted_(is_deleted) {
......
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