Skip to content

Commit 21db848

Browse files
committed
Merge pull request godotengine#104664 from tomfull123/missing-typed-dictionary-initializer-list
Add missing `initializer_list` constructor to TypedDictionary
2 parents cba1afd + 8a3f984 commit 21db848

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

core/object/ref_counted.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class Ref {
8585

8686
//virtual RefCounted * get_reference() const { return reference; }
8787
public:
88+
static _FORCE_INLINE_ String get_class_static() {
89+
return T::get_class_static();
90+
}
91+
8892
_FORCE_INLINE_ bool operator==(const T *p_ptr) const {
8993
return reference == p_ptr;
9094
}

core/variant/typed_dictionary.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ struct GetTypeInfo<const TypedDictionary<K, V> &> {
189189
_FORCE_INLINE_ TypedDictionary() { \
190190
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
191191
} \
192+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<m_type, T>> p_init) : \
193+
Dictionary() { \
194+
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, std::remove_pointer<T>::type::get_class_static(), Variant()); \
195+
for (const KeyValue<m_type, T> &E : p_init) { \
196+
operator[](E.key) = E.value; \
197+
} \
198+
} \
192199
}; \
193200
template <typename T> \
194201
struct GetTypeInfo<TypedDictionary<m_type, T>> { \

tests/core/variant/test_dictionary.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,28 @@ TEST_CASE("[Dictionary] Iteration") {
606606
a2.clear();
607607
}
608608

609+
TEST_CASE("[Dictionary] Object value init") {
610+
Object *a = memnew(Object);
611+
Object *b = memnew(Object);
612+
TypedDictionary<double, Object *> tdict = {
613+
{ 0.0, a },
614+
{ 5.0, b },
615+
};
616+
CHECK_EQ(tdict[0.0], Variant(a));
617+
CHECK_EQ(tdict[5.0], Variant(b));
618+
memdelete(a);
619+
memdelete(b);
620+
}
621+
622+
TEST_CASE("[Dictionary] RefCounted value init") {
623+
Ref<RefCounted> a = memnew(RefCounted);
624+
Ref<RefCounted> b = memnew(RefCounted);
625+
TypedDictionary<double, Ref<RefCounted>> tdict = {
626+
{ 0.0, a },
627+
{ 5.0, b },
628+
};
629+
CHECK_EQ(tdict[0.0], Variant(a));
630+
CHECK_EQ(tdict[5.0], Variant(b));
631+
}
632+
609633
} // namespace TestDictionary

0 commit comments

Comments
 (0)