You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-07 12:30:27 +00:00
Add std::initializer_list constructor for Dictionary.
This commit is contained in:
@@ -693,6 +693,15 @@ Dictionary::Dictionary() {
|
|||||||
_p->refcount.init();
|
_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() {
|
Dictionary::~Dictionary() {
|
||||||
_unref();
|
_unref();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
|
|
||||||
#include "core/string/ustring.h"
|
#include "core/string/ustring.h"
|
||||||
#include "core/templates/list.h"
|
#include "core/templates/list.h"
|
||||||
|
#include "core/templates/pair.h"
|
||||||
#include "core/variant/array.h"
|
#include "core/variant/array.h"
|
||||||
|
|
||||||
class Variant;
|
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_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(const Dictionary &p_from);
|
||||||
|
Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init);
|
||||||
Dictionary();
|
Dictionary();
|
||||||
~Dictionary();
|
~Dictionary();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -59,6 +59,14 @@ public:
|
|||||||
_FORCE_INLINE_ TypedDictionary() {
|
_FORCE_INLINE_ TypedDictionary() {
|
||||||
set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
|
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>
|
template <typename K, typename V>
|
||||||
@@ -135,6 +143,13 @@ struct GetTypeInfo<const TypedDictionary<K, V> &> {
|
|||||||
_FORCE_INLINE_ TypedDictionary() { \
|
_FORCE_INLINE_ TypedDictionary() { \
|
||||||
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
|
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> \
|
template <typename T> \
|
||||||
struct GetTypeInfo<TypedDictionary<T, m_type>> { \
|
struct GetTypeInfo<TypedDictionary<T, m_type>> { \
|
||||||
@@ -217,6 +232,13 @@ struct GetTypeInfo<const TypedDictionary<K, V> &> {
|
|||||||
_FORCE_INLINE_ TypedDictionary() { \
|
_FORCE_INLINE_ TypedDictionary() { \
|
||||||
set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant()); \
|
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 <> \
|
template <> \
|
||||||
struct GetTypeInfo<TypedDictionary<m_type_key, m_type_value>> { \
|
struct GetTypeInfo<TypedDictionary<m_type_key, m_type_value>> { \
|
||||||
|
|||||||
@@ -95,6 +95,27 @@ TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
|
|||||||
CHECK(map.size() == length);
|
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()") {
|
TEST_CASE("[Dictionary] get_key_lists()") {
|
||||||
Dictionary map;
|
Dictionary map;
|
||||||
List<Variant> keys;
|
List<Variant> keys;
|
||||||
|
|||||||
Reference in New Issue
Block a user