diff options
Diffstat (limited to 'core/variant/array.cpp')
-rw-r--r-- | core/variant/array.cpp | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/core/variant/array.cpp b/core/variant/array.cpp index 9a2922a777..2fb2dd4a30 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -139,9 +139,9 @@ uint32_t Array::hash() const { return h; } -void Array::_assign(const Array &p_array) { +bool Array::_assign(const Array &p_array) { if (_p->typed.type != Variant::OBJECT && _p->typed.type == p_array._p->typed.type) { - //same type or untyped, just reference, shuold be fine + //same type or untyped, just reference, should be fine _ref(p_array); } else if (_p->typed.type == Variant::NIL) { //from typed to untyped, must copy, but this is cheap anyway _p->array = p_array._p->array; @@ -150,7 +150,7 @@ void Array::_assign(const Array &p_array) { //for objects, it needs full validation, either can be converted or fail for (int i = 0; i < p_array._p->array.size(); i++) { if (!_p->typed.validate(p_array._p->array[i], "assign")) { - return; + return false; } } _p->array = p_array._p->array; //then just copy, which is cheap anyway @@ -168,10 +168,10 @@ void Array::_assign(const Array &p_array) { Callable::CallError ce; Variant::construct(_p->typed.type, new_array.write[i], (const Variant **)&ptr, 1, ce); if (ce.error != Callable::CallError::CALL_OK) { - ERR_FAIL_MSG("Unable to convert array index " + itos(i) + " from '" + Variant::get_type_name(src_val.get_type()) + "' to '" + Variant::get_type_name(_p->typed.type) + "'."); + ERR_FAIL_V_MSG(false, "Unable to convert array index " + itos(i) + " from '" + Variant::get_type_name(src_val.get_type()) + "' to '" + Variant::get_type_name(_p->typed.type) + "'."); } } else { - ERR_FAIL_MSG("Unable to convert array index " + itos(i) + " from '" + Variant::get_type_name(src_val.get_type()) + "' to '" + Variant::get_type_name(_p->typed.type) + "'."); + ERR_FAIL_V_MSG(false, "Unable to convert array index " + itos(i) + " from '" + Variant::get_type_name(src_val.get_type()) + "' to '" + Variant::get_type_name(_p->typed.type) + "'."); } } @@ -180,12 +180,13 @@ void Array::_assign(const Array &p_array) { } else if (_p->typed.can_reference(p_array._p->typed)) { //same type or compatible _ref(p_array); } else { - ERR_FAIL_MSG("Assignment of arrays of incompatible types."); + ERR_FAIL_V_MSG(false, "Assignment of arrays of incompatible types."); } + return true; } void Array::operator=(const Array &p_array) { - _assign(p_array); + _ref(p_array); } void Array::push_back(const Variant &p_value) { @@ -207,6 +208,11 @@ void Array::insert(int p_pos, const Variant &p_value) { _p->array.insert(p_pos, p_value); } +void Array::fill(const Variant &p_value) { + ERR_FAIL_COND(!_p->typed.validate(p_value, "fill")); + _p->array.fill(p_value); +} + void Array::erase(const Variant &p_value) { ERR_FAIL_COND(!_p->typed.validate(p_value, "erase")); _p->array.erase(p_value); @@ -445,8 +451,8 @@ int Array::bsearch_custom(const Variant &p_value, Callable p_callable, bool p_be return bisect(_p->array, p_value, p_before, less); } -void Array::invert() { - _p->array.invert(); +void Array::reverse() { + _p->array.reverse(); } void Array::push_front(const Variant &p_value) { @@ -528,6 +534,10 @@ Array::Array(const Array &p_from, uint32_t p_type, const StringName &p_class_nam _assign(p_from); } +bool Array::typed_assign(const Array &p_other) { + return _assign(p_other); +} + void Array::set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script) { ERR_FAIL_COND_MSG(_p->array.size() > 0, "Type can only be set when array is empty."); ERR_FAIL_COND_MSG(_p->refcount.get() > 1, "Type can only be set when array has no more than one user."); @@ -542,6 +552,22 @@ void Array::set_typed(uint32_t p_type, const StringName &p_class_name, const Var _p->typed.where = "TypedArray"; } +bool Array::is_typed() const { + return _p->typed.type != Variant::NIL; +} + +uint32_t Array::get_typed_builtin() const { + return _p->typed.type; +} + +StringName Array::get_typed_class_name() const { + return _p->typed.class_name; +} + +Variant Array::get_typed_script() const { + return _p->typed.script; +} + Array::Array(const Array &p_from) { _p = nullptr; _ref(p_from); |