1 /*************************************************************************/
2 /* dictionary.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 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 #include "dictionary.h"
31 #include "io/json.h"
32 #include "safe_refcount.h"
33 #include "variant.h"
34
35 struct _DictionaryVariantHash {
36
hash_DictionaryVariantHash37 static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
38 };
39
40 struct DictionaryPrivate {
41
42 SafeRefCount refcount;
43 HashMap<Variant, Variant, _DictionaryVariantHash> variant_map;
44 bool shared;
45 };
46
get_key_list(List<Variant> * p_keys) const47 void Dictionary::get_key_list(List<Variant> *p_keys) const {
48
49 _p->variant_map.get_key_list(p_keys);
50 }
51
_copy_on_write() const52 void Dictionary::_copy_on_write() const {
53
54 //make a copy of what we have
55 if (_p->shared)
56 return;
57
58 DictionaryPrivate *p = memnew(DictionaryPrivate);
59 p->shared = _p->shared;
60 p->variant_map = _p->variant_map;
61 p->refcount.init();
62 _unref();
63 _p = p;
64 }
65
operator [](const Variant & p_key)66 Variant &Dictionary::operator[](const Variant &p_key) {
67
68 _copy_on_write();
69
70 return _p->variant_map[p_key];
71 }
72
operator [](const Variant & p_key) const73 const Variant &Dictionary::operator[](const Variant &p_key) const {
74
75 return _p->variant_map[p_key];
76 }
getptr(const Variant & p_key) const77 const Variant *Dictionary::getptr(const Variant &p_key) const {
78
79 return _p->variant_map.getptr(p_key);
80 }
getptr(const Variant & p_key)81 Variant *Dictionary::getptr(const Variant &p_key) {
82
83 _copy_on_write();
84 return _p->variant_map.getptr(p_key);
85 }
86
get_valid(const Variant & p_key) const87 Variant Dictionary::get_valid(const Variant &p_key) const {
88
89 const Variant *v = getptr(p_key);
90 if (!v)
91 return Variant();
92 return *v;
93 }
94
size() const95 int Dictionary::size() const {
96
97 return _p->variant_map.size();
98 }
empty() const99 bool Dictionary::empty() const {
100
101 return !_p->variant_map.size();
102 }
103
has(const Variant & p_key) const104 bool Dictionary::has(const Variant &p_key) const {
105
106 return _p->variant_map.has(p_key);
107 }
108
has_all(const Array & p_keys) const109 bool Dictionary::has_all(const Array &p_keys) const {
110 for (int i = 0; i < p_keys.size(); i++) {
111 if (!has(p_keys[i])) {
112 return false;
113 }
114 }
115 return true;
116 }
117
erase(const Variant & p_key)118 void Dictionary::erase(const Variant &p_key) {
119 _copy_on_write();
120 _p->variant_map.erase(p_key);
121 }
122
operator ==(const Dictionary & p_dictionary) const123 bool Dictionary::operator==(const Dictionary &p_dictionary) const {
124
125 return _p == p_dictionary._p;
126 }
127
_ref(const Dictionary & p_from) const128 void Dictionary::_ref(const Dictionary &p_from) const {
129
130 //make a copy first (thread safe)
131 if (!p_from._p->refcount.ref())
132 return; // couldn't copy
133
134 //if this is the same, unreference the other one
135 if (p_from._p == _p) {
136 _p->refcount.unref();
137 return;
138 }
139 if (_p)
140 _unref();
141 _p = p_from._p;
142 }
143
clear()144 void Dictionary::clear() {
145
146 _copy_on_write();
147 _p->variant_map.clear();
148 }
149
is_shared() const150 bool Dictionary::is_shared() const {
151
152 return _p->shared;
153 }
154
_unref() const155 void Dictionary::_unref() const {
156
157 ERR_FAIL_COND(!_p);
158 if (_p->refcount.unref()) {
159 memdelete(_p);
160 }
161 _p = NULL;
162 }
hash() const163 uint32_t Dictionary::hash() const {
164
165 uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
166
167 List<Variant> keys;
168 get_key_list(&keys);
169
170 for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
171
172 h = hash_djb2_one_32(E->get().hash(), h);
173 h = hash_djb2_one_32(operator[](E->get()).hash(), h);
174 }
175
176 return h;
177 }
178
keys() const179 Array Dictionary::keys() const {
180
181 Array karr;
182 karr.resize(size());
183 const Variant *K = NULL;
184 int idx = 0;
185 while ((K = next(K))) {
186 karr[idx++] = (*K);
187 }
188 return karr;
189 }
190
values() const191 Array Dictionary::values() const {
192
193 Array varr;
194 varr.resize(size());
195 const Variant *key = NULL;
196 int i = 0;
197 while ((key = next(key))) {
198 varr[i++] = _p->variant_map[*key];
199 }
200 return varr;
201 }
202
next(const Variant * p_key) const203 const Variant *Dictionary::next(const Variant *p_key) const {
204
205 return _p->variant_map.next(p_key);
206 }
207
parse_json(const String & p_json)208 Error Dictionary::parse_json(const String &p_json) {
209
210 String errstr;
211 int errline = 0;
212 if (p_json != "") {
213 Error err = JSON::parse(p_json, *this, errstr, errline);
214 if (err != OK) {
215 ERR_EXPLAIN("Error parsing JSON: " + errstr + " at line: " + itos(errline));
216 ERR_FAIL_COND_V(err != OK, err);
217 }
218 }
219
220 return OK;
221 }
222
to_json() const223 String Dictionary::to_json() const {
224
225 return JSON::print(*this);
226 }
227
operator =(const Dictionary & p_dictionary)228 void Dictionary::operator=(const Dictionary &p_dictionary) {
229
230 _ref(p_dictionary);
231 }
232
Dictionary(const Dictionary & p_from)233 Dictionary::Dictionary(const Dictionary &p_from) {
234 _p = NULL;
235 _ref(p_from);
236 }
237
Dictionary(bool p_shared)238 Dictionary::Dictionary(bool p_shared) {
239
240 _p = memnew(DictionaryPrivate);
241 _p->refcount.init();
242 _p->shared = p_shared;
243 }
~Dictionary()244 Dictionary::~Dictionary() {
245
246 _unref();
247 }
248