dictionary.cpp 25 KB

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