Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for range based loops for TypedArray #1717

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("\tVariant &operator[](int64_t p_index);")
result.append("\tvoid set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);")
result.append("\tvoid _ref(const Array &p_from) const;")
result.append("\tconst Variant *ptr() const;")
result.append("\tVariant *ptrw();")

if class_name == "Dictionary":
result.append("\tconst Variant &operator[](const Variant &p_key) const;")
Expand Down
93 changes: 93 additions & 0 deletions include/godot_cpp/variant/typed_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,90 @@

namespace godot {

struct ConstIterator {
_FORCE_INLINE_ const Variant &operator*() const { return *element_ptr; }
_FORCE_INLINE_ const Variant *operator->() const { return element_ptr; }

_FORCE_INLINE_ ConstIterator &operator++() {
element_ptr++;
return *this;
}
_FORCE_INLINE_ ConstIterator &operator--() {
element_ptr--;
return *this;
}

_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) :
element_ptr(p_element_ptr) {}
_FORCE_INLINE_ ConstIterator() {}
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_other) :
element_ptr(p_other.element_ptr) {}

_FORCE_INLINE_ ConstIterator &operator=(const ConstIterator &p_other) {
element_ptr = p_other.element_ptr;
return *this;
}

private:
const Variant *element_ptr = nullptr;
};

struct Iterator {
_FORCE_INLINE_ Variant &operator*() const { return *element_ptr; }
_FORCE_INLINE_ Variant *operator->() const { return element_ptr; }

_FORCE_INLINE_ Iterator &operator++() {
element_ptr++;
return *this;
}
_FORCE_INLINE_ Iterator &operator--() {
element_ptr--;
return *this;
}

_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) :
element_ptr(p_element_ptr) {}
_FORCE_INLINE_ Iterator() {}
_FORCE_INLINE_ Iterator(const Iterator &p_other) :
element_ptr(p_other.element_ptr) {}

_FORCE_INLINE_ Iterator &operator=(const Iterator &p_other) {
element_ptr = p_other.element_ptr;
return *this;
}

operator ConstIterator() const {
return ConstIterator(element_ptr);
}

private:
Variant *element_ptr = nullptr;
};

template <typename T>
class TypedArray : public Array {
public:
Iterator begin() {
return Iterator(ptrw());
}

Iterator end() {
return Iterator(ptrw() + size());
}

ConstIterator begin() const {
return ConstIterator(ptr());
}
ConstIterator end() const {
return ConstIterator(ptr() + size());
}

_FORCE_INLINE_ void operator=(const Array &p_array) {
ERR_FAIL_COND_MSG(!is_same_typed(p_array), "Cannot assign an array with a different element type.");
_ref(p_array);
Expand All @@ -65,6 +146,18 @@ class TypedArray : public Array {
template <> \
class TypedArray<m_type> : public Array { \
public: \
Iterator begin() { \
return Iterator(ptrw()); \
} \
Iterator end() { \
return Iterator(ptrw() + size()); \
} \
ConstIterator begin() const { \
return ConstIterator(ptr()); \
} \
ConstIterator end() const { \
return ConstIterator(ptr() + size()); \
} \
_FORCE_INLINE_ void operator=(const Array &p_array) { \
ERR_FAIL_COND_MSG(!is_same_typed(p_array), "Cannot assign an array with a different element type."); \
_ref(p_array); \
Expand Down
8 changes: 8 additions & 0 deletions src/variant/packed_arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ void Array::_ref(const Array &p_from) const {
internal::gdextension_interface_array_ref((GDExtensionTypePtr *)this, (GDExtensionConstTypePtr *)&p_from);
}

const Variant *Array::ptr() const {
return (const Variant *)internal::gdextension_interface_array_operator_index_const((GDExtensionTypePtr *)this, 0);
}

Variant *Array::ptrw() {
return (Variant *)internal::gdextension_interface_array_operator_index((GDExtensionTypePtr *)this, 0);
}

const Variant &Dictionary::operator[](const Variant &p_key) const {
const Variant *var = (const Variant *)internal::gdextension_interface_dictionary_operator_index_const((GDExtensionTypePtr *)this, (GDExtensionVariantPtr)&p_key);
return *var;
Expand Down