Skip to content

Commit 8f6bb11

Browse files
committed
Merge pull request godotengine#100602 from KoBeWi/over_100_changes_in_50_random_files_aka_the_best_kind_of_PR
Add templated version of `ObjectDB::get_instance()`
2 parents 4950dee + bc9d0c7 commit 8f6bb11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+121
-104
lines changed

core/core_bind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ void Thread::_start_func(void *ud) {
13501350
target_callable.callp(nullptr, 0, ret, ce);
13511351
// If script properly kept a reference to the thread, we should be able to re-reference it now
13521352
// (well, or if the call failed, since we had to break chains anyway because the outcome isn't known upfront).
1353-
t = Ref<Thread>(ObjectDB::get_instance(th_instance_id));
1353+
t = ObjectDB::get_ref<Thread>(th_instance_id);
13541354
if (t.is_valid()) {
13551355
t->ret = ret;
13561356
t->running.clear();

core/error/error_macros.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void _physics_interpolation_warning(const char *p_function, const char *p_file,
188188
} else {
189189
String node_name;
190190
if (p_id.is_valid()) {
191-
Node *node = Object::cast_to<Node>(ObjectDB::get_instance(p_id));
191+
Node *node = ObjectDB::get_instance<Node>(p_id);
192192
if (node && node->is_inside_tree()) {
193193
node_name = "\"" + String(node->get_path()) + "\"";
194194
} else {

core/object/object.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
template <typename T>
4747
class TypedArray;
4848

49+
template <typename T>
50+
class Ref;
51+
4952
enum PropertyHint {
5053
PROPERTY_HINT_NONE, ///< no hint provided.
5154
PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians_as_degrees][,degrees][,exp][,suffix:<keyword>] range.
@@ -1075,6 +1078,15 @@ class ObjectDB {
10751078

10761079
return object;
10771080
}
1081+
1082+
template <typename T>
1083+
_ALWAYS_INLINE_ static T *get_instance(ObjectID p_instance_id) {
1084+
return Object::cast_to<T>(get_instance(p_instance_id));
1085+
}
1086+
1087+
template <typename T>
1088+
_ALWAYS_INLINE_ static Ref<T> get_ref(ObjectID p_instance_id); // Defined in ref_counted.h
1089+
10781090
static void debug_objects(DebugFunc p_func);
10791091
static int get_object_count();
10801092
};

core/object/ref_counted.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,8 @@ struct VariantInternalAccessor<const Ref<T> &> {
299299
// Zero-constructing Ref initializes reference to nullptr (and thus empty).
300300
template <typename T>
301301
struct is_zero_constructible<Ref<T>> : std::true_type {};
302+
303+
template <typename T>
304+
Ref<T> ObjectDB::get_ref(ObjectID p_instance_id) {
305+
return Ref<T>(get_instance(p_instance_id));
306+
}

editor/editor_file_system.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3099,7 +3099,7 @@ void EditorFileSystem::_queue_refresh_filesystem() {
30993099

31003100
void EditorFileSystem::_refresh_filesystem() {
31013101
for (const ObjectID &id : folders_to_sort) {
3102-
EditorFileSystemDirectory *dir = Object::cast_to<EditorFileSystemDirectory>(ObjectDB::get_instance(id));
3102+
EditorFileSystemDirectory *dir = ObjectDB::get_instance<EditorFileSystemDirectory>(id);
31033103
if (dir) {
31043104
dir->subdirs.sort_custom<DirectoryComparator>();
31053105
}

editor/editor_node.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,7 +2486,7 @@ void EditorNode::edit_item(Object *p_object, Object *p_editing_owner) {
24862486
if (kv.key == owner_id || !kv.value.has(plugin)) {
24872487
continue;
24882488
}
2489-
EditorPropertyResource *epres = Object::cast_to<EditorPropertyResource>(ObjectDB::get_instance(kv.key));
2489+
EditorPropertyResource *epres = ObjectDB::get_instance<EditorPropertyResource>(kv.key);
24902490
if (epres) {
24912491
// If it's resource property editing the same resource type, fold it later to avoid premature modifications
24922492
// that may result in unsafe iteration of active_plugins.
@@ -2617,7 +2617,7 @@ void EditorNode::_add_to_history(const Object *p_object, const String &p_propert
26172617
ObjectID history_id = editor_history.get_current();
26182618
if (id != history_id) {
26192619
const MultiNodeEdit *multi_node_edit = Object::cast_to<const MultiNodeEdit>(p_object);
2620-
const MultiNodeEdit *history_multi_node_edit = Object::cast_to<const MultiNodeEdit>(ObjectDB::get_instance(history_id));
2620+
const MultiNodeEdit *history_multi_node_edit = ObjectDB::get_instance<MultiNodeEdit>(history_id);
26212621
if (multi_node_edit && history_multi_node_edit && multi_node_edit->is_same_selection(history_multi_node_edit)) {
26222622
return;
26232623
}

editor/gui/scene_tree_editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_
12901290

12911291
void SceneTreeEditor::_tree_scroll_to_item(ObjectID p_item_id) {
12921292
ERR_FAIL_NULL(tree);
1293-
TreeItem *item = Object::cast_to<TreeItem>(ObjectDB::get_instance(p_item_id));
1293+
TreeItem *item = ObjectDB::get_instance<TreeItem>(p_item_id);
12941294
if (item) {
12951295
tree->scroll_to_item(item, true);
12961296
}

editor/plugins/animation_player_editor_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,7 @@ AnimationMixer *AnimationPlayerEditor::fetch_mixer_for_library() const {
19321932
}
19331933

19341934
Node *AnimationPlayerEditor::get_cached_root_node() const {
1935-
return Object::cast_to<Node>(ObjectDB::get_instance(cached_root_node_id));
1935+
return ObjectDB::get_instance<Node>(cached_root_node_id);
19361936
}
19371937

19381938
bool AnimationPlayerEditor::_validate_tracks(const Ref<Animation> p_anim) {

editor/plugins/canvas_item_editor_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4740,7 +4740,7 @@ void CanvasItemEditor::_popup_callback(int p_op) {
47404740

47414741
undo_redo->create_action(TTR("Paste Pose"));
47424742
for (const PoseClipboard &E : pose_clipboard) {
4743-
Node2D *n2d = Object::cast_to<Node2D>(ObjectDB::get_instance(E.id));
4743+
Node2D *n2d = ObjectDB::get_instance<Node2D>(E.id);
47444744
if (!n2d) {
47454745
continue;
47464746
}

editor/plugins/editor_resource_tooltip_plugins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
void EditorResourceTooltipPlugin::_thumbnail_ready(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) {
4040
ObjectID trid = p_udata;
41-
TextureRect *tr = Object::cast_to<TextureRect>(ObjectDB::get_instance(trid));
41+
TextureRect *tr = ObjectDB::get_instance<TextureRect>(trid);
4242

4343
if (!tr) {
4444
return;

0 commit comments

Comments
 (0)