dictionary.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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::merge(const Dictionary &p_dictionary, bool p_overwrite) {
  265. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  266. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  267. Variant key = E.key;
  268. Variant value = E.value;
  269. ERR_FAIL_COND(!_p->typed_key.validate(key, "merge"));
  270. ERR_FAIL_COND(!_p->typed_value.validate(value, "merge"));
  271. if (p_overwrite || !has(key)) {
  272. operator[](key) = value;
  273. }
  274. }
  275. }
  276. Dictionary Dictionary::merged(const Dictionary &p_dictionary, bool p_overwrite) const {
  277. Dictionary ret = duplicate();
  278. ret.merge(p_dictionary, p_overwrite);
  279. return ret;
  280. }
  281. void Dictionary::_unref() const {
  282. ERR_FAIL_NULL(_p);
  283. if (_p->refcount.unref()) {
  284. if (_p->read_only) {
  285. memdelete(_p->read_only);
  286. }
  287. if (_p->typed_fallback) {
  288. memdelete(_p->typed_fallback);
  289. }
  290. memdelete(_p);
  291. }
  292. _p = nullptr;
  293. }
  294. uint32_t Dictionary::hash() const {
  295. return recursive_hash(0);
  296. }
  297. uint32_t Dictionary::recursive_hash(int recursion_count) const {
  298. if (recursion_count > MAX_RECURSION) {
  299. ERR_PRINT("Max recursion reached");
  300. return 0;
  301. }
  302. uint32_t h = hash_murmur3_one_32(Variant::DICTIONARY);
  303. recursion_count++;
  304. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  305. h = hash_murmur3_one_32(E.key.recursive_hash(recursion_count), h);
  306. h = hash_murmur3_one_32(E.value.recursive_hash(recursion_count), h);
  307. }
  308. return hash_fmix32(h);
  309. }
  310. Array Dictionary::keys() const {
  311. Array varr;
  312. if (is_typed_key()) {
  313. varr.set_typed(get_typed_key_builtin(), get_typed_key_class_name(), get_typed_key_script());
  314. }
  315. if (_p->variant_map.is_empty()) {
  316. return varr;
  317. }
  318. varr.resize(size());
  319. int i = 0;
  320. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  321. varr[i] = E.key;
  322. i++;
  323. }
  324. return varr;
  325. }
  326. Array Dictionary::values() const {
  327. Array varr;
  328. if (is_typed_value()) {
  329. varr.set_typed(get_typed_value_builtin(), get_typed_value_class_name(), get_typed_value_script());
  330. }
  331. if (_p->variant_map.is_empty()) {
  332. return varr;
  333. }
  334. varr.resize(size());
  335. int i = 0;
  336. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  337. varr[i] = E.value;
  338. i++;
  339. }
  340. return varr;
  341. }
  342. void Dictionary::assign(const Dictionary &p_dictionary) {
  343. const ContainerTypeValidate &typed_key = _p->typed_key;
  344. const ContainerTypeValidate &typed_key_source = p_dictionary._p->typed_key;
  345. const ContainerTypeValidate &typed_value = _p->typed_value;
  346. const ContainerTypeValidate &typed_value_source = p_dictionary._p->typed_value;
  347. if ((typed_key == typed_key_source || typed_key.type == Variant::NIL || (typed_key_source.type == Variant::OBJECT && typed_key.can_reference(typed_key_source))) &&
  348. (typed_value == typed_value_source || typed_value.type == Variant::NIL || (typed_value_source.type == Variant::OBJECT && typed_value.can_reference(typed_value_source)))) {
  349. // From same to same or,
  350. // from anything to variants or,
  351. // from subclasses to base classes.
  352. _p->variant_map = p_dictionary._p->variant_map;
  353. return;
  354. }
  355. int size = p_dictionary._p->variant_map.size();
  356. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map = HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>(size);
  357. Vector<Variant> key_array;
  358. key_array.resize(size);
  359. Variant *key_data = key_array.ptrw();
  360. Vector<Variant> value_array;
  361. value_array.resize(size);
  362. Variant *value_data = value_array.ptrw();
  363. if (typed_key == typed_key_source || typed_key.type == Variant::NIL || (typed_key_source.type == Variant::OBJECT && typed_key.can_reference(typed_key_source))) {
  364. // From same to same or,
  365. // from anything to variants or,
  366. // from subclasses to base classes.
  367. int i = 0;
  368. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  369. const Variant *key = &E.key;
  370. key_data[i++] = *key;
  371. }
  372. } 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))) {
  373. // From variants to objects or,
  374. // from base classes to subclasses.
  375. int i = 0;
  376. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  377. const Variant *key = &E.key;
  378. if (key->get_type() != Variant::NIL && (key->get_type() != Variant::OBJECT || !typed_key.validate_object(*key, "assign"))) {
  379. 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)));
  380. }
  381. key_data[i++] = *key;
  382. }
  383. } else if (typed_key.type == Variant::OBJECT || typed_key_source.type == Variant::OBJECT) {
  384. 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),
  385. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  386. } else if (typed_key_source.type == Variant::NIL && typed_key.type != Variant::OBJECT) {
  387. // From variants to primitives.
  388. int i = 0;
  389. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  390. const Variant *key = &E.key;
  391. if (key->get_type() == typed_key.type) {
  392. key_data[i++] = *key;
  393. continue;
  394. }
  395. if (!Variant::can_convert_strict(key->get_type(), typed_key.type)) {
  396. 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)));
  397. }
  398. Callable::CallError ce;
  399. Variant::construct(typed_key.type, key_data[i++], &key, 1, ce);
  400. 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)));
  401. }
  402. } else if (Variant::can_convert_strict(typed_key_source.type, typed_key.type)) {
  403. // From primitives to different convertible primitives.
  404. int i = 0;
  405. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  406. const Variant *key = &E.key;
  407. Callable::CallError ce;
  408. Variant::construct(typed_key.type, key_data[i++], &key, 1, ce);
  409. 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)));
  410. }
  411. } else {
  412. 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),
  413. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  414. }
  415. if (typed_value == typed_value_source || typed_value.type == Variant::NIL || (typed_value_source.type == Variant::OBJECT && typed_value.can_reference(typed_value_source))) {
  416. // From same to same or,
  417. // from anything to variants or,
  418. // from subclasses to base classes.
  419. int i = 0;
  420. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  421. const Variant *value = &E.value;
  422. value_data[i++] = *value;
  423. }
  424. } 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)))) {
  425. // From variants to objects or,
  426. // from base classes to subclasses.
  427. int i = 0;
  428. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  429. const Variant *value = &E.value;
  430. if (value->get_type() != Variant::NIL && (value->get_type() != Variant::OBJECT || !typed_value.validate_object(*value, "assign"))) {
  431. 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)));
  432. }
  433. value_data[i++] = *value;
  434. }
  435. } else if (typed_value.type == Variant::OBJECT || typed_value_source.type == Variant::OBJECT) {
  436. 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),
  437. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  438. } else if (typed_value_source.type == Variant::NIL && typed_value.type != Variant::OBJECT) {
  439. // From variants to primitives.
  440. int i = 0;
  441. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  442. const Variant *value = &E.value;
  443. if (value->get_type() == typed_value.type) {
  444. value_data[i++] = *value;
  445. continue;
  446. }
  447. if (!Variant::can_convert_strict(value->get_type(), typed_value.type)) {
  448. 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)));
  449. }
  450. Callable::CallError ce;
  451. Variant::construct(typed_value.type, value_data[i++], &value, 1, ce);
  452. 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)));
  453. }
  454. } else if (Variant::can_convert_strict(typed_value_source.type, typed_value.type)) {
  455. // From primitives to different convertible primitives.
  456. int i = 0;
  457. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  458. const Variant *value = &E.value;
  459. Callable::CallError ce;
  460. Variant::construct(typed_value.type, value_data[i++], &value, 1, ce);
  461. 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)));
  462. }
  463. } else {
  464. 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),
  465. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  466. }
  467. for (int i = 0; i < size; i++) {
  468. variant_map.insert(key_data[i], value_data[i]);
  469. }
  470. _p->variant_map = variant_map;
  471. }
  472. const Variant *Dictionary::next(const Variant *p_key) const {
  473. if (p_key == nullptr) {
  474. // caller wants to get the first element
  475. if (_p->variant_map.begin()) {
  476. return &_p->variant_map.begin()->key;
  477. }
  478. return nullptr;
  479. }
  480. Variant key = *p_key;
  481. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "next"), nullptr);
  482. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(key);
  483. if (!E) {
  484. return nullptr;
  485. }
  486. ++E;
  487. if (E) {
  488. return &E->key;
  489. }
  490. return nullptr;
  491. }
  492. Dictionary Dictionary::duplicate(bool p_deep) const {
  493. return recursive_duplicate(p_deep, 0);
  494. }
  495. void Dictionary::make_read_only() {
  496. if (_p->read_only == nullptr) {
  497. _p->read_only = memnew(Variant);
  498. }
  499. }
  500. bool Dictionary::is_read_only() const {
  501. return _p->read_only != nullptr;
  502. }
  503. Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
  504. Dictionary n;
  505. n._p->typed_key = _p->typed_key;
  506. n._p->typed_value = _p->typed_value;
  507. if (recursion_count > MAX_RECURSION) {
  508. ERR_PRINT("Max recursion reached");
  509. return n;
  510. }
  511. if (p_deep) {
  512. recursion_count++;
  513. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  514. n[E.key.recursive_duplicate(true, recursion_count)] = E.value.recursive_duplicate(true, recursion_count);
  515. }
  516. } else {
  517. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  518. n[E.key] = E.value;
  519. }
  520. }
  521. return n;
  522. }
  523. 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) {
  524. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  525. ERR_FAIL_COND_MSG(_p->variant_map.size() > 0, "Type can only be set when dictionary is empty.");
  526. ERR_FAIL_COND_MSG(_p->refcount.get() > 1, "Type can only be set when dictionary has no more than one user.");
  527. ERR_FAIL_COND_MSG(_p->typed_key.type != Variant::NIL || _p->typed_value.type != Variant::NIL, "Type can only be set once.");
  528. 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.");
  529. Ref<Script> key_script = p_key_script;
  530. ERR_FAIL_COND_MSG(key_script.is_valid() && p_key_class_name == StringName(), "Script class can only be set together with base class name.");
  531. Ref<Script> value_script = p_value_script;
  532. ERR_FAIL_COND_MSG(value_script.is_valid() && p_value_class_name == StringName(), "Script class can only be set together with base class name.");
  533. _p->typed_key.type = Variant::Type(p_key_type);
  534. _p->typed_key.class_name = p_key_class_name;
  535. _p->typed_key.script = key_script;
  536. _p->typed_key.where = "TypedDictionary.Key";
  537. _p->typed_value.type = Variant::Type(p_value_type);
  538. _p->typed_value.class_name = p_value_class_name;
  539. _p->typed_value.script = value_script;
  540. _p->typed_value.where = "TypedDictionary.Value";
  541. }
  542. bool Dictionary::is_typed() const {
  543. return is_typed_key() || is_typed_value();
  544. }
  545. bool Dictionary::is_typed_key() const {
  546. return _p->typed_key.type != Variant::NIL;
  547. }
  548. bool Dictionary::is_typed_value() const {
  549. return _p->typed_value.type != Variant::NIL;
  550. }
  551. bool Dictionary::is_same_typed(const Dictionary &p_other) const {
  552. return is_same_typed_key(p_other) && is_same_typed_value(p_other);
  553. }
  554. bool Dictionary::is_same_typed_key(const Dictionary &p_other) const {
  555. return _p->typed_key == p_other._p->typed_key;
  556. }
  557. bool Dictionary::is_same_typed_value(const Dictionary &p_other) const {
  558. return _p->typed_value == p_other._p->typed_value;
  559. }
  560. uint32_t Dictionary::get_typed_key_builtin() const {
  561. return _p->typed_key.type;
  562. }
  563. uint32_t Dictionary::get_typed_value_builtin() const {
  564. return _p->typed_value.type;
  565. }
  566. StringName Dictionary::get_typed_key_class_name() const {
  567. return _p->typed_key.class_name;
  568. }
  569. StringName Dictionary::get_typed_value_class_name() const {
  570. return _p->typed_value.class_name;
  571. }
  572. Variant Dictionary::get_typed_key_script() const {
  573. return _p->typed_key.script;
  574. }
  575. Variant Dictionary::get_typed_value_script() const {
  576. return _p->typed_value.script;
  577. }
  578. void Dictionary::operator=(const Dictionary &p_dictionary) {
  579. if (this == &p_dictionary) {
  580. return;
  581. }
  582. _ref(p_dictionary);
  583. }
  584. const void *Dictionary::id() const {
  585. return _p;
  586. }
  587. 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) {
  588. _p = memnew(DictionaryPrivate);
  589. _p->refcount.init();
  590. set_typed(p_key_type, p_key_class_name, p_key_script, p_value_type, p_value_class_name, p_value_script);
  591. assign(p_base);
  592. }
  593. Dictionary::Dictionary(const Dictionary &p_from) {
  594. _p = nullptr;
  595. _ref(p_from);
  596. }
  597. Dictionary::Dictionary() {
  598. _p = memnew(DictionaryPrivate);
  599. _p->refcount.init();
  600. }
  601. Dictionary::~Dictionary() {
  602. _unref();
  603. }