dictionary.cpp 27 KB

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