1 /*************************************************************************/
2 /* dictionary.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "gdnative/dictionary.h"
32
33 #include "core/variant.h"
34 // core/variant.h before to avoid compile errors with MSVC
35 #include "core/dictionary.h"
36 #include "core/io/json.h"
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 static_assert(sizeof(godot_dictionary) == sizeof(Dictionary), "Dictionary size mismatch");
43
godot_dictionary_new(godot_dictionary * r_dest)44 void GDAPI godot_dictionary_new(godot_dictionary *r_dest) {
45 Dictionary *dest = (Dictionary *)r_dest;
46 memnew_placement(dest, Dictionary);
47 }
48
godot_dictionary_new_copy(godot_dictionary * r_dest,const godot_dictionary * p_src)49 void GDAPI godot_dictionary_new_copy(godot_dictionary *r_dest, const godot_dictionary *p_src) {
50 Dictionary *dest = (Dictionary *)r_dest;
51 const Dictionary *src = (const Dictionary *)p_src;
52 memnew_placement(dest, Dictionary(*src));
53 }
54
godot_dictionary_destroy(godot_dictionary * p_self)55 void GDAPI godot_dictionary_destroy(godot_dictionary *p_self) {
56 Dictionary *self = (Dictionary *)p_self;
57 self->~Dictionary();
58 }
59
godot_dictionary_duplicate(const godot_dictionary * p_self,const godot_bool p_deep)60 godot_dictionary GDAPI godot_dictionary_duplicate(const godot_dictionary *p_self, const godot_bool p_deep) {
61 const Dictionary *self = (const Dictionary *)p_self;
62 godot_dictionary res;
63 Dictionary *val = (Dictionary *)&res;
64 memnew_placement(val, Dictionary);
65 *val = self->duplicate(p_deep);
66 return res;
67 }
68
godot_dictionary_size(const godot_dictionary * p_self)69 godot_int GDAPI godot_dictionary_size(const godot_dictionary *p_self) {
70 const Dictionary *self = (const Dictionary *)p_self;
71 return self->size();
72 }
73
godot_dictionary_empty(const godot_dictionary * p_self)74 godot_bool GDAPI godot_dictionary_empty(const godot_dictionary *p_self) {
75 const Dictionary *self = (const Dictionary *)p_self;
76 return self->empty();
77 }
78
godot_dictionary_clear(godot_dictionary * p_self)79 void GDAPI godot_dictionary_clear(godot_dictionary *p_self) {
80 Dictionary *self = (Dictionary *)p_self;
81 self->clear();
82 }
83
godot_dictionary_has(const godot_dictionary * p_self,const godot_variant * p_key)84 godot_bool GDAPI godot_dictionary_has(const godot_dictionary *p_self, const godot_variant *p_key) {
85 const Dictionary *self = (const Dictionary *)p_self;
86 const Variant *key = (const Variant *)p_key;
87 return self->has(*key);
88 }
89
godot_dictionary_has_all(const godot_dictionary * p_self,const godot_array * p_keys)90 godot_bool GDAPI godot_dictionary_has_all(const godot_dictionary *p_self, const godot_array *p_keys) {
91 const Dictionary *self = (const Dictionary *)p_self;
92 const Array *keys = (const Array *)p_keys;
93 return self->has_all(*keys);
94 }
95
godot_dictionary_erase(godot_dictionary * p_self,const godot_variant * p_key)96 void GDAPI godot_dictionary_erase(godot_dictionary *p_self, const godot_variant *p_key) {
97 Dictionary *self = (Dictionary *)p_self;
98 const Variant *key = (const Variant *)p_key;
99 self->erase(*key);
100 }
101
godot_dictionary_hash(const godot_dictionary * p_self)102 godot_int GDAPI godot_dictionary_hash(const godot_dictionary *p_self) {
103 const Dictionary *self = (const Dictionary *)p_self;
104 return self->hash();
105 }
106
godot_dictionary_keys(const godot_dictionary * p_self)107 godot_array GDAPI godot_dictionary_keys(const godot_dictionary *p_self) {
108 godot_array dest;
109 const Dictionary *self = (const Dictionary *)p_self;
110 memnew_placement(&dest, Array(self->keys()));
111 return dest;
112 }
113
godot_dictionary_values(const godot_dictionary * p_self)114 godot_array GDAPI godot_dictionary_values(const godot_dictionary *p_self) {
115 godot_array dest;
116 const Dictionary *self = (const Dictionary *)p_self;
117 memnew_placement(&dest, Array(self->values()));
118 return dest;
119 }
120
godot_dictionary_get(const godot_dictionary * p_self,const godot_variant * p_key)121 godot_variant GDAPI godot_dictionary_get(const godot_dictionary *p_self, const godot_variant *p_key) {
122 godot_variant raw_dest;
123 Variant *dest = (Variant *)&raw_dest;
124 const Dictionary *self = (const Dictionary *)p_self;
125 const Variant *key = (const Variant *)p_key;
126 memnew_placement(dest, Variant(self->operator[](*key)));
127 return raw_dest;
128 }
129
godot_dictionary_set(godot_dictionary * p_self,const godot_variant * p_key,const godot_variant * p_value)130 void GDAPI godot_dictionary_set(godot_dictionary *p_self, const godot_variant *p_key, const godot_variant *p_value) {
131 Dictionary *self = (Dictionary *)p_self;
132 const Variant *key = (const Variant *)p_key;
133 const Variant *value = (const Variant *)p_value;
134 self->operator[](*key) = *value;
135 }
136
godot_dictionary_operator_index(godot_dictionary * p_self,const godot_variant * p_key)137 godot_variant GDAPI *godot_dictionary_operator_index(godot_dictionary *p_self, const godot_variant *p_key) {
138 Dictionary *self = (Dictionary *)p_self;
139 const Variant *key = (const Variant *)p_key;
140 return (godot_variant *)&self->operator[](*key);
141 }
142
godot_dictionary_operator_index_const(const godot_dictionary * p_self,const godot_variant * p_key)143 const godot_variant GDAPI *godot_dictionary_operator_index_const(const godot_dictionary *p_self, const godot_variant *p_key) {
144 const Dictionary *self = (const Dictionary *)p_self;
145 const Variant *key = (const Variant *)p_key;
146 return (const godot_variant *)&self->operator[](*key);
147 }
148
godot_dictionary_next(const godot_dictionary * p_self,const godot_variant * p_key)149 godot_variant GDAPI *godot_dictionary_next(const godot_dictionary *p_self, const godot_variant *p_key) {
150 Dictionary *self = (Dictionary *)p_self;
151 const Variant *key = (const Variant *)p_key;
152 return (godot_variant *)self->next(key);
153 }
154
godot_dictionary_operator_equal(const godot_dictionary * p_self,const godot_dictionary * p_b)155 godot_bool GDAPI godot_dictionary_operator_equal(const godot_dictionary *p_self, const godot_dictionary *p_b) {
156 const Dictionary *self = (const Dictionary *)p_self;
157 const Dictionary *b = (const Dictionary *)p_b;
158 return *self == *b;
159 }
160
godot_dictionary_to_json(const godot_dictionary * p_self)161 godot_string GDAPI godot_dictionary_to_json(const godot_dictionary *p_self) {
162 godot_string raw_dest;
163 String *dest = (String *)&raw_dest;
164 const Dictionary *self = (const Dictionary *)p_self;
165 memnew_placement(dest, String(JSON::print(Variant(*self))));
166 return raw_dest;
167 }
168
169 // GDNative core 1.1
170
godot_dictionary_erase_with_return(godot_dictionary * p_self,const godot_variant * p_key)171 godot_bool GDAPI godot_dictionary_erase_with_return(godot_dictionary *p_self, const godot_variant *p_key) {
172 Dictionary *self = (Dictionary *)p_self;
173 const Variant *key = (const Variant *)p_key;
174 return self->erase(*key);
175 }
176
godot_dictionary_get_with_default(const godot_dictionary * p_self,const godot_variant * p_key,const godot_variant * p_default)177 godot_variant GDAPI godot_dictionary_get_with_default(const godot_dictionary *p_self, const godot_variant *p_key, const godot_variant *p_default) {
178 const Dictionary *self = (const Dictionary *)p_self;
179 const Variant *key = (const Variant *)p_key;
180 const Variant *def = (const Variant *)p_default;
181
182 godot_variant raw_dest;
183 Variant *dest = (Variant *)&raw_dest;
184 memnew_placement(dest, Variant(self->get(*key, *def)));
185
186 return raw_dest;
187 }
188
189 #ifdef __cplusplus
190 }
191 #endif
192