Skip to content

Commit 8a76e31

Browse files
committed
Remove bool from Object::notification virtual function; replace with separate functions to avoid branching.
1 parent b13c96b commit 8a76e31

File tree

3 files changed

+91
-61
lines changed

3 files changed

+91
-61
lines changed

core/object/object.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -911,32 +911,44 @@ Variant Object::call_const(const StringName &p_method, const Variant **p_args, i
911911
return ret;
912912
}
913913

914-
void Object::notification(int p_notification, bool p_reversed) {
915-
if (p_reversed) {
916-
if (script_instance) {
917-
script_instance->notification(p_notification, p_reversed);
918-
}
919-
} else {
920-
_notificationv(p_notification, p_reversed);
921-
}
914+
void Object::_notification_forward(int p_notification) {
915+
// Notify classes starting with Object and ending with most derived subclass.
916+
// e.g. Object -> Node -> Node3D
917+
_notification_forwardv(p_notification);
922918

923919
if (_extension) {
924920
if (_extension->notification2) {
925-
_extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(p_reversed));
921+
_extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(false));
926922
#ifndef DISABLE_DEPRECATED
927923
} else if (_extension->notification) {
928924
_extension->notification(_extension_instance, p_notification);
929925
#endif // DISABLE_DEPRECATED
930926
}
931927
}
932928

933-
if (p_reversed) {
934-
_notificationv(p_notification, p_reversed);
935-
} else {
936-
if (script_instance) {
937-
script_instance->notification(p_notification, p_reversed);
929+
if (script_instance) {
930+
script_instance->notification(p_notification, false);
931+
}
932+
}
933+
934+
void Object::_notification_backward(int p_notification) {
935+
if (script_instance) {
936+
script_instance->notification(p_notification, true);
937+
}
938+
939+
if (_extension) {
940+
if (_extension->notification2) {
941+
_extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(true));
942+
#ifndef DISABLE_DEPRECATED
943+
} else if (_extension->notification) {
944+
_extension->notification(_extension_instance, p_notification);
945+
#endif // DISABLE_DEPRECATED
938946
}
939947
}
948+
949+
// Notify classes starting with most derived subclass and ending in Object.
950+
// e.g. Node3D -> Node -> Object
951+
_notification_backwardv(p_notification);
940952
}
941953

