Skip to content

Commit

Permalink
[Core] Add set_safe method to Array and Dictionary
Browse files Browse the repository at this point in the history
This returns an error, making it possible to check the behavior

Also adds range checking for `Array.set` to match `Vector` and allow it
to be use safely
  • Loading branch information
AThousandShips committed Sep 9, 2024
1 parent 040fe96 commit e426347
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,20 @@ void Array::set(int p_idx, const Variant &p_value) {
Variant value = p_value;
ERR_FAIL_COND(!_p->typed.validate(value, "set"));

ERR_FAIL_INDEX(p_idx, size());
operator[](p_idx) = value;
}

Error Array::set_safe(int p_idx, const Variant &p_value) {
ERR_FAIL_COND_V_MSG(_p->read_only, ERR_LOCKED, "Array is in read-only state.");
Variant value = p_value;
ERR_FAIL_COND_V(!_p->typed.validate(value, "set"), ERR_INVALID_PARAMETER);

ERR_FAIL_INDEX_V(p_idx, size(), ERR_PARAMETER_RANGE_ERROR);
operator[](p_idx) = value;
return OK;
}

const Variant &Array::get(int p_idx) const {
return operator[](p_idx);
}
Expand Down
1 change: 1 addition & 0 deletions core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Array {
const Variant &operator[](int p_idx) const;

void set(int p_idx, const Variant &p_value);
Error set_safe(int p_idx, const Variant &p_value);
const Variant &get(int p_idx) const;

int size() const;
Expand Down
8 changes: 8 additions & 0 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ void Dictionary::set(const Variant &p_key, const Variant &p_value) {
operator[](key) = p_value;
}

Error Dictionary::set_safe(const Variant &p_key, const Variant &p_value) {
ERR_FAIL_COND_V_MSG(_p->read_only, ERR_LOCKED, "Dictionary is in read-only state.");
Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "set_safe"), ERR_INVALID_PARAMETER);
operator[](key) = p_value;
return OK;
}

Variant Dictionary::get_valid(const Variant &p_key) const {
Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get_valid"), Variant());
Expand Down
1 change: 1 addition & 0 deletions core/variant/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Dictionary {
Variant *getptr(const Variant &p_key);

void set(const Variant &p_key, const Variant &p_value);
Error set_safe(const Variant &p_key, const Variant &p_value);
Variant get_valid(const Variant &p_key) const;
Variant get(const Variant &p_key, const Variant &p_default) const;
Variant get_or_add(const Variant &p_key, const Variant &p_default);
Expand Down
3 changes: 3 additions & 0 deletions tests/core/variant/test_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
map.set("This key does not exist", String());
CHECK_FALSE(map.has("This key does not exist"));
CHECK(map.size() == length);

CHECK(map.set_safe("This key does not exist", String()) == ERR_LOCKED);
CHECK(map.size() == length);
ERR_PRINT_ON;
}

Expand Down

0 comments on commit e426347

Please sign in to comment.