Quellcode durchsuchen

Add std::initializer_list constructor for Dictionary.

Pāvels Nadtočajevs vor 8 Monaten
Ursprung
Commit
54945c4d28

+ 9 - 0
core/variant/dictionary.cpp

@@ -693,6 +693,15 @@ Dictionary::Dictionary() {
 	_p->refcount.init();
 }
 
+Dictionary::Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init) {
+	_p = memnew(DictionaryPrivate);
+	_p->refcount.init();
+
+	for (const KeyValue<Variant, Variant> &E : p_init) {
+		operator[](E.key) = E.value;
+	}
+}
+
 Dictionary::~Dictionary() {
 	_unref();
 }

+ 2 - 0
core/variant/dictionary.h

@@ -33,6 +33,7 @@
 
 #include "core/string/ustring.h"
 #include "core/templates/list.h"
+#include "core/templates/pair.h"
 #include "core/variant/array.h"
 
 class Variant;
@@ -112,6 +113,7 @@ public:
 
 	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);
 	Dictionary(const Dictionary &p_from);
+	Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init);
 	Dictionary();
 	~Dictionary();
 };

+ 22 - 0
core/variant/typed_dictionary.h

@@ -59,6 +59,14 @@ public:
 	_FORCE_INLINE_ TypedDictionary() {
 		set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
 	}
+
+	_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<K, V>> p_init) :
+			Dictionary() {
+		set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
+		for (const KeyValue<K, V> &E : p_init) {
+			operator[](E.key) = E.value;
+		}
+	}
 };
 
 template <typename K, typename V>
@@ -135,6 +143,13 @@ struct GetTypeInfo<const TypedDictionary<K, V> &> {
 		_FORCE_INLINE_ TypedDictionary() {                                                                                                         \
 			set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant());                                 \
 		}                                                                                                                                          \
+		_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<T, m_type>> p_init) :                                                        \
+				Dictionary() {                                                                                                                     \
+			set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant());                                 \
+			for (const KeyValue<T, m_type> &E : p_init) {                                                                                          \
+				operator[](E.key) = E.value;                                                                                                       \
+			}                                                                                                                                      \
+		}                                                                                                                                          \
 	};                                                                                                                                             \
 	template <typename T>                                                                                                                          \
 	struct GetTypeInfo<TypedDictionary<T, m_type>> {                                                                                               \
@@ -217,6 +232,13 @@ struct GetTypeInfo<const TypedDictionary<K, V> &> {
 		_FORCE_INLINE_ TypedDictionary() {                                                                                        \
 			set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant());                \
 		}                                                                                                                         \
+		_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<m_type_key, m_type_value>> p_init) :                        \
+				Dictionary() {                                                                                                    \
+			set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant());                \
+			for (const KeyValue<m_type_key, m_type_value> &E : p_init) {                                                          \
+				operator[](E.key) = E.value;                                                                                      \
+			}                                                                                                                     \
+		}                                                                                                                         \
 	};                                                                                                                            \
 	template <>                                                                                                                   \
 	struct GetTypeInfo<TypedDictionary<m_type_key, m_type_value>> {                                                               \

+ 21 - 0
tests/core/variant/test_dictionary.h

@@ -95,6 +95,27 @@ TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
 	CHECK(map.size() == length);
 }
 
+TEST_CASE("[Dictionary] List init") {
+	Dictionary dict{
+		{ 0, "int" },
+		{ "packed_string_array", PackedStringArray({ "array", "of", "values" }) },
+		{ "key", Dictionary({ { "nested", 200 } }) },
+		{ Vector2(), "v2" },
+	};
+	CHECK(dict.size() == 4);
+	CHECK(dict[0] == "int");
+	CHECK(PackedStringArray(dict["packed_string_array"])[2] == "values");
+	CHECK(Dictionary(dict["key"])["nested"] == Variant(200));
+	CHECK(dict[Vector2()] == "v2");
+
+	TypedDictionary<double, double> tdict{
+		{ 0.0, 1.0 },
+		{ 5.0, 2.0 },
+	};
+	CHECK_EQ(tdict[0.0], Variant(1.0));
+	CHECK_EQ(tdict[5.0], Variant(2.0));
+}
+
 TEST_CASE("[Dictionary] get_key_lists()") {
 	Dictionary map;
 	List<Variant> keys;