Skip to content

Commit

Permalink
Avoid const_cast in node.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
rune-scape committed Jul 29, 2024
1 parent 607b230 commit d4b2ee0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
42 changes: 21 additions & 21 deletions editor/scene_tree_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
selection.sort_custom<Node::Comparator>();

for (Node *node : selection) {
HashMap<const Node *, Node *> duplimap;
HashMap<Node *, Node *> duplimap;
Node *dup = node->duplicate_from_editor(duplimap);

ERR_CONTINUE(!dup);
Expand Down Expand Up @@ -853,7 +853,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {

selection.sort_custom<Node::Comparator>();

HashMap<const Node *, Node *> add_below_map;
HashMap<Node *, Node *> add_below_map;

for (List<Node *>::Element *E = selection.back(); E; E = E->prev()) {
Node *node = E->get();
Expand All @@ -876,7 +876,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
}
}

HashMap<const Node *, Node *> duplimap;
HashMap<Node *, Node *> duplimap;
Node *dup = node->duplicate_from_editor(duplimap);

ERR_CONTINUE(!dup);
Expand Down Expand Up @@ -1172,15 +1172,15 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
List<Node *> selection = editor_selection->get_selected_node_list();
List<Node *>::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());
}
}
} break;
case TOOL_OPEN_DOCUMENTATION: {
List<Node *> 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);
Expand Down Expand Up @@ -1802,7 +1802,7 @@ void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<Stri

bool SceneTreeDock::_has_tracks_to_delete(Node *p_node, List<Node *> &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;
}
Expand All @@ -1826,7 +1826,7 @@ bool SceneTreeDock::_has_tracks_to_delete(Node *p_node, List<Node *> &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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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.");
}
Expand Down Expand Up @@ -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<Node3D>(node);
if (node_3d) {
position += node_3d->get_global_position();
Expand All @@ -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<Node2D>(node);
if (node_2d) {
position += node_2d->get_global_position();
Expand Down Expand Up @@ -3193,12 +3193,12 @@ void SceneTreeDock::_new_scene_from(const String &p_file) {

Node *base = selection.front()->get();

HashMap<const Node *, Node *> duplimap;
HashMap<const Node *, Node *> inverse_duplimap;
HashMap<Node *, Node *> duplimap;
HashMap<Node *, Node *> inverse_duplimap;
Node *copy = base->duplicate_from_editor(duplimap);

for (const KeyValue<const Node *, Node *> &item : duplimap) {
inverse_duplimap[item.value] = const_cast<Node *>(item.key);
for (const KeyValue<Node *, Node *> &item : duplimap) {
inverse_duplimap[item.value] = item.key;
}

if (copy) {
Expand Down Expand Up @@ -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<const Node *, Node *> &p_inverse_duplimap) {
HashMap<const Node *, Node *>::ConstIterator E = p_inverse_duplimap.find(p_node);
void SceneTreeDock::_set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap<Node *, Node *> &p_inverse_duplimap) {
HashMap<Node *, Node *>::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);
}
Expand Down Expand Up @@ -4016,7 +4016,7 @@ List<Node *> SceneTreeDock::paste_nodes(bool p_paste_as_sibling) {
}

for (Node *node : node_clipboard) {
HashMap<const Node *, Node *> duplimap;
HashMap<Node *, Node *> duplimap;

Node *dup = node->duplicate_from_editor(duplimap, resource_remap);
ERR_CONTINUE(!dup);
Expand All @@ -4032,12 +4032,12 @@ List<Node *> SceneTreeDock::paste_nodes(bool p_paste_as_sibling) {
ur->add_do_method(paste_parent, "add_child", dup, true);
}

for (KeyValue<const Node *, Node *> &E2 : duplimap) {
for (KeyValue<Node *, Node *> &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<Node *>(E2.key))) {
if (node_clipboard_edited_scene_owned.find(E2.key)) {
ur->add_do_method(d, "set_owner", edited_scene);
}
}
Expand Down
2 changes: 1 addition & 1 deletion editor/scene_tree_dock.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class SceneTreeDock : public VBoxContainer {
void _scene_tree_gui_input(Ref<InputEvent> p_event);

void _new_scene_from(const String &p_file);
void _set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap<const Node *, Node *> &p_inverse_duplimap);
void _set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap<Node *, Node *> &p_inverse_duplimap);

bool _validate_no_foreign();
bool _validate_no_instance();
Expand Down
2 changes: 1 addition & 1 deletion scene/debugger/scene_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void SceneDebugger::_save_node(ObjectID id, const String &p_path) {
ERR_FAIL_NULL(node);

#ifdef TOOLS_ENABLED
HashMap<const Node *, Node *> duplimap;
HashMap<Node *, Node *> duplimap;
Node *copy = node->duplicate_from_editor(duplimap);
#else
Node *copy = node->duplicate();
Expand Down
36 changes: 18 additions & 18 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1710,9 +1710,9 @@ TypedArray<Node> 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 *>(*node);
return *node;
} else {
return nullptr;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 *>(*node);
next = *node;
} else {
return nullptr;
}
Expand Down Expand Up @@ -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<Node *>(p_node);
return p_node;
}

HashSet<const Node *> visited;
Expand All @@ -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)) {
Expand All @@ -2161,7 +2161,7 @@ Node *Node::find_common_parent_with(const Node *p_node) const {
return nullptr;
}

return const_cast<Node *>(common_parent);
return common_parent;
}

