From 8a3f9846c5deb5ab543eae8a6a9f7b0c422a3d5f Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 26 Mar 2025 19:01:56 +0000 Subject: [PATCH] Add missing initializer_list constructor for TypedDictionary --- core/object/ref_counted.h | 4 ++++ core/variant/typed_dictionary.h | 7 +++++++ tests/core/variant/test_dictionary.h | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/core/object/ref_counted.h b/core/object/ref_counted.h index 10d5e76161..09b0f6a027 100644 --- a/core/object/ref_counted.h +++ b/core/object/ref_counted.h @@ -85,6 +85,10 @@ class Ref { //virtual RefCounted * get_reference() const { return reference; } public: + static _FORCE_INLINE_ String get_class_static() { + return T::get_class_static(); + } + _FORCE_INLINE_ bool operator==(const T *p_ptr) const { return reference == p_ptr; } diff --git a/core/variant/typed_dictionary.h b/core/variant/typed_dictionary.h index 7670eb6adc..b0df0d3aaa 100644 --- a/core/variant/typed_dictionary.h +++ b/core/variant/typed_dictionary.h @@ -189,6 +189,13 @@ struct GetTypeInfo &> { _FORCE_INLINE_ TypedDictionary() { \ set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \ } \ + _FORCE_INLINE_ TypedDictionary(std::initializer_list> p_init) : \ + Dictionary() { \ + set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, std::remove_pointer::type::get_class_static(), Variant()); \ + for (const KeyValue &E : p_init) { \ + operator[](E.key) = E.value; \ + } \ + } \ }; \ template \ struct GetTypeInfo> { \ diff --git a/tests/core/variant/test_dictionary.h b/tests/core/variant/test_dictionary.h index fd3341209a..cef6a8ef74 100644 --- a/tests/core/variant/test_dictionary.h +++ b/tests/core/variant/test_dictionary.h @@ -606,4 +606,28 @@ TEST_CASE("[Dictionary] Iteration") { a2.clear(); } +TEST_CASE("[Dictionary] Object value init") { + Object *a = memnew(Object); + Object *b = memnew(Object); + TypedDictionary tdict = { + { 0.0, a }, + { 5.0, b }, + }; + CHECK_EQ(tdict[0.0], Variant(a)); + CHECK_EQ(tdict[5.0], Variant(b)); + memdelete(a); + memdelete(b); +} + +TEST_CASE("[Dictionary] RefCounted value init") { + Ref a = memnew(RefCounted); + Ref b = memnew(RefCounted); + TypedDictionary> tdict = { + { 0.0, a }, + { 5.0, b }, + }; + CHECK_EQ(tdict[0.0], Variant(a)); + CHECK_EQ(tdict[5.0], Variant(b)); +} + } // namespace TestDictionary