1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

[Core] Add iteration support to Array

This commit is contained in:
A Thousand Ships
2023-12-24 13:44:21 +01:00
parent 1f0f81049f
commit 64146cb7f3
15 changed files with 251 additions and 73 deletions

View File

@@ -46,6 +46,70 @@ class Array {
void _unref() const;
public:
struct ConstIterator {
_FORCE_INLINE_ const Variant &operator*() const;
_FORCE_INLINE_ const Variant *operator->() const;
_FORCE_INLINE_ ConstIterator &operator++();
_FORCE_INLINE_ ConstIterator &operator--();
_FORCE_INLINE_ bool operator==(const ConstIterator &p_other) const { return element_ptr == p_other.element_ptr; }
_FORCE_INLINE_ bool operator!=(const ConstIterator &p_other) const { return element_ptr == p_other.element_ptr; }
_FORCE_INLINE_ ConstIterator(const Variant *p_element_ptr, Variant *p_read_only = nullptr) :
element_ptr(p_element_ptr), read_only(p_read_only) {}
_FORCE_INLINE_ ConstIterator() {}
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_other) :
element_ptr(p_other.element_ptr), read_only(p_other.read_only) {}
_FORCE_INLINE_ ConstIterator &operator=(const ConstIterator &p_other) {
element_ptr = p_other.element_ptr;
read_only = p_other.read_only;
return *this;
}
private:
const Variant *element_ptr = nullptr;
Variant *read_only = nullptr;
};
struct Iterator {
_FORCE_INLINE_ Variant &operator*() const;
_FORCE_INLINE_ Variant *operator->() const;
_FORCE_INLINE_ Iterator &operator++();
_FORCE_INLINE_ Iterator &operator--();
_FORCE_INLINE_ bool operator==(const Iterator &p_other) const { return element_ptr == p_other.element_ptr; }
_FORCE_INLINE_ bool operator!=(const Iterator &p_other) const { return element_ptr != p_other.element_ptr; }
_FORCE_INLINE_ Iterator(Variant *p_element_ptr, Variant *p_read_only = nullptr) :
element_ptr(p_element_ptr), read_only(p_read_only) {}
_FORCE_INLINE_ Iterator() {}
_FORCE_INLINE_ Iterator(const Iterator &p_other) :
element_ptr(p_other.element_ptr), read_only(p_other.read_only) {}
_FORCE_INLINE_ Iterator &operator=(const Iterator &p_other) {
element_ptr = p_other.element_ptr;
read_only = p_other.read_only;
return *this;
}
operator ConstIterator() const {
return ConstIterator(element_ptr, read_only);
}
private:
Variant *element_ptr = nullptr;
Variant *read_only = nullptr;
};
Iterator begin();
Iterator end();
ConstIterator begin() const;
ConstIterator end() const;
void _ref(const Array &p_from) const;
Variant &operator[](int p_idx);