dictionary.cpp 26 KB

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