| 219 | } |
| 220 | |
| 221 | void Array::assign(const Array &p_array) { |
| 222 | const ContainerTypeValidate &typed = _p->typed; |
| 223 | const ContainerTypeValidate &source_typed = p_array._p->typed; |
| 224 | |
| 225 | if (typed == source_typed || typed.type == Variant::NIL || (source_typed.type == Variant::OBJECT && typed.can_reference(source_typed))) { |
| 226 | // from same to same or |
| 227 | // from anything to variants or |
| 228 | // from subclasses to base classes |
| 229 | _p->array = p_array._p->array; |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | const Variant *source = p_array._p->array.ptr(); |
| 234 | int size = p_array._p->array.size(); |
| 235 | |
| 236 | if ((source_typed.type == Variant::NIL && typed.type == Variant::OBJECT) || (source_typed.type == Variant::OBJECT && source_typed.can_reference(typed))) { |
| 237 | // from variants to objects or |
| 238 | // from base classes to subclasses |
| 239 | for (int i = 0; i < size; i++) { |
| 240 | const Variant &element = source[i]; |
| 241 | if (element.get_type() != Variant::NIL && (element.get_type() != Variant::OBJECT || !typed.validate_object(element, "assign"))) { |
| 242 | ERR_FAIL_MSG(vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(element.get_type()), Variant::get_type_name(typed.type))); |
| 243 | } |
| 244 | } |
| 245 | _p->array = p_array._p->array; |
| 246 | return; |
| 247 | } |
| 248 | if (typed.type == Variant::OBJECT || source_typed.type == Variant::OBJECT) { |
| 249 | ERR_FAIL_MSG(vformat(R"(Cannot assign contents of "Array[%s]" to "Array[%s]".)", Variant::get_type_name(source_typed.type), Variant::get_type_name(typed.type))); |
| 250 | } |
| 251 | |
| 252 | Vector<Variant> array; |
| 253 | array.resize(size); |
| 254 | Variant *data = array.ptrw(); |
| 255 | |
| 256 | if (source_typed.type == Variant::NIL && typed.type != Variant::OBJECT) { |
| 257 | // from variants to primitives |
| 258 | for (int i = 0; i < size; i++) { |
| 259 | const Variant *value = source + i; |
| 260 | if (value->get_type() == typed.type) { |
| 261 | data[i] = *value; |
| 262 | continue; |
| 263 | } |
| 264 | if (!Variant::can_convert_strict(value->get_type(), typed.type)) { |
| 265 | ERR_FAIL_MSG(vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type))); |
| 266 | } |
| 267 | Callable::CallError ce; |
| 268 | Variant::construct(typed.type, data[i], &value, 1, ce); |
| 269 | ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type))); |
| 270 | } |
| 271 | } else if (Variant::can_convert_strict(source_typed.type, typed.type)) { |
| 272 | // from primitives to different convertible primitives |
| 273 | for (int i = 0; i < size; i++) { |
| 274 | const Variant *value = source + i; |
| 275 | Callable::CallError ce; |
| 276 | Variant::construct(typed.type, data[i], &value, 1, ce); |
| 277 | ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type))); |
| 278 | } |
no test coverage detected