Skip to content

Commit

Permalink
Merge pull request #96076 from AThousandShips/improve_null_check_core…
Browse files Browse the repository at this point in the history
…_drivers

[Core,Drivers] Improve use of `Ref.is_null/valid`
  • Loading branch information
Repiteo committed Dec 23, 2024
2 parents 0c80b47 + ec650a2 commit 3c304ab
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion core/input/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ List<StringName> InputMap::get_actions() const {
}

List<Ref<InputEvent>>::Element *InputMap::_find_event(Action &p_action, const Ref<InputEvent> &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<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
Expand Down
4 changes: 2 additions & 2 deletions core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3992,7 +3992,7 @@ Error Image::load_svg_from_buffer(const Vector<uint8_t> &p_array, float scale) {
ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);

Ref<Image> 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);

Expand Down Expand Up @@ -4059,7 +4059,7 @@ Error Image::_load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc
const uint8_t *r = p_array.ptr();

Ref<Image> 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);

Expand Down
14 changes: 7 additions & 7 deletions core/io/packet_peer_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -137,7 +137,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;
Expand Down Expand Up @@ -178,7 +178,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).");
Expand Down Expand Up @@ -227,7 +227,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).");

Expand Down Expand Up @@ -278,12 +278,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;
Expand Down
4 changes: 2 additions & 2 deletions core/io/plist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,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;
}
Expand Down Expand Up @@ -743,7 +743,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;
}
Expand Down
2 changes: 1 addition & 1 deletion core/io/remote_filesystem_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

Vector<RemoteFilesystemClient::FileCache> RemoteFilesystemClient::_load_cache_file() {
Ref<FileAccess> fa = FileAccess::open(cache_path.path_join(FILES_CACHE_FILE), FileAccess::READ);
if (!fa.is_valid()) {
if (fa.is_null()) {
return Vector<FileCache>(); // No cache, return empty
}

Expand Down
6 changes: 3 additions & 3 deletions core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void Resource::reload_from_file() {

Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);

if (!s.is_valid()) {
if (s.is_null()) {
return;
}

Expand Down Expand Up @@ -651,7 +651,7 @@ Ref<Resource> ResourceCache::get_ref(const String &p_path) {
ref = Ref<Resource>(*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);
Expand All @@ -670,7 +670,7 @@ void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
for (KeyValue<String, Resource *> &E : resources) {
Ref<Resource> ref = Ref<Resource>(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);
Expand Down
4 changes: 2 additions & 2 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,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 {
Expand Down Expand Up @@ -1387,7 +1387,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons

ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT);
Ref<Resource> 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);
}
Expand Down
4 changes: 2 additions & 2 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hi
thread_mode = LOAD_THREAD_SPAWN_SINGLE;
}
Ref<LoadToken> 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;
}
Expand Down Expand Up @@ -945,7 +945,7 @@ Ref<Resource> ResourceLoader::ensure_resource_ref_override_for_outer_load(const
Object *obj = ClassDB::instantiate(p_res_type);
ERR_FAIL_NULL_V(obj, Ref<Resource>());
Ref<Resource> res(obj);
if (!res.is_valid()) {
if (res.is_null()) {
memdelete(obj);
ERR_FAIL_V(Ref<Resource>());
}
Expand Down
10 changes: 5 additions & 5 deletions core/io/stream_peer_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void StreamPeerTCP::accept_socket(Ref<NetSocket> 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).");

Expand All @@ -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).");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}

Expand Down
8 changes: 4 additions & 4 deletions core/io/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -108,7 +108,7 @@ Ref<StreamPeerTCP> TCPServer::take_connection() {
IPAddress ip;
uint16_t port = 0;
ns = _sock->accept(ip, port);
if (!ns.is_valid()) {
if (ns.is_null()) {
return conn;
}

Expand Down
8 changes: 4 additions & 4 deletions core/io/udp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,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);
Expand Down Expand Up @@ -1578,7 +1578,7 @@ void Object::_clear_internal_resource_paths(const Variant &p_var) {
switch (p_var.get_type()) {
case Variant::OBJECT: {
Ref<Resource> r = p_var;
if (!r.is_valid()) {
if (r.is_null()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/gles3/storage/texture_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &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);
Expand Down
2 changes: 1 addition & 1 deletion drivers/png/resource_saver_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
Error ResourceSaverPNG::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
Ref<ImageTexture> 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<Image> img = texture->get_image();
Expand Down

0 comments on commit 3c304ab

Please sign in to comment.