From d4b2ee0032aa48a98c6e8a05189d8048d80b722e Mon Sep 17 00:00:00 2001 From: rune-scape Date: Sun, 28 Jul 2024 13:39:21 -0700 Subject: [PATCH] Avoid const_cast in node.cpp --- editor/scene_tree_dock.cpp | 42 +++++++++++++++---------------- editor/scene_tree_dock.h | 2 +- scene/debugger/scene_debugger.cpp | 2 +- scene/main/node.cpp | 36 +++++++++++++------------- scene/main/node.h | 8 +++--- 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 2cb2120d1107..4fa9eb0957d8 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -637,7 +637,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { selection.sort_custom(); for (Node *node : selection) { - HashMap duplimap; + HashMap duplimap; Node *dup = node->duplicate_from_editor(duplimap); ERR_CONTINUE(!dup); @@ -853,7 +853,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { selection.sort_custom(); - HashMap add_below_map; + HashMap add_below_map; for (List::Element *E = selection.back(); E; E = E->prev()) { Node *node = E->get(); @@ -876,7 +876,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { } } - HashMap duplimap; + HashMap duplimap; Node *dup = node->duplicate_from_editor(duplimap); ERR_CONTINUE(!dup); @@ -1172,7 +1172,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { List selection = editor_selection->get_selected_node_list(); List::Element *e = selection.front(); if (e) { - const Node *node = e->get(); + Node *node = e->get(); if (node) { FileSystemDock::get_singleton()->navigate_to_path(node->get_scene_file_path()); } @@ -1180,7 +1180,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { } break; case TOOL_OPEN_DOCUMENTATION: { List selection = editor_selection->get_selected_node_list(); - for (const Node *node : selection) { + for (Node *node : selection) { ScriptEditor::get_singleton()->goto_help("class_name:" + node->get_class()); } EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT); @@ -1802,7 +1802,7 @@ void SceneTreeDock::_fill_path_renames(Vector base_path, Vector &p_to_delete) const { // Skip if this node will be deleted. - for (const Node *F : p_to_delete) { + for (Node *F : p_to_delete) { if (F == p_node || F->is_ancestor_of(p_node)) { return false; } @@ -1826,7 +1826,7 @@ bool SceneTreeDock::_has_tracks_to_delete(Node *p_node, List &p_to_delet NodePath track_np = anim->track_get_path(i); Node *n = root->get_node_or_null(track_np); if (n) { - for (const Node *F : p_to_delete) { + for (Node *F : p_to_delete) { if (F == n || F->is_ancestor_of(n)) { return true; } @@ -2634,7 +2634,7 @@ void SceneTreeDock::_delete_confirm(bool p_cut) { bool entire_scene = false; - for (const Node *E : remove_list) { + for (Node *E : remove_list) { if (E == edited_scene) { entire_scene = true; break; @@ -2653,7 +2653,7 @@ void SceneTreeDock::_delete_confirm(bool p_cut) { undo_redo->add_undo_method(scene_tree, "update_tree"); undo_redo->add_undo_reference(edited_scene); } else { - for (const Node *E : remove_list) { + for (Node *E : remove_list) { // `move_child` + `get_index` doesn't really work for internal nodes. ERR_FAIL_COND_MSG(E->get_internal_mode() != INTERNAL_MODE_DISABLED, "Trying to remove internal node, this is not supported."); } @@ -2920,7 +2920,7 @@ void SceneTreeDock::_create() { if (parent_node_3d) { Vector3 position; uint32_t node_count = 0; - for (const Node *node : nodes) { + for (Node *node : nodes) { const Node3D *node_3d = Object::cast_to(node); if (node_3d) { position += node_3d->get_global_position(); @@ -2937,7 +2937,7 @@ void SceneTreeDock::_create() { if (parent_node_2d) { Vector2 position; uint32_t node_count = 0; - for (const Node *node : nodes) { + for (Node *node : nodes) { const Node2D *node_2d = Object::cast_to(node); if (node_2d) { position += node_2d->get_global_position(); @@ -3193,12 +3193,12 @@ void SceneTreeDock::_new_scene_from(const String &p_file) { Node *base = selection.front()->get(); - HashMap duplimap; - HashMap inverse_duplimap; + HashMap duplimap; + HashMap inverse_duplimap; Node *copy = base->duplicate_from_editor(duplimap); - for (const KeyValue &item : duplimap) { - inverse_duplimap[item.value] = const_cast(item.key); + for (const KeyValue &item : duplimap) { + inverse_duplimap[item.value] = item.key; } if (copy) { @@ -3238,11 +3238,11 @@ void SceneTreeDock::_new_scene_from(const String &p_file) { } } -void SceneTreeDock::_set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap &p_inverse_duplimap) { - HashMap::ConstIterator E = p_inverse_duplimap.find(p_node); +void SceneTreeDock::_set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap &p_inverse_duplimap) { + HashMap::ConstIterator E = p_inverse_duplimap.find(p_node); if (E) { - const Node *original = E->value; + Node *original = E->value; if (original->get_owner()) { p_node->set_owner(p_owner); } @@ -4016,7 +4016,7 @@ List SceneTreeDock::paste_nodes(bool p_paste_as_sibling) { } for (Node *node : node_clipboard) { - HashMap duplimap; + HashMap duplimap; Node *dup = node->duplicate_from_editor(duplimap, resource_remap); ERR_CONTINUE(!dup); @@ -4032,12 +4032,12 @@ List SceneTreeDock::paste_nodes(bool p_paste_as_sibling) { ur->add_do_method(paste_parent, "add_child", dup, true); } - for (KeyValue &E2 : duplimap) { + for (KeyValue &E2 : duplimap) { Node *d = E2.value; // When copying, all nodes that should have an owner assigned here were given nullptr as an owner // and added to the node_clipboard_edited_scene_owned list. if (d != dup && E2.key->get_owner() == nullptr) { - if (node_clipboard_edited_scene_owned.find(const_cast(E2.key))) { + if (node_clipboard_edited_scene_owned.find(E2.key)) { ur->add_do_method(d, "set_owner", edited_scene); } } diff --git a/editor/scene_tree_dock.h b/editor/scene_tree_dock.h index 5028cd5cc9cc..75540cc21430 100644 --- a/editor/scene_tree_dock.h +++ b/editor/scene_tree_dock.h @@ -250,7 +250,7 @@ class SceneTreeDock : public VBoxContainer { void _scene_tree_gui_input(Ref p_event); void _new_scene_from(const String &p_file); - void _set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap &p_inverse_duplimap); + void _set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap &p_inverse_duplimap); bool _validate_no_foreign(); bool _validate_no_instance(); diff --git a/scene/debugger/scene_debugger.cpp b/scene/debugger/scene_debugger.cpp index 07c32eef1360..b3c0273a9959 100644 --- a/scene/debugger/scene_debugger.cpp +++ b/scene/debugger/scene_debugger.cpp @@ -224,7 +224,7 @@ void SceneDebugger::_save_node(ObjectID id, const String &p_path) { ERR_FAIL_NULL(node); #ifdef TOOLS_ENABLED - HashMap duplimap; + HashMap duplimap; Node *copy = node->duplicate_from_editor(duplimap); #else Node *copy = node->duplicate(); diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 0396f3ab4afa..943e3c5cc0ae 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -1710,9 +1710,9 @@ TypedArray Node::get_children(bool p_include_internal) const { } Node *Node::_get_child_by_name(const StringName &p_name) const { - const Node *const *node = data.children.getptr(p_name); + Node *const *node = data.children.getptr(p_name); if (node) { - return const_cast(*node); + return *node; } else { return nullptr; } @@ -1757,7 +1757,7 @@ Node *Node::get_node_or_null(const NodePath &p_path) const { } } else if (name.is_node_unique_name()) { - Node **unique = current->data.owned_unique_nodes.getptr(name); + Node *const *unique = current->data.owned_unique_nodes.getptr(name); if (!unique && current->data.owner) { unique = current->data.owner->data.owned_unique_nodes.getptr(name); } @@ -1767,9 +1767,9 @@ Node *Node::get_node_or_null(const NodePath &p_path) const { next = *unique; } else { next = nullptr; - const Node *const *node = current->data.children.getptr(name); + Node *const *node = current->data.children.getptr(name); if (node) { - next = const_cast(*node); + next = *node; } else { return nullptr; } @@ -2134,9 +2134,9 @@ void Node::_clean_up_owner() { data.OW = nullptr; } -Node *Node::find_common_parent_with(const Node *p_node) const { +Node *Node::find_common_parent_with(Node *p_node) const { if (this == p_node) { - return const_cast(p_node); + return p_node; } HashSet visited; @@ -2148,7 +2148,7 @@ Node *Node::find_common_parent_with(const Node *p_node) const { n = n->data.parent; } - const Node *common_parent = p_node; + Node *common_parent = p_node; while (common_parent) { if (visited.has(common_parent)) { @@ -2161,7 +2161,7 @@ Node *Node::find_common_parent_with(const Node *p_node) const { return nullptr; } - return const_cast(common_parent); + return common_parent; } NodePath Node::get_path_to(const Node *p_node, bool p_use_unique_path) const { @@ -2633,7 +2633,7 @@ bool Node::get_scene_instance_load_placeholder() const { return data.use_placeholder; } -Node *Node::_duplicate(int p_flags, HashMap *r_duplimap) const { +Node *Node::_duplicate(int p_flags, HashMap *r_duplimap) { ERR_THREAD_GUARD_V(nullptr); Node *node = nullptr; @@ -2675,18 +2675,18 @@ Node *Node::_duplicate(int p_flags, HashMap *r_duplimap) c node->data.editable_instance = data.editable_instance; } - List hidden_roots; - List node_tree; + List hidden_roots; + List node_tree; node_tree.push_front(this); if (instantiated) { // Since nodes in the instantiated hierarchy won't be duplicated explicitly, we need to make an inventory // of all the nodes in the tree of the instantiated scene in order to transfer the values of the properties - Vector instance_roots; + Vector instance_roots; instance_roots.push_back(this); - for (List::Element *N = node_tree.front(); N; N = N->next()) { + for (List::Element *N = node_tree.front(); N; N = N->next()) { for (int i = 0; i < N->get()->get_child_count(); ++i) { Node *descendant = N->get()->get_child(i); @@ -2756,7 +2756,7 @@ Node *Node::_duplicate(int p_flags, HashMap *r_duplimap) c } } - for (const Node *&E : hidden_roots) { + for (Node *&E : hidden_roots) { Node *parent = node->get_node(get_path_to(E->data.parent)); if (!parent) { memdelete(node); @@ -2781,7 +2781,7 @@ Node *Node::_duplicate(int p_flags, HashMap *r_duplimap) c Node *Node::duplicate(int p_flags) const { ERR_THREAD_GUARD_V(nullptr); - Node *dupe = _duplicate(p_flags); + Node *dupe = const_cast(this)->_duplicate(p_flags); _duplicate_properties(this, this, dupe, p_flags); @@ -2793,11 +2793,11 @@ Node *Node::duplicate(int p_flags) const { } #ifdef TOOLS_ENABLED -Node *Node::duplicate_from_editor(HashMap &r_duplimap) const { +Node *Node::duplicate_from_editor(HashMap &r_duplimap) { return duplicate_from_editor(r_duplimap, HashMap, Ref>()); } -Node *Node::duplicate_from_editor(HashMap &r_duplimap, const HashMap, Ref> &p_resource_remap) const { +Node *Node::duplicate_from_editor(HashMap &r_duplimap, const HashMap, Ref> &p_resource_remap) { int flags = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR; Node *dupe = _duplicate(flags, &r_duplimap); diff --git a/scene/main/node.h b/scene/main/node.h index ee195ddef965..c741591600e4 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -269,7 +269,7 @@ class Node : public Object { void _duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const; void _duplicate_signals(const Node *p_original, Node *p_copy) const; - Node *_duplicate(int p_flags, HashMap *r_duplimap = nullptr) const; + Node *_duplicate(int p_flags, HashMap *r_duplimap = nullptr); TypedArray _get_groups() const; @@ -454,7 +454,7 @@ class Node : public Object { NodePath get_path() const; NodePath get_path_to(const Node *p_node, bool p_use_unique_path = false) const; - Node *find_common_parent_with(const Node *p_node) const; + Node *find_common_parent_with(Node *p_node) const; void add_to_group(const StringName &p_identifier, bool p_persistent = false); void remove_from_group(const StringName &p_identifier); @@ -610,8 +610,8 @@ class Node : public Object { Node *duplicate(int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const; #ifdef TOOLS_ENABLED - Node *duplicate_from_editor(HashMap &r_duplimap) const; - Node *duplicate_from_editor(HashMap &r_duplimap, const HashMap, Ref> &p_resource_remap) const; + Node *duplicate_from_editor(HashMap &r_duplimap); + Node *duplicate_from_editor(HashMap &r_duplimap, const HashMap, Ref> &p_resource_remap); void remap_node_resources(Node *p_node, const HashMap, Ref> &p_resource_remap) const; void remap_nested_resources(Ref p_resource, const HashMap, Ref> &p_resource_remap) const; #endif