Skip to content

Commit

Permalink
Merge pull request #31 from apple1417/master
Browse files Browse the repository at this point in the history
add default constructors for iterators
  • Loading branch information
apple1417 authored Jan 4, 2024
2 parents f7c6d3f + f09949a commit fa5b705
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/unrealsdk/unreal/classes/ustruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace unrealsdk::unreal {

#pragma region Field Iterator

UStruct::FieldIterator::FieldIterator(void) : this_struct(nullptr), field(nullptr) {}
UStruct::FieldIterator::FieldIterator(const UStruct* this_struct, UField* field)
: this_struct(this_struct), field(field) {}

Expand Down Expand Up @@ -55,13 +56,14 @@ utils::IteratorProxy<UStruct::FieldIterator> UStruct::fields(void) const {
begin++;
}

return {begin, {nullptr, nullptr}};
return {begin, {}};
}

#pragma endregion

#pragma region Property Iterator

UStruct::PropertyIterator::PropertyIterator(void) : prop(nullptr) {}
UStruct::PropertyIterator::PropertyIterator(UProperty* prop) : prop(prop) {}

UStruct::PropertyIterator::reference UStruct::PropertyIterator::operator*() const {
Expand All @@ -86,13 +88,14 @@ bool UStruct::PropertyIterator::operator!=(const UStruct::PropertyIterator& rhs)
};

utils::IteratorProxy<UStruct::PropertyIterator> UStruct::properties(void) const {
return {{this->PropertyLink}, {nullptr}};
return {{this->PropertyLink}, {}};
}

#pragma endregion

#pragma region SuperField Iterator

UStruct::SuperFieldIterator::SuperFieldIterator(void) : this_struct(nullptr) {}
UStruct::SuperFieldIterator::SuperFieldIterator(const UStruct* this_struct)
: this_struct(this_struct) {}

Expand All @@ -118,7 +121,7 @@ bool UStruct::SuperFieldIterator::operator!=(const UStruct::SuperFieldIterator&
};

utils::IteratorProxy<UStruct::SuperFieldIterator> UStruct::superfields(void) const {
return {{this}, {nullptr}};
return {{this}, {}};
}

#pragma endregion
Expand Down
3 changes: 3 additions & 0 deletions src/unrealsdk/unreal/classes/ustruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class UStruct : public UField {
UField* field;

public:
FieldIterator(void);
FieldIterator(const UStruct* this_struct, UField* field);

reference operator*() const;
Expand All @@ -120,6 +121,7 @@ class UStruct : public UField {
UProperty* prop;

public:
PropertyIterator(void);
PropertyIterator(UProperty* prop);

reference operator*() const;
Expand All @@ -142,6 +144,7 @@ class UStruct : public UField {
const UStruct* this_struct;

public:
SuperFieldIterator(void);
SuperFieldIterator(const UStruct* this_struct);

reference operator*() const;
Expand Down
6 changes: 3 additions & 3 deletions src/unrealsdk/unreal/structs/tarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ struct TArray {
size_t idx{};

public:
Iterator(void) : arr(nullptr) {}
Iterator(const TArray<U>* arr) : arr(arr) {}

reference operator*() const { return (*this->arr)[this->idx]; };

Iterator& operator++() {
++this->idx;
// Use `arr == nullptr` as the end condition, so we behave a little better if the array
// grows during iteration - we can't guarantee control over this iterator as well as
// the others
// grows during iteration
if (this->idx >= (size_t)arr->count) {
arr = nullptr;
}
Expand Down Expand Up @@ -179,7 +179,7 @@ struct TArray {
template <typename U = T,
typename = std::enable_if_t<std::is_same_v<U, T> && std::negation_v<std::is_void<U>>>>
[[nodiscard]] static Iterator<U> end(void) {
return {nullptr};
return {};
}
#pragma endregion
};
Expand Down
20 changes: 12 additions & 8 deletions src/unrealsdk/unreal/wrappers/gobjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace unrealsdk::unreal {

#pragma region Iterator

GObjects::Iterator::Iterator(void) : gobjects(nullptr), idx(0) {}
GObjects::Iterator::Iterator(const GObjects& gobjects, size_t idx)
: gobjects(&gobjects), idx(idx) {}

Expand All @@ -24,18 +25,18 @@ GObjects::Iterator::reference GObjects::Iterator::operator*() const {

GObjects::Iterator& GObjects::Iterator::operator++() {
do {
// If we're on the last object, increment to max index
// If we're on the last object, set gobjects to null to end the iterator
if (this->idx >= (this->gobjects->size() - 1)) {
this->idx = std::numeric_limits<size_t>::max();
this->gobjects = nullptr;
break;
}

++this->idx;

// If this index points to a null object, increment again
// We really should handle gc'd object entries better (in UE4), but this is a quick hack to
// get the iterator mostly working. In practice, you really shouldn't be iterating through
// all objects anyway.
// We really should handle gc'd object entries better (in UE4), but this is a quick hack
// to get the iterator mostly working. In practice, you really shouldn't be iterating
// through all objects anyway.
} while (this->operator*() == nullptr);

return *this;
Expand All @@ -47,7 +48,10 @@ GObjects::Iterator GObjects::Iterator::operator++(int) {
}

bool GObjects::Iterator::operator==(const GObjects::Iterator& rhs) const {
return this->idx == rhs.idx;
if (this->gobjects == nullptr && rhs.gobjects == nullptr) {
return true;
}
return this->gobjects == rhs.gobjects && this->idx == rhs.idx;
};
bool GObjects::Iterator::operator!=(const GObjects::Iterator& rhs) const {
return !(*this == rhs);
Expand All @@ -57,8 +61,8 @@ GObjects::Iterator GObjects::begin(void) const {
return {*this, 0};
}

GObjects::Iterator GObjects::end(void) const {
return {*this, std::numeric_limits<size_t>::max()};
GObjects::Iterator GObjects::end(void) {
return {};
}

#pragma endregion
Expand Down
3 changes: 2 additions & 1 deletion src/unrealsdk/unreal/wrappers/gobjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GObjects {
size_t idx;

public:
Iterator(void);
Iterator(const GObjects& gobjects, size_t idx);

reference operator*() const;
Expand Down Expand Up @@ -83,7 +84,7 @@ class GObjects {
*
* @return The iterator.
*/
[[nodiscard]] Iterator end(void) const;
[[nodiscard]] static Iterator end(void);

/**
* @brief Get the object behind a weak object pointer (or null if it's invalid).
Expand Down

0 comments on commit fa5b705

Please sign in to comment.