Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/variant/dictionary.cpp
9903 views
1
/**************************************************************************/
2
/* dictionary.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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 "dictionary.h"
32
33
#include "core/templates/hash_map.h"
34
#include "core/templates/safe_refcount.h"
35
#include "core/variant/container_type_validate.h"
36
#include "core/variant/variant.h"
37
// required in this order by VariantInternal, do not remove this comment.
38
#include "core/object/class_db.h"
39
#include "core/object/object.h"
40
#include "core/variant/type_info.h"
41
#include "core/variant/variant_internal.h"
42
43
struct DictionaryPrivate {
44
SafeRefCount refcount;
45
Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
46
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map;
47
ContainerTypeValidate typed_key;
48
ContainerTypeValidate typed_value;
49
Variant *typed_fallback = nullptr; // Allows a typed dictionary to return dummy values when attempting an invalid access.
50
};
51
52
Dictionary::ConstIterator Dictionary::begin() const {
53
return _p->variant_map.begin();
54
}
55
56
Dictionary::ConstIterator Dictionary::end() const {
57
return _p->variant_map.end();
58
}
59
60
LocalVector<Variant> Dictionary::get_key_list() const {
61
LocalVector<Variant> keys;
62
63
keys.reserve(_p->variant_map.size());
64
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
65
keys.push_back(E.key);
66
}
67
return keys;
68
}
69
70
Variant Dictionary::get_key_at_index(int p_index) const {
71
int index = 0;
72
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
73
if (index == p_index) {
74
return E.key;
75
}
76
index++;
77
}
78
79
return Variant();
80
}
81
82
Variant Dictionary::get_value_at_index(int p_index) const {
83
int index = 0;
84
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
85
if (index == p_index) {
86
return E.value;
87
}
88
index++;
89
}
90
91
return Variant();
92
}
93
94
// WARNING: This operator does not validate the value type. For scripting/extensions this is
95
// done in `variant_setget.cpp`. Consider using `set()` if the data might be invalid.
96
Variant &Dictionary::operator[](const Variant &p_key) {
97
Variant key = p_key;
98
if (unlikely(!_p->typed_key.validate(key, "use `operator[]`"))) {
99
if (unlikely(!_p->typed_fallback)) {
100
_p->typed_fallback = memnew(Variant);
101
}
102
VariantInternal::initialize(_p->typed_fallback, _p->typed_value.type);
103
return *_p->typed_fallback;
104
} else if (unlikely(_p->read_only)) {
105
if (likely(_p->variant_map.has(key))) {
106
*_p->read_only = _p->variant_map[key];
107
} else {
108
VariantInternal::initialize(_p->read_only, _p->typed_value.type);
109
}
110
return *_p->read_only;
111
} else {
112
const uint32_t old_size = _p->variant_map.size();
113
Variant &value = _p->variant_map[key];
114
if (_p->variant_map.size() > old_size) {
115
VariantInternal::initialize(&value, _p->typed_value.type);
116
}
117
return value;
118
}
119
}
120
121
const Variant &Dictionary::operator[](const Variant &p_key) const {
122
Variant key = p_key;
123
if (unlikely(!_p->typed_key.validate(key, "use `operator[]`"))) {
124
if (unlikely(!_p->typed_fallback)) {
125
_p->typed_fallback = memnew(Variant);
126
}
127
VariantInternal::initialize(_p->typed_fallback, _p->typed_value.type);
128
return *_p->typed_fallback;
129
} else {
130
// Will not insert key, so no initialization is necessary.
131
return _p->variant_map[key];
132
}
133
}
134
135
const Variant *Dictionary::getptr(const Variant &p_key) const {
136
Variant key = p_key;
137
if (unlikely(!_p->typed_key.validate(key, "getptr"))) {
138
return nullptr;
139
}
140
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
141
if (!E) {
142
return nullptr;
143
}
144
return &E->value;
145
}
146
147
// WARNING: This method does not validate the value type.
148
Variant *Dictionary::getptr(const Variant &p_key) {
149
Variant key = p_key;
150
if (unlikely(!_p->typed_key.validate(key, "getptr"))) {
151
return nullptr;
152
}
153
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E(_p->variant_map.find(key));
154
if (!E) {
155
return nullptr;
156
}
157
if (unlikely(_p->read_only != nullptr)) {
158
*_p->read_only = E->value;
159
return _p->read_only;
160
} else {
161
return &E->value;
162
}
163
}
164
165
Variant Dictionary::get_valid(const Variant &p_key) const {
166
Variant key = p_key;
167
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get_valid"), Variant());
168
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
169
170
if (!E) {
171
return Variant();
172
}
173
return E->value;
174
}
175
176
Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
177
Variant key = p_key;
178
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
179
const Variant *result = getptr(key);
180
if (!result) {
181
return p_default;
182
}
183
184
return *result;
185
}
186
187
Variant Dictionary::get_or_add(const Variant &p_key, const Variant &p_default) {
188
Variant key = p_key;
189
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
190
const Variant *result = getptr(key);
191
if (!result) {
192
Variant value = p_default;
193
ERR_FAIL_COND_V(!_p->typed_value.validate(value, "add"), value);
194
operator[](key) = value;
195
return value;
196
}
197
return *result;
198
}
199
200
bool Dictionary::set(const Variant &p_key, const Variant &p_value) {
201
ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
202
Variant key = p_key;
203
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "set"), false);
204
Variant value = p_value;
205
ERR_FAIL_COND_V(!_p->typed_value.validate(value, "set"), false);
206
_p->variant_map[key] = value;
207
return true;
208
}
209
210
int Dictionary::size() const {
211
return _p->variant_map.size();
212
}
213
214
bool Dictionary::is_empty() const {
215
return !_p->variant_map.size();
216
}
217
218
bool Dictionary::has(const Variant &p_key) const {
219
Variant key = p_key;
220
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has'"), false);
221
return _p->variant_map.has(key);
222
}
223
224
bool Dictionary::has_all(const Array &p_keys) const {
225
for (int i = 0; i < p_keys.size(); i++) {
226
Variant key = p_keys[i];
227
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has_all'"), false);
228
if (!_p->variant_map.has(key)) {
229
return false;
230
}
231
}
232
return true;
233
}
234
235
Variant Dictionary::find_key(const Variant &p_value) const {
236
Variant value = p_value;
237
ERR_FAIL_COND_V(!_p->typed_value.validate(value, "find_key"), Variant());
238
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
239
if (E.value == value) {
240
return E.key;
241
}
242
}
243
return Variant();
244
}
245
246
bool Dictionary::erase(const Variant &p_key) {
247
Variant key = p_key;
248
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "erase"), false);
249
ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
250
return _p->variant_map.erase(key);
251
}
252
253
bool Dictionary::operator==(const Dictionary &p_dictionary) const {
254
return recursive_equal(p_dictionary, 0);
255
}
256
257
bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
258
return !recursive_equal(p_dictionary, 0);
259
}
260
261
bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_count) const {
262
// Cheap checks
263
if (_p == p_dictionary._p) {
264
return true;
265
}
266
if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
267
return false;
268
}
269
270
// Heavy O(n) check
271
if (recursion_count > MAX_RECURSION) {
272
ERR_PRINT("Max recursion reached");
273
return true;
274
}
275
recursion_count++;
276
for (const KeyValue<Variant, Variant> &this_E : _p->variant_map) {
277
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator other_E(p_dictionary._p->variant_map.find(this_E.key));
278
if (!other_E || !this_E.value.hash_compare(other_E->value, recursion_count, false)) {
279
return false;
280
}
281
}
282
return true;
283
}
284
285
void Dictionary::_ref(const Dictionary &p_from) const {
286
//make a copy first (thread safe)
287
if (!p_from._p->refcount.ref()) {
288
return; // couldn't copy
289
}
290
291
//if this is the same, unreference the other one
292
if (p_from._p == _p) {
293
_p->refcount.unref();
294
return;
295
}
296
if (_p) {
297
_unref();
298
}
299
_p = p_from._p;
300
}
301
302
void Dictionary::clear() {
303
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
304
_p->variant_map.clear();
305
}
306
307
struct _DictionaryVariantSort {
308
_FORCE_INLINE_ bool operator()(const KeyValue<Variant, Variant> &p_l, const KeyValue<Variant, Variant> &p_r) const {
309
bool valid = false;
310
Variant res;
311
Variant::evaluate(Variant::OP_LESS, p_l.key, p_r.key, res, valid);
312
if (!valid) {
313
res = false;
314
}
315
return res;
316
}
317
};
318
319
void Dictionary::sort() {
320
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
321
_p->variant_map.sort_custom<_DictionaryVariantSort>();
322
}
323
324
void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
325
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
326
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
327
Variant key = E.key;
328
Variant value = E.value;
329
ERR_FAIL_COND(!_p->typed_key.validate(key, "merge"));
330
ERR_FAIL_COND(!_p->typed_value.validate(value, "merge"));
331
if (p_overwrite || !has(key)) {
332
operator[](key) = value;
333
}
334
}
335
}
336
337
Dictionary Dictionary::merged(const Dictionary &p_dictionary, bool p_overwrite) const {
338
Dictionary ret = duplicate();
339
ret.merge(p_dictionary, p_overwrite);
340
return ret;
341
}
342
343
void Dictionary::_unref() const {
344
ERR_FAIL_NULL(_p);
345
if (_p->refcount.unref()) {
346
if (_p->read_only) {
347
memdelete(_p->read_only);
348
}
349
if (_p->typed_fallback) {
350
memdelete(_p->typed_fallback);
351
}
352
memdelete(_p);
353
}
354
_p = nullptr;
355
}
356
357
uint32_t Dictionary::hash() const {
358
return recursive_hash(0);
359
}
360
361
uint32_t Dictionary::recursive_hash(int recursion_count) const {
362
if (recursion_count > MAX_RECURSION) {
363
ERR_PRINT("Max recursion reached");
364
return 0;
365
}
366
367
uint32_t h = hash_murmur3_one_32(Variant::DICTIONARY);
368
369
recursion_count++;
370
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
371
h = hash_murmur3_one_32(E.key.recursive_hash(recursion_count), h);
372
h = hash_murmur3_one_32(E.value.recursive_hash(recursion_count), h);
373
}
374
375
return hash_fmix32(h);
376
}
377
378
Array Dictionary::keys() const {
379
Array varr;
380
if (is_typed_key()) {
381
varr.set_typed(get_typed_key_builtin(), get_typed_key_class_name(), get_typed_key_script());
382
}
383
if (_p->variant_map.is_empty()) {
384
return varr;
385
}
386
387
varr.resize(size());
388
389
int i = 0;
390
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
391
varr[i] = E.key;
392
i++;
393
}
394
395
return varr;
396
}
397
398
Array Dictionary::values() const {
399
Array varr;
400
if (is_typed_value()) {
401
varr.set_typed(get_typed_value_builtin(), get_typed_value_class_name(), get_typed_value_script());
402
}
403
if (_p->variant_map.is_empty()) {
404
return varr;
405
}
406
407
varr.resize(size());
408
409
int i = 0;
410
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
411
varr[i] = E.value;
412
i++;
413
}
414
415
return varr;
416
}
417
418
void Dictionary::assign(const Dictionary &p_dictionary) {
419
const ContainerTypeValidate &typed_key = _p->typed_key;
420
const ContainerTypeValidate &typed_key_source = p_dictionary._p->typed_key;
421
422
const ContainerTypeValidate &typed_value = _p->typed_value;
423
const ContainerTypeValidate &typed_value_source = p_dictionary._p->typed_value;
424
425
if ((typed_key == typed_key_source || typed_key.type == Variant::NIL || (typed_key_source.type == Variant::OBJECT && typed_key.can_reference(typed_key_source))) &&
426
(typed_value == typed_value_source || typed_value.type == Variant::NIL || (typed_value_source.type == Variant::OBJECT && typed_value.can_reference(typed_value_source)))) {
427
// From same to same or,
428
// from anything to variants or,
429
// from subclasses to base classes.
430
_p->variant_map = p_dictionary._p->variant_map;
431
return;
432
}
433
434
int size = p_dictionary._p->variant_map.size();
435
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map = HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>(size);
436
437
Vector<Variant> key_array;
438
key_array.resize(size);
439
Variant *key_data = key_array.ptrw();
440
441
Vector<Variant> value_array;
442
value_array.resize(size);
443
Variant *value_data = value_array.ptrw();
444
445
if (typed_key == typed_key_source || typed_key.type == Variant::NIL || (typed_key_source.type == Variant::OBJECT && typed_key.can_reference(typed_key_source))) {
446
// From same to same or,
447
// from anything to variants or,
448
// from subclasses to base classes.
449
int i = 0;
450
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
451
const Variant *key = &E.key;
452
key_data[i++] = *key;
453
}
454
} else if ((typed_key_source.type == Variant::NIL && typed_key.type == Variant::OBJECT) || (typed_key_source.type == Variant::OBJECT && typed_key_source.can_reference(typed_key))) {
455
// From variants to objects or,
456
// from base classes to subclasses.
457
int i = 0;
458
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
459
const Variant *key = &E.key;
460
if (key->get_type() != Variant::NIL && (key->get_type() != Variant::OBJECT || !typed_key.validate_object(*key, "assign"))) {
461
ERR_FAIL_MSG(vformat(R"(Unable to convert key from "%s" to "%s".)", Variant::get_type_name(key->get_type()), Variant::get_type_name(typed_key.type)));
462
}
463
key_data[i++] = *key;
464
}
465
} else if (typed_key.type == Variant::OBJECT || typed_key_source.type == Variant::OBJECT) {
466
ERR_FAIL_MSG(vformat(R"(Cannot assign contents of "Dictionary[%s, %s]" to "Dictionary[%s, %s]".)", Variant::get_type_name(typed_key_source.type), Variant::get_type_name(typed_value_source.type),
467
Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
468
} else if (typed_key_source.type == Variant::NIL && typed_key.type != Variant::OBJECT) {
469
// From variants to primitives.
470
int i = 0;
471
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
472
const Variant *key = &E.key;
473
if (key->get_type() == typed_key.type) {
474
key_data[i++] = *key;
475
continue;
476
}
477
if (!Variant::can_convert_strict(key->get_type(), typed_key.type)) {
478
ERR_FAIL_MSG(vformat(R"(Unable to convert key from "%s" to "%s".)", Variant::get_type_name(key->get_type()), Variant::get_type_name(typed_key.type)));
479
}
480
Callable::CallError ce;
481
Variant::construct(typed_key.type, key_data[i++], &key, 1, ce);
482
ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert key from "%s" to "%s".)", Variant::get_type_name(key->get_type()), Variant::get_type_name(typed_key.type)));
483
}
484
} else if (Variant::can_convert_strict(typed_key_source.type, typed_key.type)) {
485
// From primitives to different convertible primitives.
486
int i = 0;
487
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
488
const Variant *key = &E.key;
489
Callable::CallError ce;
490
Variant::construct(typed_key.type, key_data[i++], &key, 1, ce);
491
ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert key from "%s" to "%s".)", Variant::get_type_name(key->get_type()), Variant::get_type_name(typed_key.type)));
492
}
493
} else {
494
ERR_FAIL_MSG(vformat(R"(Cannot assign contents of "Dictionary[%s, %s]" to "Dictionary[%s, %s].)", Variant::get_type_name(typed_key_source.type), Variant::get_type_name(typed_value_source.type),
495
Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
496
}
497
498
if (typed_value == typed_value_source || typed_value.type == Variant::NIL || (typed_value_source.type == Variant::OBJECT && typed_value.can_reference(typed_value_source))) {
499
// From same to same or,
500
// from anything to variants or,
501
// from subclasses to base classes.
502
int i = 0;
503
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
504
const Variant *value = &E.value;
505
value_data[i++] = *value;
506
}
507
} else if (((typed_value_source.type == Variant::NIL && typed_value.type == Variant::OBJECT) || (typed_value_source.type == Variant::OBJECT && typed_value_source.can_reference(typed_value)))) {
508
// From variants to objects or,
509
// from base classes to subclasses.
510
int i = 0;
511
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
512
const Variant *value = &E.value;
513
if (value->get_type() != Variant::NIL && (value->get_type() != Variant::OBJECT || !typed_value.validate_object(*value, "assign"))) {
514
ERR_FAIL_MSG(vformat(R"(Unable to convert value at key "%s" from "%s" to "%s".)", key_data[i], Variant::get_type_name(value->get_type()), Variant::get_type_name(typed_value.type)));
515
}
516
value_data[i++] = *value;
517
}
518
} else if (typed_value.type == Variant::OBJECT || typed_value_source.type == Variant::OBJECT) {
519
ERR_FAIL_MSG(vformat(R"(Cannot assign contents of "Dictionary[%s, %s]" to "Dictionary[%s, %s]".)", Variant::get_type_name(typed_key_source.type), Variant::get_type_name(typed_value_source.type),
520
Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
521
} else if (typed_value_source.type == Variant::NIL && typed_value.type != Variant::OBJECT) {
522
// From variants to primitives.
523
int i = 0;
524
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
525
const Variant *value = &E.value;
526
if (value->get_type() == typed_value.type) {
527
value_data[i++] = *value;
528
continue;
529
}
530
if (!Variant::can_convert_strict(value->get_type(), typed_value.type)) {
531
ERR_FAIL_MSG(vformat(R"(Unable to convert value at key "%s" from "%s" to "%s".)", key_data[i], Variant::get_type_name(value->get_type()), Variant::get_type_name(typed_value.type)));
532
}
533
Callable::CallError ce;
534
Variant::construct(typed_value.type, value_data[i++], &value, 1, ce);
535
ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert value at key "%s" from "%s" to "%s".)", key_data[i - 1], Variant::get_type_name(value->get_type()), Variant::get_type_name(typed_value.type)));
536
}
537
} else if (Variant::can_convert_strict(typed_value_source.type, typed_value.type)) {
538
// From primitives to different convertible primitives.
539
int i = 0;
540
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
541
const Variant *value = &E.value;
542
Callable::CallError ce;
543
Variant::construct(typed_value.type, value_data[i++], &value, 1, ce);
544
ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert value at key "%s" from "%s" to "%s".)", key_data[i - 1], Variant::get_type_name(value->get_type()), Variant::get_type_name(typed_value.type)));
545
}
546
} else {
547
ERR_FAIL_MSG(vformat(R"(Cannot assign contents of "Dictionary[%s, %s]" to "Dictionary[%s, %s].)", Variant::get_type_name(typed_key_source.type), Variant::get_type_name(typed_value_source.type),
548
Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
549
}
550
551
for (int i = 0; i < size; i++) {
552
variant_map.insert(key_data[i], value_data[i]);
553
}
554
555
_p->variant_map = variant_map;
556
}
557
558
const Variant *Dictionary::next(const Variant *p_key) const {
559
if (p_key == nullptr) {
560
// caller wants to get the first element
561
if (_p->variant_map.begin()) {
562
return &_p->variant_map.begin()->key;
563
}
564
return nullptr;
565
}
566
Variant key = *p_key;
567
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "next"), nullptr);
568
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(key);
569
570
if (!E) {
571
return nullptr;
572
}
573
574
++E;
575
576
if (E) {
577
return &E->key;
578
}
579
580
return nullptr;
581
}
582
583
Dictionary Dictionary::duplicate(bool p_deep) const {
584
return recursive_duplicate(p_deep, RESOURCE_DEEP_DUPLICATE_NONE, 0);
585
}
586
587
Dictionary Dictionary::duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode) const {
588
return recursive_duplicate(true, p_deep_subresources_mode, 0);
589
}
590
591
void Dictionary::make_read_only() {
592
if (_p->read_only == nullptr) {
593
_p->read_only = memnew(Variant);
594
}
595
}
596
bool Dictionary::is_read_only() const {
597
return _p->read_only != nullptr;
598
}
599
600
Dictionary Dictionary::recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const {
601
Dictionary n;
602
n._p->typed_key = _p->typed_key;
603
n._p->typed_value = _p->typed_value;
604
605
if (recursion_count > MAX_RECURSION) {
606
ERR_PRINT("Max recursion reached");
607
return n;
608
}
609
610
if (p_deep) {
611
bool is_call_chain_end = recursion_count == 0;
612
613
recursion_count++;
614
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
615
n[E.key.recursive_duplicate(true, p_deep_subresources_mode, recursion_count)] = E.value.recursive_duplicate(true, p_deep_subresources_mode, recursion_count);
616
}
617
618
// Variant::recursive_duplicate() may have created a remap cache by now.
619
if (is_call_chain_end) {
620
Resource::_teardown_duplicate_from_variant();
621
}
622
} else {
623
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
624
n[E.key] = E.value;
625
}
626
}
627
628
return n;
629
}
630
631
void Dictionary::set_typed(const ContainerType &p_key_type, const ContainerType &p_value_type) {
632
set_typed(p_key_type.builtin_type, p_key_type.class_name, p_key_type.script, p_value_type.builtin_type, p_value_type.class_name, p_key_type.script);
633
}
634
635
void Dictionary::set_typed(uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {
636
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
637
ERR_FAIL_COND_MSG(_p->variant_map.size() > 0, "Type can only be set when dictionary is empty.");
638
ERR_FAIL_COND_MSG(_p->refcount.get() > 1, "Type can only be set when dictionary has no more than one user.");
639
ERR_FAIL_COND_MSG(_p->typed_key.type != Variant::NIL || _p->typed_value.type != Variant::NIL, "Type can only be set once.");
640
ERR_FAIL_COND_MSG((p_key_class_name != StringName() && p_key_type != Variant::OBJECT) || (p_value_class_name != StringName() && p_value_type != Variant::OBJECT), "Class names can only be set for type OBJECT.");
641
Ref<Script> key_script = p_key_script;
642
ERR_FAIL_COND_MSG(key_script.is_valid() && p_key_class_name == StringName(), "Script class can only be set together with base class name.");
643
Ref<Script> value_script = p_value_script;
644
ERR_FAIL_COND_MSG(value_script.is_valid() && p_value_class_name == StringName(), "Script class can only be set together with base class name.");
645
646
_p->typed_key.type = Variant::Type(p_key_type);
647
_p->typed_key.class_name = p_key_class_name;
648
_p->typed_key.script = key_script;
649
_p->typed_key.where = "TypedDictionary.Key";
650
651
_p->typed_value.type = Variant::Type(p_value_type);
652
_p->typed_value.class_name = p_value_class_name;
653
_p->typed_value.script = value_script;
654
_p->typed_value.where = "TypedDictionary.Value";
655
}
656
657
bool Dictionary::is_typed() const {
658
return is_typed_key() || is_typed_value();
659
}
660
661
bool Dictionary::is_typed_key() const {
662
return _p->typed_key.type != Variant::NIL;
663
}
664
665
bool Dictionary::is_typed_value() const {
666
return _p->typed_value.type != Variant::NIL;
667
}
668
669
bool Dictionary::is_same_instance(const Dictionary &p_other) const {
670
return _p == p_other._p;
671
}
672
673
bool Dictionary::is_same_typed(const Dictionary &p_other) const {
674
return is_same_typed_key(p_other) && is_same_typed_value(p_other);
675
}
676
677
bool Dictionary::is_same_typed_key(const Dictionary &p_other) const {
678
return _p->typed_key == p_other._p->typed_key;
679
}
680
681
bool Dictionary::is_same_typed_value(const Dictionary &p_other) const {
682
return _p->typed_value == p_other._p->typed_value;
683
}
684
685
ContainerType Dictionary::get_key_type() const {
686
ContainerType type;
687
type.builtin_type = _p->typed_key.type;
688
type.class_name = _p->typed_key.class_name;
689
type.script = _p->typed_key.script;
690
return type;
691
}
692
693
ContainerType Dictionary::get_value_type() const {
694
ContainerType type;
695
type.builtin_type = _p->typed_value.type;
696
type.class_name = _p->typed_value.class_name;
697
type.script = _p->typed_value.script;
698
return type;
699
}
700
701
uint32_t Dictionary::get_typed_key_builtin() const {
702
return _p->typed_key.type;
703
}
704
705
uint32_t Dictionary::get_typed_value_builtin() const {
706
return _p->typed_value.type;
707
}
708
709
StringName Dictionary::get_typed_key_class_name() const {
710
return _p->typed_key.class_name;
711
}
712
713
StringName Dictionary::get_typed_value_class_name() const {
714
return _p->typed_value.class_name;
715
}
716
717
Variant Dictionary::get_typed_key_script() const {
718
return _p->typed_key.script;
719
}
720
721
Variant Dictionary::get_typed_value_script() const {
722
return _p->typed_value.script;
723
}
724
725
const ContainerTypeValidate &Dictionary::get_key_validator() const {
726
return _p->typed_key;
727
}
728
729
const ContainerTypeValidate &Dictionary::get_value_validator() const {
730
return _p->typed_value;
731
}
732
733
void Dictionary::operator=(const Dictionary &p_dictionary) {
734
if (this == &p_dictionary) {
735
return;
736
}
737
_ref(p_dictionary);
738
}
739
740
const void *Dictionary::id() const {
741
return _p;
742
}
743
744
Dictionary::Dictionary(const Dictionary &p_base, uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {
745
_p = memnew(DictionaryPrivate);
746
_p->refcount.init();
747
set_typed(p_key_type, p_key_class_name, p_key_script, p_value_type, p_value_class_name, p_value_script);
748
assign(p_base);
749
}
750
751
Dictionary::Dictionary(const Dictionary &p_from) {
752
_p = nullptr;
753
_ref(p_from);
754
}
755
756
Dictionary::Dictionary() {
757
_p = memnew(DictionaryPrivate);
758
_p->refcount.init();
759
}
760
761
Dictionary::Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init) {
762
_p = memnew(DictionaryPrivate);
763
_p->refcount.init();
764
765
for (const KeyValue<Variant, Variant> &E : p_init) {
766
operator[](E.key) = E.value;
767
}
768
}
769
770
Dictionary::~Dictionary() {
771
_unref();
772
}
773
774