NodePath Node::get_path_to(const Node *p_node, bool p_use_unique_path) const {
Expand Down Expand Up @@ -2633,7 +2633,7 @@ bool Node::get_scene_instance_load_placeholder() const {
return data.use_placeholder;
}

Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) const {
Node *Node::_duplicate(int p_flags, HashMap<Node *, Node *> *r_duplimap) {
ERR_THREAD_GUARD_V(nullptr);
Node *node = nullptr;

Expand Down Expand Up @@ -2675,18 +2675,18 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c
node->data.editable_instance = data.editable_instance;
}

List<const Node *> hidden_roots;
List<const Node *> node_tree;
List<Node *> hidden_roots;
List<Node *> 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<const Node *> instance_roots;
Vector<Node *> instance_roots;
instance_roots.push_back(this);

for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
for (List<Node *>::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);

Expand Down Expand Up @@ -2756,7 +2756,7 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *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);
Expand All @@ -2781,7 +2781,7 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c

Node *Node::duplicate(int p_flags) const {
ERR_THREAD_GUARD_V(nullptr);
Node *dupe = _duplicate(p_flags);
Node *dupe = const_cast<Node *>(this)->_duplicate(p_flags);

_duplicate_properties(this, this, dupe, p_flags);

Expand All @@ -2793,11 +2793,11 @@ Node *Node::duplicate(int p_flags) const {
}

#ifdef TOOLS_ENABLED
Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const {
Node *Node::duplicate_from_editor(HashMap<Node *, Node *> &r_duplimap) {
return duplicate_from_editor(r_duplimap, HashMap<Ref<Resource>, Ref<Resource>>());
}

Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
Node *Node::duplicate_from_editor(HashMap<Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) {
int flags = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR;
Node *dupe = _duplicate(flags, &r_duplimap);

Expand Down
8 changes: 4 additions & 4 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Node *, Node *> *r_duplimap = nullptr) const;
Node *_duplicate(int p_flags, HashMap<Node *, Node *> *r_duplimap = nullptr);

TypedArray<StringName> _get_groups() const;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<const Node *, Node *> &r_duplimap) const;
Node *duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
Node *duplicate_from_editor(HashMap<Node *, Node *> &r_duplimap);
Node *duplicate_from_editor(HashMap<Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap);
void remap_node_resources(Node *p_node, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
void remap_nested_resources(Ref<Resource> p_resource, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
#endif
Expand Down

0 comments on commit d4b2ee0

Please sign in to comment.