From ec650a2f09ee2a91f48499464ab1462cb1e25eac Mon Sep 17 00:00:00 2001 From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:13:44 +0200 Subject: [PATCH] [Core,Drivers] Improve use of `Ref.is_null/valid` Use `is_null` over `!is_valid` and vice versa. --- core/input/input_map.cpp | 2 +- core/io/image.cpp | 4 ++-- core/io/packet_peer_udp.cpp | 14 +++++++------- core/io/plist.cpp | 4 ++-- core/io/remote_filesystem_client.cpp | 2 +- core/io/resource.cpp | 6 +++--- core/io/resource_format_binary.cpp | 4 ++-- core/io/resource_loader.cpp | 8 ++++---- core/io/stream_peer_tcp.cpp | 10 +++++----- core/io/tcp_server.cpp | 8 ++++---- core/io/udp_server.cpp | 8 ++++---- core/object/object.cpp | 4 ++-- drivers/gles3/storage/texture_storage.cpp | 2 +- drivers/png/resource_saver_png.cpp | 2 +- 14 files changed, 39 insertions(+), 39 deletions(-) diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp index 6378f185450..4a2cc341aea 100644 --- a/core/input/input_map.cpp +++ b/core/input/input_map.cpp @@ -156,7 +156,7 @@ List InputMap::get_actions() const { } List>::Element *InputMap::_find_event(Action &p_action, const Ref &p_event, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const { - ERR_FAIL_COND_V(!p_event.is_valid(), nullptr); + ERR_FAIL_COND_V(p_event.is_null(), nullptr); int i = 0; for (List>::Element *E = p_action.inputs.front(); E; E = E->next()) { diff --git a/core/io/image.cpp b/core/io/image.cpp index 9b5bb058ef3..155940b3559 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -4057,7 +4057,7 @@ Error Image::load_svg_from_buffer(const Vector &p_array, float scale) { ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); Ref image = _svg_scalable_mem_loader_func(p_array.ptr(), buffer_size, scale); - ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR); + ERR_FAIL_COND_V(image.is_null(), ERR_PARSE_ERROR); copy_internals_from(image); @@ -4124,7 +4124,7 @@ Error Image::_load_from_buffer(const Vector &p_array, ImageMemLoadFunc const uint8_t *r = p_array.ptr(); Ref image = p_loader(r, buffer_size); - ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR); + ERR_FAIL_COND_V(image.is_null(), ERR_PARSE_ERROR); copy_internals_from(image); diff --git a/core/io/packet_peer_udp.cpp b/core/io/packet_peer_udp.cpp index fae3de2a982..5255791b577 100644 --- a/core/io/packet_peer_udp.cpp +++ b/core/io/packet_peer_udp.cpp @@ -47,7 +47,7 @@ void PacketPeerUDP::set_broadcast_enabled(bool p_enabled) { Error PacketPeerUDP::join_multicast_group(IPAddress p_multi_address, const String &p_if_name) { ERR_FAIL_COND_V(udp_server, ERR_LOCKED); - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!p_multi_address.is_valid(), ERR_INVALID_PARAMETER); if (!_sock->is_open()) { @@ -62,7 +62,7 @@ Error PacketPeerUDP::join_multicast_group(IPAddress p_multi_address, const Strin Error PacketPeerUDP::leave_multicast_group(IPAddress p_multi_address, const String &p_if_name) { ERR_FAIL_COND_V(udp_server, ERR_LOCKED); - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!_sock->is_open(), ERR_UNCONFIGURED); return _sock->leave_multicast_group(p_multi_address, p_if_name); } @@ -119,7 +119,7 @@ Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { } Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED); Error err; @@ -160,7 +160,7 @@ int PacketPeerUDP::get_max_packet_size() const { } Error PacketPeerUDP::bind(int p_port, const IPAddress &p_bind_address, int p_recv_buffer_size) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V_MSG(p_port < 0 || p_port > 65535, ERR_INVALID_PARAMETER, "The local port number must be between 0 and 65535 (inclusive)."); @@ -209,7 +209,7 @@ void PacketPeerUDP::disconnect_shared_socket() { Error PacketPeerUDP::connect_to_host(const IPAddress &p_host, int p_port) { ERR_FAIL_COND_V(udp_server, ERR_LOCKED); - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V_MSG(p_port < 1 || p_port > 65535, ERR_INVALID_PARAMETER, "The remote port number must be between 1 and 65535 (inclusive)."); @@ -260,12 +260,12 @@ void PacketPeerUDP::close() { } Error PacketPeerUDP::wait() { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); return _sock->poll(NetSocket::POLL_TYPE_IN, -1); } Error PacketPeerUDP::_poll() { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); if (!_sock->is_open()) { return FAILED; diff --git a/core/io/plist.cpp b/core/io/plist.cpp index 32e83c31f2d..c9bd13707d7 100644 --- a/core/io/plist.cpp +++ b/core/io/plist.cpp @@ -708,7 +708,7 @@ bool PList::load_string(const String &p_string, String &r_err_out) { stack.push_back(dict); } else { // Add root node. - if (!root.is_null()) { + if (root.is_valid()) { r_err_out = "Root node already set."; return false; } @@ -740,7 +740,7 @@ bool PList::load_string(const String &p_string, String &r_err_out) { stack.push_back(arr); } else { // Add root node. - if (!root.is_null()) { + if (root.is_valid()) { r_err_out = "Root node already set."; return false; } diff --git a/core/io/remote_filesystem_client.cpp b/core/io/remote_filesystem_client.cpp index c3f9a0016cd..3c2aa64c537 100644 --- a/core/io/remote_filesystem_client.cpp +++ b/core/io/remote_filesystem_client.cpp @@ -44,7 +44,7 @@ Vector RemoteFilesystemClient::_load_cache_file() { Ref fa = FileAccess::open(cache_path.path_join(FILES_CACHE_FILE), FileAccess::READ); - if (!fa.is_valid()) { + if (fa.is_null()) { return Vector(); // No cache, return empty } diff --git a/core/io/resource.cpp b/core/io/resource.cpp index c65484b6c61..1dff3d4f996 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -235,7 +235,7 @@ void Resource::reload_from_file() { Ref s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); - if (!s.is_valid()) { + if (s.is_null()) { return; } @@ -655,7 +655,7 @@ Ref ResourceCache::get_ref(const String &p_path) { ref = Ref(*res); } - if (res && !ref.is_valid()) { + if (res && ref.is_null()) { // This resource is in the process of being deleted, ignore its existence (*res)->path_cache = String(); resources.erase(p_path); @@ -674,7 +674,7 @@ void ResourceCache::get_cached_resources(List> *p_resources) { for (KeyValue &E : resources) { Ref ref = Ref(E.value); - if (!ref.is_valid()) { + if (ref.is_null()) { // This resource is in the process of being deleted, ignore its existence E.value->path_cache = String(); to_remove.push_back(E.key); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 8bfa91a220e..98262f4ef13 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -699,7 +699,7 @@ Error ResourceLoaderBinary::load() { external_resources.write[i].path = path; //remap happens here, not on load because on load it can actually be used for filesystem dock resource remap external_resources.write[i].load_token = ResourceLoader::_load_start(path, external_resources[i].type, use_sub_threads ? ResourceLoader::LOAD_THREAD_DISTRIBUTE : ResourceLoader::LOAD_THREAD_FROM_CURRENT, cache_mode_for_external); - if (!external_resources[i].load_token.is_valid()) { + if (external_resources[i].load_token.is_null()) { if (!ResourceLoader::get_abort_on_missing_resources()) { ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type); } else { @@ -1389,7 +1389,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT); Ref res = loader.get_resource(); - ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT); + ERR_FAIL_COND_V(res.is_null(), ERR_FILE_CORRUPT); return ResourceFormatSaverBinary::singleton->save(res, p_path); } diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index c8c7d430ccd..904f29c3a8b 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -290,7 +290,7 @@ Ref ResourceLoader::_load(const String &p_path, const String &p_origin } found = true; res = loader[i]->load(p_path, original_path, r_error, p_use_sub_threads, r_progress, p_cache_mode); - if (!res.is_null()) { + if (res.is_valid()) { break; } } @@ -299,7 +299,7 @@ Ref ResourceLoader::_load(const String &p_path, const String &p_origin res_ref_overrides.erase(load_nesting); load_nesting--; - if (!res.is_null()) { + if (res.is_valid()) { return res; } @@ -514,7 +514,7 @@ Ref ResourceLoader::load(const String &p_path, const String &p_type_hi thread_mode = LOAD_THREAD_SPAWN_SINGLE; } Ref load_token = _load_start(p_path, p_type_hint, thread_mode, p_cache_mode); - if (!load_token.is_valid()) { + if (load_token.is_null()) { if (r_error) { *r_error = FAILED; } @@ -934,7 +934,7 @@ Ref ResourceLoader::ensure_resource_ref_override_for_outer_load(const Object *obj = ClassDB::instantiate(p_res_type); ERR_FAIL_NULL_V(obj, Ref()); Ref res(obj); - if (!res.is_valid()) { + if (res.is_null()) { memdelete(obj); ERR_FAIL_V(Ref()); } diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index 90a8f49a758..1ad38cbb46e 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -89,7 +89,7 @@ void StreamPeerTCP::accept_socket(Ref p_sock, IPAddress p_host, uint1 } Error StreamPeerTCP::bind(int p_port, const IPAddress &p_host) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V_MSG(p_port < 0 || p_port > 65535, ERR_INVALID_PARAMETER, "The local port number must be between 0 and 65535 (inclusive)."); @@ -106,7 +106,7 @@ Error StreamPeerTCP::bind(int p_port, const IPAddress &p_host) { } Error StreamPeerTCP::connect_to_host(const IPAddress &p_host, int p_port) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(status != STATUS_NONE, ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V_MSG(p_port < 1 || p_port > 65535, ERR_INVALID_PARAMETER, "The remote port number must be between 1 and 65535 (inclusive)."); @@ -140,7 +140,7 @@ Error StreamPeerTCP::connect_to_host(const IPAddress &p_host, int p_port) { } Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); if (status != STATUS_CONNECTED) { return FAILED; @@ -238,7 +238,7 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool } void StreamPeerTCP::set_no_delay(bool p_enabled) { - ERR_FAIL_COND(!_sock.is_valid() || !_sock->is_open()); + ERR_FAIL_COND(_sock.is_null() || !_sock->is_open()); _sock->set_tcp_no_delay_enabled(p_enabled); } @@ -281,7 +281,7 @@ Error StreamPeerTCP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_rec } int StreamPeerTCP::get_available_bytes() const { - ERR_FAIL_COND_V(!_sock.is_valid(), -1); + ERR_FAIL_COND_V(_sock.is_null(), -1); return _sock->get_available_bytes(); } diff --git a/core/io/tcp_server.cpp b/core/io/tcp_server.cpp index f2b3d5e56a7..04b550572d4 100644 --- a/core/io/tcp_server.cpp +++ b/core/io/tcp_server.cpp @@ -40,7 +40,7 @@ void TCPServer::_bind_methods() { } Error TCPServer::listen(uint16_t p_port, const IPAddress &p_bind_address) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); @@ -82,13 +82,13 @@ int TCPServer::get_local_port() const { } bool TCPServer::is_listening() const { - ERR_FAIL_COND_V(!_sock.is_valid(), false); + ERR_FAIL_COND_V(_sock.is_null(), false); return _sock->is_open(); } bool TCPServer::is_connection_available() const { - ERR_FAIL_COND_V(!_sock.is_valid(), false); + ERR_FAIL_COND_V(_sock.is_null(), false); if (!_sock->is_open()) { return false; @@ -108,7 +108,7 @@ Ref TCPServer::take_connection() { IPAddress ip; uint16_t port = 0; ns = _sock->accept(ip, port); - if (!ns.is_valid()) { + if (ns.is_null()) { return conn; } diff --git a/core/io/udp_server.cpp b/core/io/udp_server.cpp index 75ba784dbd1..f2fce3b292b 100644 --- a/core/io/udp_server.cpp +++ b/core/io/udp_server.cpp @@ -44,7 +44,7 @@ void UDPServer::_bind_methods() { } Error UDPServer::poll() { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); if (!_sock->is_open()) { return ERR_UNCONFIGURED; } @@ -88,7 +88,7 @@ Error UDPServer::poll() { } Error UDPServer::listen(uint16_t p_port, const IPAddress &p_bind_address) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); @@ -123,13 +123,13 @@ int UDPServer::get_local_port() const { } bool UDPServer::is_listening() const { - ERR_FAIL_COND_V(!_sock.is_valid(), false); + ERR_FAIL_COND_V(_sock.is_null(), false); return _sock->is_open(); } bool UDPServer::is_connection_available() const { - ERR_FAIL_COND_V(!_sock.is_valid(), false); + ERR_FAIL_COND_V(_sock.is_null(), false); if (!_sock->is_open()) { return false; diff --git a/core/object/object.cpp b/core/object/object.cpp index ef1ca8132cb..3c322a3868c 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -940,7 +940,7 @@ void Object::set_script(const Variant &p_script) { script_instance = nullptr; } - if (!s.is_null()) { + if (s.is_valid()) { if (s->can_instantiate()) { OBJ_DEBUG_LOCK script_instance = s->instance_create(this); @@ -1579,7 +1579,7 @@ void Object::_clear_internal_resource_paths(const Variant &p_var) { switch (p_var.get_type()) { case Variant::OBJECT: { Ref r = p_var; - if (!r.is_valid()) { + if (r.is_null()) { return; } diff --git a/drivers/gles3/storage/texture_storage.cpp b/drivers/gles3/storage/texture_storage.cpp index 14bbe635a4f..3b8822e5c42 100644 --- a/drivers/gles3/storage/texture_storage.cpp +++ b/drivers/gles3/storage/texture_storage.cpp @@ -686,7 +686,7 @@ Ref TextureStorage::_get_gl_image_and_format(const Ref &p_image, I } if (need_decompress || p_force_decompress) { - if (!image.is_null()) { + if (image.is_valid()) { image = image->duplicate(); image->decompress(); ERR_FAIL_COND_V(image->is_compressed(), image); diff --git a/drivers/png/resource_saver_png.cpp b/drivers/png/resource_saver_png.cpp index 0df6b2ba213..d07416aa428 100644 --- a/drivers/png/resource_saver_png.cpp +++ b/drivers/png/resource_saver_png.cpp @@ -38,7 +38,7 @@ Error ResourceSaverPNG::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { Ref texture = p_resource; - ERR_FAIL_COND_V_MSG(!texture.is_valid(), ERR_INVALID_PARAMETER, "Can't save invalid texture as PNG."); + ERR_FAIL_COND_V_MSG(texture.is_null(), ERR_INVALID_PARAMETER, "Can't save invalid texture as PNG."); ERR_FAIL_COND_V_MSG(!texture->get_width(), ERR_INVALID_PARAMETER, "Can't save empty texture as PNG."); Ref img = texture->get_image();