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 Oct 24, 2024
1 parent 8d9afa5 commit 12b3f56
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
11 changes: 11 additions & 0 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,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
17 changes: 10 additions & 7 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,6 @@ Variant *Dictionary::getptr(const Variant &p_key) {
}
}

void Dictionary::set(const Variant &p_key, const Variant &p_value) {
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
Variant key = p_key;
ERR_FAIL_COND(!_p->typed_key.validate(key, "set"));
operator[](key) = p_value;
}

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 Expand Up @@ -205,6 +198,16 @@ bool Dictionary::set(const Variant &p_key, const Variant &p_value) {
return true;
}

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);
Variant value = p_value;
ERR_FAIL_COND_V(!_p->typed_value.validate(value, "set_safe"), ERR_INVALID_PARAMETER);
operator[](key) = p_value;
return OK;
}

int Dictionary::size() const {
return _p->variant_map.size();
}
Expand Down
2 changes: 1 addition & 1 deletion core/variant/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class Dictionary {
const Variant *getptr(const Variant &p_key) const;
Variant *getptr(const Variant &p_key);

void set(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);
bool set(const Variant &p_key, const Variant &p_value);
Error set_safe(const Variant &p_key, const Variant &p_value);

int size() const;
bool is_empty() const;
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 12b3f56

Please sign in to comment.