Skip to content

Commit 335b490

Browse files
committed
Added initialiser list support to TypedDictionary
1 parent da064d8 commit 335b490

File tree

3 files changed

+79
-44
lines changed

3 files changed

+79
-44
lines changed

include/godot_cpp/classes/ref.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class Ref {
7070
}
7171

7272
public:
73+
static _FORCE_INLINE_ String get_class_static() {
74+
return T::get_class_static();
75+
}
76+
7377
_FORCE_INLINE_ bool operator==(const T *p_ptr) const {
7478
return reference == p_ptr;
7579
}

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: 73 additions & 44 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,54 +58,75 @@ 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
6371

64-
#define MAKE_TYPED_DICTIONARY_WITH_OBJECT(m_type, m_variant_type) \
65-
template <typename T> \
66-
class TypedDictionary<T, m_type> : public Dictionary { \
67-
public: \
68-
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
69-
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
70-
Dictionary::operator=(p_dictionary); \
71-
} \
72-
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
73-
TypedDictionary(Dictionary(p_variant)) { \
74-
} \
75-
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
76-
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
77-
if (is_same_typed(p_dictionary)) { \
78-
Dictionary::operator=(p_dictionary); \
79-
} else { \
80-
assign(p_dictionary); \
81-
} \
82-
} \
83-
_FORCE_INLINE_ TypedDictionary() { \
84-
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
85-
} \
86-
}; \
87-
template <typename T> \
88-
class TypedDictionary<m_type, T> : public Dictionary { \
89-
public: \
90-
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
91-
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
92-
Dictionary::operator=(p_dictionary); \
93-
} \
94-
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
95-
TypedDictionary(Dictionary(p_variant)) { \
96-
} \
97-
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
98-
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
99-
if (is_same_typed(p_dictionary)) { \
100-
Dictionary::operator=(p_dictionary); \
101-
} else { \
102-
assign(p_dictionary); \
103-
} \
104-
} \
105-
_FORCE_INLINE_ TypedDictionary() { \
106-
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
107-
} \
72+
#define MAKE_TYPED_DICTIONARY_WITH_OBJECT(m_type, m_variant_type) \
73+
template <typename T> \
74+
class TypedDictionary<T, m_type> : public Dictionary { \
75+
public: \
76+
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
77+
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
78+
Dictionary::operator=(p_dictionary); \
79+
} \
80+
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
81+
TypedDictionary(Dictionary(p_variant)) { \
82+
} \
83+
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
84+
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
85+
if (is_same_typed(p_dictionary)) { \
86+
Dictionary::operator=(p_dictionary); \
87+
} else { \
88+
assign(p_dictionary); \
89+
} \
90+
} \
91+
_FORCE_INLINE_ TypedDictionary() { \
92+
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
93+
} \
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+
} \
101+
}; \
102+
template <typename T> \
103+
class TypedDictionary<m_type, T> : public Dictionary { \
104+
public: \
105+
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
106+
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
107+
Dictionary::operator=(p_dictionary); \
108+
} \
109+
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
110+
TypedDictionary(Dictionary(p_variant)) { \
111+
} \
112+
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
113+
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
114+
if (is_same_typed(p_dictionary)) { \
115+
Dictionary::operator=(p_dictionary); \
116+
} else { \
117+
assign(p_dictionary); \
118+
} \
119+
} \
120+
_FORCE_INLINE_ TypedDictionary() { \
121+
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
122+
} \
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, std::remove_pointer<T>::type::get_class_static(), 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)