Skip to content

Commit 6dfe5de

Browse files
committed
Merge pull request #111158 from WhalesState/trivial-itr
Refactor `Array` iterators to be trivially copyable.
2 parents d9ba9ba + 9ff5325 commit 6dfe5de

File tree

2 files changed

+10
-31
lines changed

2 files changed

+10
-31
lines changed

core/variant/array.h

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,24 @@ class Array {
5151

5252
public:
5353
struct ConstIterator {
54-
_FORCE_INLINE_ const Variant &operator*() const;
55-
_FORCE_INLINE_ const Variant *operator->() const;
54+
_FORCE_INLINE_ const Variant &operator*() const { return *element_ptr; }
55+
_FORCE_INLINE_ const Variant *operator->() const { return element_ptr; }
5656

5757
_FORCE_INLINE_ ConstIterator &operator++();
5858
_FORCE_INLINE_ ConstIterator &operator--();
5959

6060
_FORCE_INLINE_ bool operator==(const ConstIterator &p_other) const { return element_ptr == p_other.element_ptr; }
6161
_FORCE_INLINE_ bool operator!=(const ConstIterator &p_other) const { return element_ptr != p_other.element_ptr; }
6262

63+
ConstIterator() = default;
64+
6365
_FORCE_INLINE_ ConstIterator(const Variant *p_element_ptr) :
6466
element_ptr(p_element_ptr) {}
65-
_FORCE_INLINE_ ConstIterator() {}
66-
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_other) :
67-
element_ptr(p_other.element_ptr) {}
68-
69-
_FORCE_INLINE_ ConstIterator &operator=(const ConstIterator &p_other) {
70-
element_ptr = p_other.element_ptr;
71-
return *this;
72-
}
7367

7468
private:
7569
const Variant *element_ptr = nullptr;
7670
};
71+
static_assert(std::is_trivially_copyable_v<ConstIterator>, "ConstIterator must be trivially copyable");
7772

7873
struct Iterator {
7974
_FORCE_INLINE_ Variant &operator*() const;
@@ -85,26 +80,18 @@ class Array {
8580
_FORCE_INLINE_ bool operator==(const Iterator &p_other) const { return element_ptr == p_other.element_ptr; }
8681
_FORCE_INLINE_ bool operator!=(const Iterator &p_other) const { return element_ptr != p_other.element_ptr; }
8782

88-
_FORCE_INLINE_ Iterator(Variant *p_element_ptr, Variant *p_read_only = nullptr) :
89-
element_ptr(p_element_ptr), read_only(p_read_only) {}
90-
_FORCE_INLINE_ Iterator() {}
91-
_FORCE_INLINE_ Iterator(const Iterator &p_other) :
92-
element_ptr(p_other.element_ptr), read_only(p_other.read_only) {}
83+
_FORCE_INLINE_ operator ConstIterator() const { return ConstIterator(element_ptr); }
9384

94-
_FORCE_INLINE_ Iterator &operator=(const Iterator &p_other) {
95-
element_ptr = p_other.element_ptr;
96-
read_only = p_other.read_only;
97-
return *this;
98-
}
85+
Iterator() = default;
9986

100-
operator ConstIterator() const {
101-
return ConstIterator(element_ptr);
102-
}
87+
_FORCE_INLINE_ Iterator(Variant *p_element_ptr, Variant *p_read_only = nullptr) :
88+
element_ptr(p_element_ptr), read_only(p_read_only) {}
10389

10490
private:
10591
Variant *element_ptr = nullptr;
10692
Variant *read_only = nullptr;
10793
};
94+
static_assert(std::is_trivially_copyable_v<Iterator>, "Iterator must be trivially copyable");
10895

10996
Iterator begin();
11097
Iterator end();

core/variant/variant.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -974,14 +974,6 @@ Array::Iterator &Array::Iterator::operator--() {
974974
return *this;
975975
}
976976

977-
const Variant &Array::ConstIterator::operator*() const {
978-
return *element_ptr;
979-
}
980-
981-
const Variant *Array::ConstIterator::operator->() const {
982-
return element_ptr;
983-
}
984-
985977
Array::ConstIterator &Array::ConstIterator::operator++() {
986978
element_ptr++;
987979
return *this;

0 commit comments

Comments
 (0)