Skip to content

Commit 9048776

Browse files
committed
Merge pull request godotengine#99751 from bruvzg/dir_init
Add `std::initializer_list` constructor for Dictionary.
2 parents cf165a1 + 54945c4 commit 9048776

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

core/variant/dictionary.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,15 @@ Dictionary::Dictionary() {
693693
_p->refcount.init();
694694
}
695695

696+
Dictionary::Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init) {
697+
_p = memnew(DictionaryPrivate);
698+
_p->refcount.init();
699+
700+
for (const KeyValue<Variant, Variant> &E : p_init) {
701+
operator[](E.key) = E.value;
702+
}
703+
}
704+
696705
Dictionary::~Dictionary() {
697706
_unref();
698707
}

core/variant/dictionary.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "core/string/ustring.h"
3535
#include "core/templates/list.h"
36+
#include "core/templates/pair.h"
3637
#include "core/variant/array.h"
3738

3839
class Variant;
@@ -112,6 +113,7 @@ class Dictionary {
112113

113114
Dictionary(const Dictionary &p_base, uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);
114115
Dictionary(const Dictionary &p_from);
116+
Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init);
115117
Dictionary();
116118
~Dictionary();
117119
};

core/variant/typed_dictionary.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ class TypedDictionary : public Dictionary {
5959
_FORCE_INLINE_ TypedDictionary() {
6060
set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
6161
}
62+
63+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<K, V>> p_init) :
64+
Dictionary() {
65+
set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
66+
for (const KeyValue<K, V> &E : p_init) {
67+
operator[](E.key) = E.value;
68+
}
69+
}
6270
};
6371

6472
template <typename K, typename V>
@@ -135,6 +143,13 @@ struct GetTypeInfo<const TypedDictionary<K, V> &> {
135143
_FORCE_INLINE_ TypedDictionary() { \
136144
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
137145
} \
146+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<T, m_type>> p_init) : \
147+
Dictionary() { \
148+
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
149+
for (const KeyValue<T, m_type> &E : p_init) { \
150+
operator[](E.key) = E.value; \
151+
} \
152+
} \
138153
}; \
139154
template <typename T> \
140155
struct GetTypeInfo<TypedDictionary<T, m_type>> { \
@@ -217,6 +232,13 @@ struct GetTypeInfo<const TypedDictionary<K, V> &> {
217232
_FORCE_INLINE_ TypedDictionary() { \
218233
set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant()); \
219234
} \
235+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<m_type_key, m_type_value>> p_init) : \
236+
Dictionary() { \
237+
set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant()); \
238+
for (const KeyValue<m_type_key, m_type_value> &E : p_init) { \
239+
operator[](E.key) = E.value; \
240+
} \
241+
} \
220242
}; \
221243
template <> \
222244
struct GetTypeInfo<TypedDictionary<m_type_key, m_type_value>> { \

tests/core/variant/test_dictionary.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
9595
CHECK(map.size() == length);
9696
}
9797

98+
TEST_CASE("[Dictionary] List init") {
99+
Dictionary dict{
100+
{ 0, "int" },
101+
{ "packed_string_array", PackedStringArray({ "array", "of", "values" }) },
102+
{ "key", Dictionary({ { "nested", 200 } }) },
103+
{ Vector2(), "v2" },
104+
};
105+
CHECK(dict.size() == 4);
106+
CHECK(dict[0] == "int");
107+
CHECK(PackedStringArray(dict["packed_string_array"])[2] == "values");
108+
CHECK(Dictionary(dict["key"])["nested"] == Variant(200));
109+
CHECK(dict[Vector2()] == "v2");
110+
111+
TypedDictionary<double, double> tdict{
112+
{ 0.0, 1.0 },
113+
{ 5.0, 2.0 },
114+
};
115+
CHECK_EQ(tdict[0.0], Variant(1.0));
116+
CHECK_EQ(tdict[5.0], Variant(2.0));
117+
}
118+
98119
TEST_CASE("[Dictionary] get_key_lists()") {
99120
Dictionary map;
100121
List<Variant> keys;

0 commit comments

Comments
 (0)