Skip to content

Commit 4ffea9e

Browse files
committed
Added initialiser list support to TypedDictionary
1 parent f3deed0 commit 4ffea9e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

include/godot_cpp/templates/pair.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#pragma once
3232

33+
#include <godot_cpp/templates/hashfuncs.hpp>
34+
3335
namespace godot {
3436

3537
template <typename F, typename S>

include/godot_cpp/variant/typed_dictionary.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#pragma once
3232

3333
#include <godot_cpp/core/type_info.hpp>
34+
#include <godot_cpp/templates/pair.hpp>
3435
#include <godot_cpp/variant/dictionary.hpp>
3536
#include <godot_cpp/variant/variant.hpp>
3637

@@ -57,6 +58,13 @@ class TypedDictionary : public Dictionary {
5758
_FORCE_INLINE_ TypedDictionary() {
5859
set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
5960
}
61+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<K, V>> p_init) :
62+
Dictionary() {
63+
set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
64+
for (const KeyValue<K, V> &E : p_init) {
65+
operator[](E.key) = E.value;
66+
}
67+
}
6068
};
6169

6270
//specialization for the rest of variant types
@@ -83,6 +91,13 @@ class TypedDictionary : public Dictionary {
8391
_FORCE_INLINE_ TypedDictionary() { \
8492
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
8593
} \
94+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<T, m_type>> p_init) : \
95+
Dictionary() { \
96+
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
97+
for (const KeyValue<T, m_type> &E : p_init) { \
98+
operator[](E.key) = E.value; \
99+
} \
100+
} \
86101
}; \
87102
template <typename T> \
88103
class TypedDictionary<m_type, T> : public Dictionary { \
@@ -105,6 +120,13 @@ class TypedDictionary : public Dictionary {
105120
_FORCE_INLINE_ TypedDictionary() { \
106121
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
107122
} \
123+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<m_type, T>> p_init) : \
124+
Dictionary() { \
125+
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, StringName(), Variant()); \
126+
for (const KeyValue<m_type, T> &E : p_init) { \
127+
operator[](E.key) = E.value; \
128+
} \
129+
} \
108130
};
109131

110132
#define MAKE_TYPED_DICTIONARY_EXPANDED(m_type_key, m_variant_type_key, m_type_value, m_variant_type_value) \
@@ -129,6 +151,13 @@ class TypedDictionary : public Dictionary {
129151
_FORCE_INLINE_ TypedDictionary() { \
130152
set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant()); \
131153
} \
154+
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<m_type_key, m_type_value>> p_init) : \
155+
Dictionary() { \
156+
set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant()); \
157+
for (const KeyValue<m_type_key, m_type_value> &E : p_init) { \
158+
operator[](E.key) = E.value; \
159+
} \
160+
} \
132161
};
133162

134163
#define MAKE_TYPED_DICTIONARY_NIL(m_type, m_variant_type) \

0 commit comments

Comments
 (0)