-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathdictionary.tmpl.pxi
209 lines (184 loc) Β· 8.41 KB
/
dictionary.tmpl.pxi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{%- set gd_functions = cook_c_signatures("""
// GDAPI: 1.0
void godot_dictionary_new(godot_dictionary* r_dest)
void godot_dictionary_new_copy(godot_dictionary* r_dest, godot_dictionary* p_src)
void godot_dictionary_destroy(godot_dictionary* p_self)
godot_int godot_dictionary_size(godot_dictionary* p_self)
godot_bool godot_dictionary_empty(godot_dictionary* p_self)
void godot_dictionary_clear(godot_dictionary* p_self)
godot_bool godot_dictionary_has(godot_dictionary* p_self, godot_variant* p_key)
godot_bool godot_dictionary_has_all(godot_dictionary* p_self, godot_array* p_keys)
void godot_dictionary_erase(godot_dictionary* p_self, godot_variant* p_key)
godot_int godot_dictionary_hash(godot_dictionary* p_self)
godot_array godot_dictionary_keys(godot_dictionary* p_self)
godot_array godot_dictionary_values(godot_dictionary* p_self)
godot_variant godot_dictionary_get(godot_dictionary* p_self, godot_variant* p_key)
void godot_dictionary_set(godot_dictionary* p_self, godot_variant* p_key, godot_variant* p_value)
// godot_variant* godot_dictionary_operator_index(godot_dictionary* p_self, godot_variant* p_key)
// godot_variant* godot_dictionary_operator_index_const(godot_dictionary* p_self, godot_variant* p_key)
// godot_variant* godot_dictionary_next(godot_dictionary* p_self, godot_variant* p_key)
godot_bool godot_dictionary_operator_equal(godot_dictionary* p_self, godot_dictionary* p_b)
godot_string godot_dictionary_to_json(godot_dictionary* p_self)
godot_bool godot_dictionary_erase_with_return(godot_dictionary* p_self, godot_variant* p_key)
// GDAPI: 1.1
godot_variant godot_dictionary_get_with_default(godot_dictionary* p_self, godot_variant* p_key, godot_variant* p_default)
// GDAPI: 1.2
godot_dictionary godot_dictionary_duplicate(godot_dictionary* p_self, godot_bool p_deep)
""") -%}
{%- block pxd_header %}
{% endblock -%}
{%- block pyx_header %}
{% endblock -%}
@cython.final
cdef class Dictionary:
{% block cdef_attributes %}
cdef godot_dictionary _gd_data
cdef inline operator_update(self, Dictionary items)
cdef inline bint operator_equal(self, Dictionary other)
{% endblock %}
{% block python_defs %}
def __init__(self, iterable=None):
if not iterable:
gdapi10.godot_dictionary_new(&self._gd_data)
elif isinstance(iterable, Dictionary):
self._gd_data = gdapi12.godot_dictionary_duplicate(&(<Dictionary>iterable)._gd_data, False)
# TODO: handle Pool*Array
elif isinstance(iterable, dict):
gdapi10.godot_dictionary_new(&self._gd_data)
for k, v in iterable.items():
self[k] = v
else:
gdapi10.godot_dictionary_new(&self._gd_data)
try:
for k, v in iterable:
self[k] = v
except ValueError as exc:
raise ValueError("dictionary update sequence element has length 1; 2 is required")
def __dealloc__(self):
# /!\ if `__init__` is skipped, `_gd_data` must be initialized by
# hand otherwise we will get a segfault here
gdapi10.godot_dictionary_destroy(&self._gd_data)
def __repr__(self):
repr_dict = {}
for k, v in self.items():
if isinstance(k, GDString):
k = str(k)
if isinstance(v, GDString):
v = str(v)
repr_dict[k] = v
return f"<Dictionary({repr_dict})>"
def __getitem__(self, object key):
cdef godot_variant var_key
if not pyobj_to_godot_variant(key, &var_key):
raise TypeError(f"Cannot convert `{key!r}` to Godot Variant")
cdef godot_variant *p_var_ret = gdapi10.godot_dictionary_operator_index(&self._gd_data, &var_key)
gdapi10.godot_variant_destroy(&var_key)
if p_var_ret == NULL:
raise KeyError(key)
else:
return godot_variant_to_pyobj(p_var_ret)
{%set contains_specs = gd_functions['set'] | merge(pyname="__setitem__") %}
{{ render_method(**contains_specs) | indent }}
def __delitem__(self, object key):
cdef godot_variant var_key
if not pyobj_to_godot_variant(key, &var_key):
raise TypeError(f"Cannot convert `{key!r}` to Godot Variant")
cdef godot_bool ret = gdapi11.godot_dictionary_erase_with_return(&self._gd_data, &var_key)
gdapi10.godot_variant_destroy(&var_key)
if not ret:
raise KeyError(key)
def __iter__(self):
cdef godot_variant *p_key = NULL
# TODO: mid iteration mutation should throw exception ?
while True:
p_key = gdapi10.godot_dictionary_next(&self._gd_data, p_key)
if p_key == NULL:
return
yield godot_variant_to_pyobj(p_key)
def __copy__(self):
return self.duplicate(False)
def __deepcopy__(self):
return self.duplicate(True)
def get(self, object key, object default=None):
cdef godot_variant var_key
pyobj_to_godot_variant(key, &var_key)
cdef godot_variant var_ret
cdef godot_variant var_default
if default is not None:
pyobj_to_godot_variant(default, &var_default)
var_ret = gdapi11.godot_dictionary_get_with_default(&self._gd_data, &var_key, &var_default)
gdapi10.godot_variant_destroy(&var_default)
else:
var_ret = gdapi10.godot_dictionary_get(&self._gd_data, &var_key)
gdapi10.godot_variant_destroy(&var_key)
cdef object ret = godot_variant_to_pyobj(&var_ret)
gdapi10.godot_variant_destroy(&var_ret)
return ret
cdef inline operator_update(self, Dictionary items):
cdef godot_variant *p_value
cdef godot_variant *p_key = NULL
while True:
p_key = gdapi10.godot_dictionary_next(&items._gd_data, p_key)
if p_key == NULL:
break
p_value = gdapi10.godot_dictionary_operator_index(&items._gd_data, p_key)
gdapi10.godot_dictionary_set(&self._gd_data, p_key, p_value)
return self
def update(self, other):
cdef object k
cdef object v
if isinstance(other, Dictionary):
Dictionary.operator_update(self, other)
elif isinstance(other, dict):
for k, v in other.items():
self[k] = v
else:
raise TypeError("other must be godot.Dictionary or dict")
def items(self):
cdef godot_variant *p_key = NULL
cdef godot_variant *p_value
# TODO: mid iteration mutation should throw exception ?
while True:
p_key = gdapi10.godot_dictionary_next(&self._gd_data, p_key)
if p_key == NULL:
return
p_value = gdapi10.godot_dictionary_operator_index(&self._gd_data, p_key)
yield godot_variant_to_pyobj(p_key), godot_variant_to_pyobj(p_value)
cdef inline bint operator_equal(self, Dictionary other):
if other is None:
return False
cdef godot_int size = self.size()
if size != other.size():
return False
# TODO: gdnative should provide a function to do that
return dict(self) == dict(other)
def __eq__(self, other):
try:
return Dictionary.operator_equal(self, <Dictionary?>other)
except TypeError:
return False
def __ne__(self, other):
try:
return not Dictionary.operator_equal(self, <Dictionary?>other)
except TypeError:
return True
{%set len_specs = gd_functions['size'] | merge(pyname="__len__") %}
{{ render_method(**len_specs) | indent }}
{%set hash_specs = gd_functions['hash'] | merge(pyname="__hash__") %}
{{ render_method(**hash_specs) | indent }}
{%set contains_specs = gd_functions['has'] | merge(pyname="__contains__") %}
{{ render_method(**contains_specs) | indent }}
{{ render_method(**gd_functions["duplicate"]) | indent }}
{{ render_method(**gd_functions["size"]) | indent }}
{{ render_method(**gd_functions["empty"]) | indent }}
{{ render_method(**gd_functions["clear"]) | indent }}
{{ render_method(**gd_functions["has"]) | indent }}
{{ render_method(**gd_functions["has_all"]) | indent }}
{{ render_method(**gd_functions["erase"]) | indent }}
{{ render_method(**gd_functions["hash"]) | indent }}
{{ render_method(**gd_functions["keys"]) | indent }}
{{ render_method(**gd_functions["values"]) | indent }}
{{ render_method(**gd_functions["to_json"]) | indent }}
{% endblock %}
{%- block python_consts %}
{% endblock %}