942954
String Object::to_string() {

core/object/object.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,16 +544,17 @@ protected:
544544
_FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \
545545
return (void(Object::*)(int)) & m_class::_notification; \
546546
} \
547-
virtual void _notificationv(int p_notification, bool p_reversed) override { \
548-
if (!p_reversed) { \
549-
m_inherits::_notificationv(p_notification, p_reversed); \
550-
} \
547+
virtual void _notification_forwardv(int p_notification) override { \
548+
m_inherits::_notification_forwardv(p_notification); \
551549
if (m_class::_get_notification() != m_inherits::_get_notification()) { \
552550
_notification(p_notification); \
553551
} \
554-
if (p_reversed) { \
555-
m_inherits::_notificationv(p_notification, p_reversed); \
552+
} \
553+
virtual void _notification_backwardv(int p_notification) override { \
554+
if (m_class::_get_notification() != m_inherits::_get_notification()) { \
555+
_notification(p_notification); \
556556
} \
557+
m_inherits::_notification_backwardv(p_notification); \
557558
} \
558559
\
559560
private:
@@ -697,7 +698,11 @@ class Object {
697698
virtual void _validate_propertyv(PropertyInfo &p_property) const {}
698699
virtual bool _property_can_revertv(const StringName &p_name) const { return false; }
699700
virtual bool _property_get_revertv(const StringName &p_name, Variant &r_property) const { return false; }
700-
virtual void _notificationv(int p_notification, bool p_reversed) {}
701+
702+
void _notification_forward(int p_notification);
703+
void _notification_backward(int p_notification);
704+
virtual void _notification_forwardv(int p_notification) {}
705+
virtual void _notification_backwardv(int p_notification) {}
701706

702707
static void _bind_methods();
703708
static void _bind_compatibility_methods() {}
@@ -895,7 +900,17 @@ class Object {
895900
return (cerr.error == Callable::CallError::CALL_OK) ? ret : Variant();
896901
}
897902

898-
void notification(int p_notification, bool p_reversed = false);
903+
// Depending on the boolean, we call either the virtual function _notification_backward or _notification_forward.
904+
// - Forward calls subclasses in descending order (e.g. Object -> Node -> Node3D -> extension -> script).
905+
// Backward calls subclasses in descending order (e.g. script -> extension -> Node3D -> Node -> Object).
906+
_FORCE_INLINE_ void notification(int p_notification, bool p_reversed = false) {
907+
if (p_reversed) {
908+
_notification_backward(p_notification);
909+
} else {
910+
_notification_forward(p_notification);
911+
}
912+
}
913+
899914
virtual String to_string();
900915

901916
// Used mainly by script, get and set all INCLUDING string.

tests/core/object/test_object.h

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -465,72 +465,75 @@ TEST_CASE("[Object] Signals") {
465465
}
466466
}
467467

468-
class NotificationObject1 : public Object {
469-
GDCLASS(NotificationObject1, Object);
468+
class NotificationObjectSuperclass : public Object {
469+
GDCLASS(NotificationObjectSuperclass, Object);
470470

471471
protected:
472472
void _notification(int p_what) {
473-
switch (p_what) {
474-
case 12345: {
475-
order_internal1 = order_global++;
476-
} break;
477-
}
473+
order_superclass = ++order_global;
478474
}
479475

480476
public:
481-
static int order_global;
482-
int order_internal1 = -1;
483-
484-
void reset_order() {
485-
order_internal1 = -1;
486-
order_global = 1;
487-
}
477+
static inline int order_global = 0;
478+
int order_superclass = -1;
488479
};
489480

490-
int NotificationObject1::order_global = 1;
491-
492-
class NotificationObject2 : public NotificationObject1 {
493-
GDCLASS(NotificationObject2, NotificationObject1);
481+
class NotificationObjectSubclass : public NotificationObjectSuperclass {
482+
GDCLASS(NotificationObjectSubclass, NotificationObjectSuperclass);
494483

495484
protected:
496485
void _notification(int p_what) {
497-
switch (p_what) {
498-
case 12345: {
499-
order_internal2 = order_global++;
500-
} break;
501-
}
486+
order_subclass = ++order_global;
502487
}
503488

504489
public:
505-
int order_internal2 = -1;
506-
void reset_order() {
507-
NotificationObject1::reset_order();
508-
order_internal2 = -1;
490+
int order_subclass = -1;
491+
};
492+
493+
class NotificationScriptInstance : public _MockScriptInstance {
494+
void notification(int p_notification, bool p_reversed) override {
495+
order_script = ++NotificationObjectSuperclass::order_global;
509496
}
497+
498+
public:
499+
int order_script = -1;
510500
};
511501

512502
TEST_CASE("[Object] Notification order") { // GH-52325
513-
NotificationObject2 *test_notification_object = memnew(NotificationObject2);
503+
NotificationObjectSubclass *object = memnew(NotificationObjectSubclass);
514504

515-
SUBCASE("regular order") {
516-
test_notification_object->notification(12345, false);
505+
NotificationScriptInstance *script = memnew(NotificationScriptInstance);
506+
object->set_script_instance(script);
517507

518-
CHECK_EQ(test_notification_object->order_internal1, 1);
519-
CHECK_EQ(test_notification_object->order_internal2, 2);
508+
SUBCASE("regular order") {
509+
NotificationObjectSubclass::order_global = 0;
510+
object->order_superclass = -1;
511+
object->order_subclass = -1;
512+
script->order_script = -1;
513+
object->notification(12345, false);
520514

521-
test_notification_object->reset_order();
515+
CHECK_EQ(object->order_superclass, 1);
516+
CHECK_EQ(object->order_subclass, 2);
517+
// TODO If an extension is attached, it should come here.
518+
CHECK_EQ(script->order_script, 3);
519+
CHECK_EQ(NotificationObjectSubclass::order_global, 3);
522520
}
523521

524522
SUBCASE("reverse order") {
525-
test_notification_object->notification(12345, true);
526-
527-
CHECK_EQ(test_notification_object->order_internal1, 2);
528-
CHECK_EQ(test_notification_object->order_internal2, 1);
523+
NotificationObjectSubclass::order_global = 0;
524+
object->order_superclass = -1;
525+
object->order_subclass = -1;
526+
script->order_script = -1;
527+
object->notification(12345, true);
529528

530-
test_notification_object->reset_order();
529+
CHECK_EQ(script->order_script, 1);
530+
// TODO If an extension is attached, it should come here.
531+
CHECK_EQ(object->order_subclass, 2);
532+
CHECK_EQ(object->order_superclass, 3);
533+
CHECK_EQ(NotificationObjectSubclass::order_global, 3);
531534
}
532535

533-
memdelete(test_notification_object);
536+
memdelete(object);
534537
}
535538

536539
TEST_CASE("[Object] Destruction at the end of the call chain is safe") {

0 commit comments

Comments
 (0)