dictionary.cpp 26 KB

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