summaryrefslogtreecommitdiff
path: root/scene/resources
diff options
context:
space:
mode:
authorHein-Pieter van Braam <hp@tmm.cx>2017-08-24 22:58:51 +0200
committerHein-Pieter van Braam <hp@tmm.cx>2017-08-24 23:08:24 +0200
commitcacced7e507f7603bacc03ae2616e58f0ede122a (patch)
tree7af89373e86cd1a7af6ea04e10280084cabb7144 /scene/resources
parent4aa2c18cb428ffde05c67987926736a9ca62703b (diff)
Convert Object::cast_to() to the static version
Currently we rely on some undefined behavior when Object->cast_to() gets called with a Null pointer. This used to work fine with GCC < 6 but newer versions of GCC remove all codepaths in which the this pointer is Null. However, the non-static cast_to() was supposed to be null safe. This patch makes cast_to() Null safe and removes the now redundant Null checks where they existed. It is explained in this article: https://www.viva64.com/en/b/0226/
Diffstat (limited to 'scene/resources')
-rw-r--r--scene/resources/packed_scene.cpp12
-rw-r--r--scene/resources/scene_format_text.cpp4
-rw-r--r--scene/resources/shape.cpp2
-rw-r--r--scene/resources/theme.cpp9
4 files changed, 13 insertions, 14 deletions
diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cpp
index d7ea675a47..3ce44e2c31 100644
--- a/scene/resources/packed_scene.cpp
+++ b/scene/resources/packed_scene.cpp
@@ -151,18 +151,18 @@ Node *SceneState::instance(GenEditState p_edit_state) const {
//print_line("created");
//node belongs to this scene and must be created
Object *obj = ClassDB::instance(snames[n.type]);
- if (!obj || !obj->cast_to<Node>()) {
+ if (!Object::cast_to<Node>(obj)) {
if (obj) {
memdelete(obj);
obj = NULL;
}
WARN_PRINT(String("Warning node of type " + snames[n.type].operator String() + " does not exist.").ascii().get_data());
if (n.parent >= 0 && n.parent < nc && ret_nodes[n.parent]) {
- if (ret_nodes[n.parent]->cast_to<Spatial>()) {
+ if (Object::cast_to<Spatial>(ret_nodes[n.parent])) {
obj = memnew(Spatial);
- } else if (ret_nodes[n.parent]->cast_to<Control>()) {
+ } else if (Object::cast_to<Control>(ret_nodes[n.parent])) {
obj = memnew(Control);
- } else if (ret_nodes[n.parent]->cast_to<Node2D>()) {
+ } else if (Object::cast_to<Node2D>(ret_nodes[n.parent])) {
obj = memnew(Node2D);
}
}
@@ -172,7 +172,7 @@ Node *SceneState::instance(GenEditState p_edit_state) const {
}
}
- node = obj->cast_to<Node>();
+ node = Object::cast_to<Node>(obj);
} else {
print_line("wtf class is disabled for: " + itos(n.type));
@@ -754,7 +754,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, Map<StringName
// only connections that originate or end into main saved scene are saved
// everything else is discarded
- Node *target = c.target->cast_to<Node>();
+ Node *target = Object::cast_to<Node>(c.target);
if (!target) {
continue;
diff --git a/scene/resources/scene_format_text.cpp b/scene/resources/scene_format_text.cpp
index 49cd030a9a..0cccdc4fbf 100644
--- a/scene/resources/scene_format_text.cpp
+++ b/scene/resources/scene_format_text.cpp
@@ -239,7 +239,7 @@ Error ResourceInteractiveLoaderText::poll() {
return error;
}
- Resource *r = obj->cast_to<Resource>();
+ Resource *r = Object::cast_to<Resource>(obj);
if (!r) {
error_text += "Can't create sub resource of type, because not a resource: " + type;
@@ -305,7 +305,7 @@ Error ResourceInteractiveLoaderText::poll() {
return error;
}
- Resource *r = obj->cast_to<Resource>();
+ Resource *r = Object::cast_to<Resource>(obj);
if (!r) {
error_text += "Can't create sub resource of type, because not a resource: " + res_type;
diff --git a/scene/resources/shape.cpp b/scene/resources/shape.cpp
index 6be88374e5..d719aa5403 100644
--- a/scene/resources/shape.cpp
+++ b/scene/resources/shape.cpp
@@ -74,7 +74,7 @@ Ref<ArrayMesh> Shape::get_debug_mesh() {
arr.resize(Mesh::ARRAY_MAX);
arr[Mesh::ARRAY_VERTEX] = array;
- SceneTree *st = OS::get_singleton()->get_main_loop()->cast_to<SceneTree>();
+ SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, arr);
diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp
index ac1bb105ac..c2c7c37762 100644
--- a/scene/resources/theme.cpp
+++ b/scene/resources/theme.cpp
@@ -1030,14 +1030,13 @@ RES ResourceFormatLoaderTheme::load(const String &p_path, const String &p_origin
ERR_FAIL_V(RES());
}
- if (res->cast_to<StyleBox>()) {
-
+ if (Object::cast_to<StyleBox>(*res)) {
theme->set_stylebox(item, control, res);
- } else if (res->cast_to<Font>()) {
+ } else if (Object::cast_to<Font>(*res)) {
theme->set_font(item, control, res);
- } else if (res->cast_to<Font>()) {
+ } else if (Object::cast_to<Font>(*res)) {
theme->set_font(item, control, res);
- } else if (res->cast_to<Texture>()) {
+ } else if (Object::cast_to<Texture>(*res)) {
theme->set_icon(item, control, res);
} else {
memdelete(f);