dictionary.cpp 27 KB

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