dictionary.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /**************************************************************************/
  2. /* dictionary.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "dictionary.h"
  31. #include "core/templates/hash_map.h"
  32. #include "core/templates/safe_refcount.h"
  33. #include "core/variant/container_type_validate.h"
  34. #include "core/variant/variant.h"
  35. // required in this order by VariantInternal, do not remove this comment.
  36. #include "core/object/class_db.h"
  37. #include "core/object/object.h"
  38. #include "core/variant/type_info.h"
  39. #include "core/variant/variant_internal.h"
  40. struct DictionaryPrivate {
  41. SafeRefCount refcount;
  42. Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
  43. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map;
  44. ContainerTypeValidate typed_key;
  45. ContainerTypeValidate typed_value;
  46. Variant *typed_fallback = nullptr; // Allows a typed dictionary to return dummy values when attempting an invalid access.
  47. };
  48. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  49. if (_p->variant_map.is_empty()) {
  50. return;
  51. }
  52. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  53. p_keys->push_back(E.key);
  54. }
  55. }
  56. Variant Dictionary::get_key_at_index(int p_index) const {
  57. int index = 0;
  58. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  59. if (index == p_index) {
  60. return E.key;
  61. }
  62. index++;
  63. }
  64. return Variant();
  65. }
  66. Variant Dictionary::get_value_at_index(int p_index) const {
  67. int index = 0;
  68. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  69. if (index == p_index) {
  70. return E.value;
  71. }
  72. index++;
  73. }
  74. return Variant();
  75. }
  76. Variant &Dictionary::operator[](const Variant &p_key) {
  77. if (unlikely(_p->read_only)) {
  78. if (likely(_p->variant_map.has(p_key))) {
  79. *_p->read_only = _p->variant_map[p_key];
  80. } else {
  81. *_p->read_only = Variant();
  82. }
  83. return *_p->read_only;
  84. } else {
  85. return _p->variant_map[p_key];
  86. }
  87. }
  88. const Variant &Dictionary::operator[](const Variant &p_key) const {
  89. // Will not insert key, so no conversion is necessary.
  90. return _p->variant_map[p_key];
  91. }
  92. const Variant *Dictionary::getptr(const Variant &p_key) const {
  93. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(p_key));
  94. if (!E) {
  95. return nullptr;
  96. }
  97. return &E->value;
  98. }
  99. Variant *Dictionary::getptr(const Variant &p_key) {
  100. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E(_p->variant_map.find(p_key));
  101. if (!E) {
  102. return nullptr;
  103. }
  104. if (unlikely(_p->read_only != nullptr)) {
  105. *_p->read_only = E->value;
  106. return _p->read_only;
  107. } else {
  108. return &E->value;
  109. }
  110. }
  111. Variant Dictionary::get_valid(const Variant &p_key) const {
  112. Variant key = p_key;
  113. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get_valid"), Variant());
  114. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
  115. if (!E) {
  116. return Variant();
  117. }
  118. return E->value;
  119. }
  120. Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
  121. Variant key = p_key;
  122. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
  123. const Variant *result = getptr(key);
  124. if (!result) {
  125. return p_default;
  126. }
  127. return *result;
  128. }
  129. Variant Dictionary::get_or_add(const Variant &p_key, const Variant &p_default) {
  130. Variant key = p_key;
  131. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
  132. const Variant *result = getptr(key);
  133. if (!result) {
  134. Variant value = p_default;
  135. ERR_FAIL_COND_V(!_p->typed_value.validate(value, "add"), value);
  136. operator[](key) = value;
  137. return value;
  138. }
  139. return *result;
  140. }
  141. int Dictionary::size() const {
  142. return _p->variant_map.size();
  143. }
  144. bool Dictionary::is_empty() const {
  145. return !_p->variant_map.size();
  146. }
  147. bool Dictionary::has(const Variant &p_key) const {
  148. Variant key = p_key;
  149. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has'"), false);
  150. return _p->variant_map.has(p_key);
  151. }
  152. bool Dictionary::has_all(const Array &p_keys) const {
  153. for (int i = 0; i < p_keys.size(); i++) {
  154. Variant key = p_keys[i];
  155. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has_all'"), false);
  156. if (!has(key)) {
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. Variant Dictionary::find_key(const Variant &p_value) const {
  163. Variant value = p_value;
  164. ERR_FAIL_COND_V(!_p->typed_value.validate(value, "find_key"), Variant());
  165. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  166. if (E.value == value) {
  167. return E.key;
  168. }
  169. }
  170. return Variant();
  171. }
  172. bool Dictionary::erase(const Variant &p_key) {
  173. Variant key = p_key;
  174. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "erase"), false);
  175. ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
  176. return _p->variant_map.erase(key);
  177. }
  178. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  179. return recursive_equal(p_dictionary, 0);
  180. }
  181. bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
  182. return !recursive_equal(p_dictionary, 0);
  183. }
  184. bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_count) const {
  185. // Cheap checks
  186. if (_p == p_dictionary._p) {
  187. return true;
  188. }
  189. if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
  190. return false;
  191. }
  192. // Heavy O(n) check
  193. if (recursion_count > MAX_RECURSION) {
  194. ERR_PRINT("Max recursion reached");
  195. return true;
  196. }
  197. recursion_count++;
  198. for (const KeyValue<Variant, Variant> &this_E : _p->variant_map) {
  199. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator other_E(p_dictionary._p->variant_map.find(this_E.key));
  200. if (!other_E || !this_E.value.hash_compare(other_E->value, recursion_count, false)) {
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. void Dictionary::_ref(const Dictionary &p_from) const {
  207. //make a copy first (thread safe)
  208. if (!p_from._p->refcount.ref()) {
  209. return; // couldn't copy
  210. }
  211. //if this is the same, unreference the other one
  212. if (p_from._p == _p) {
  213. _p->refcount.unref();
  214. return;
  215. }
  216. if (_p) {
  217. _unref();
  218. }
  219. _p = p_from._p;
  220. }
  221. void Dictionary::clear() {
  222. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  223. _p->variant_map.clear();
  224. }
  225. void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
  226. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  227. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  228. Variant key = E.key;
  229. Variant value = E.value;
  230. ERR_FAIL_COND(!_p->typed_key.validate(key, "merge"));
  231. ERR_FAIL_COND(!_p->typed_value.validate(value, "merge"));
  232. if (p_overwrite || !has(key)) {
  233. operator[](key) = value;
  234. }
  235. }
  236. }
  237. Dictionary Dictionary::merged(const Dictionary &p_dictionary, bool p_overwrite) const {
  238. Dictionary ret = duplicate();
  239. ret.merge(p_dictionary, p_overwrite);
  240. return ret;
  241. }
  242. void Dictionary::_unref() const {
  243. ERR_FAIL_NULL(_p);
  244. if (_p->refcount.unref()) {
  245. if (_p->read_only) {
  246. memdelete(_p->read_only);
  247. }
  248. if (_p->typed_fallback) {
  249. memdelete(_p->typed_fallback);
  250. }
  251. memdelete(_p);
  252. }
  253. _p = nullptr;
  254. }
  255. uint32_t Dictionary::hash() const {
  256. return recursive_hash(0);
  257. }
  258. uint32_t Dictionary::recursive_hash(int recursion_count) const {
  259. if (recursion_count > MAX_RECURSION) {
  260. ERR_PRINT("Max recursion reached");
  261. return 0;
  262. }
  263. uint32_t h = hash_murmur3_one_32(Variant::DICTIONARY);
  264. recursion_count++;
  265. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  266. h = hash_murmur3_one_32(E.key.recursive_hash(recursion_count), h);
  267. h = hash_murmur3_one_32(E.value.recursive_hash(recursion_count), h);
  268. }
  269. return hash_fmix32(h);
  270. }
  271. Array Dictionary::keys() const {
  272. Array varr;
  273. if (is_typed_key()) {
  274. varr.set_typed(get_typed_key_builtin(), get_typed_key_class_name(), get_typed_key_script());
  275. }
  276. if (_p->variant_map.is_empty()) {
  277. return varr;
  278. }
  279. varr.resize(size());
  280. int i = 0;
  281. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  282. varr[i] = E.key;
  283. i++;
  284. }
  285. return varr;
  286. }
  287. Array Dictionary::values() const {
  288. Array varr;
  289. if (is_typed_value()) {
  290. varr.set_typed(get_typed_value_builtin(), get_typed_value_class_name(), get_typed_value_script());
  291. }
  292. if (_p->variant_map.is_empty()) {
  293. return varr;
  294. }
  295. varr.resize(size());
  296. int i = 0;
  297. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  298. varr[i] = E.value;
  299. i++;
  300. }
  301. return varr;
  302. }
  303. void Dictionary::assign(const Dictionary &p_dictionary) {
  304. const ContainerTypeValidate &typed_key = _p->typed_key;
  305. const ContainerTypeValidate &typed_key_source = p_dictionary._p->typed_key;
  306. const ContainerTypeValidate &typed_value = _p->typed_value;
  307. const ContainerTypeValidate &typed_value_source = p_dictionary._p->typed_value;
  308. if ((typed_key == typed_key_source || typed_key.type == Variant::NIL || (typed_key_source.type == Variant::OBJECT && typed_key.can_reference(typed_key_source))) &&
  309. (typed_value == typed_value_source || typed_value.type == Variant::NIL || (typed_value_source.type == Variant::OBJECT && typed_value.can_reference(typed_value_source)))) {
  310. // From same to same or,
  311. // from anything to variants or,
  312. // from subclasses to base classes.
  313. _p->variant_map = p_dictionary._p->variant_map;
  314. return;
  315. }
  316. int size = p_dictionary._p->variant_map.size();
  317. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map = HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>(size);
  318. Vector<Variant> key_array;
  319. key_array.resize(size);
  320. Variant *key_data = key_array.ptrw();
  321. Vector<Variant> value_array;
  322. value_array.resize(size);
  323. Variant *value_data = value_array.ptrw();
  324. if (typed_key == typed_key_source || typed_key.type == Variant::NIL || (typed_key_source.type == Variant::OBJECT && typed_key.can_reference(typed_key_source))) {
  325. // From same to same or,
  326. // from anything to variants or,
  327. // from subclasses to base classes.
  328. int i = 0;
  329. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  330. const Variant *key = &E.key;
  331. key_data[i++] = *key;
  332. }
  333. } 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))) {
  334. // From variants to objects or,
  335. // from base classes to subclasses.
  336. int i = 0;
  337. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  338. const Variant *key = &E.key;
  339. if (key->get_type() != Variant::NIL && (key->get_type() != Variant::OBJECT || !typed_key.validate_object(*key, "assign"))) {
  340. 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)));
  341. }
  342. key_data[i++] = *key;
  343. }
  344. } else if (typed_key.type == Variant::OBJECT || typed_key_source.type == Variant::OBJECT) {
  345. 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),
  346. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  347. } else if (typed_key_source.type == Variant::NIL && typed_key.type != Variant::OBJECT) {
  348. // From variants to primitives.
  349. int i = 0;
  350. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  351. const Variant *key = &E.key;
  352. if (key->get_type() == typed_key.type) {
  353. key_data[i++] = *key;
  354. continue;
  355. }
  356. if (!Variant::can_convert_strict(key->get_type(), typed_key.type)) {
  357. 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)));
  358. }
  359. Callable::CallError ce;
  360. Variant::construct(typed_key.type, key_data[i++], &key, 1, ce);
  361. 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)));
  362. }
  363. } else if (Variant::can_convert_strict(typed_key_source.type, typed_key.type)) {
  364. // From primitives to different convertible primitives.
  365. int i = 0;
  366. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  367. const Variant *key = &E.key;
  368. Callable::CallError ce;
  369. Variant::construct(typed_key.type, key_data[i++], &key, 1, ce);
  370. 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)));
  371. }
  372. } else {
  373. 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),
  374. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  375. }
  376. if (typed_value == typed_value_source || typed_value.type == Variant::NIL || (typed_value_source.type == Variant::OBJECT && typed_value.can_reference(typed_value_source))) {
  377. // From same to same or,
  378. // from anything to variants or,
  379. // from subclasses to base classes.
  380. int i = 0;
  381. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  382. const Variant *value = &E.value;
  383. value_data[i++] = *value;
  384. }
  385. } 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)))) {
  386. // From variants to objects or,
  387. // from base classes to subclasses.
  388. int i = 0;
  389. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  390. const Variant *value = &E.value;
  391. if (value->get_type() != Variant::NIL && (value->get_type() != Variant::OBJECT || !typed_value.validate_object(*value, "assign"))) {
  392. 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)));
  393. }
  394. value_data[i++] = *value;
  395. }
  396. } else if (typed_value.type == Variant::OBJECT || typed_value_source.type == Variant::OBJECT) {
  397. 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),
  398. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  399. } else if (typed_value_source.type == Variant::NIL && typed_value.type != Variant::OBJECT) {
  400. // From variants to primitives.
  401. int i = 0;
  402. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  403. const Variant *value = &E.value;
  404. if (value->get_type() == typed_value.type) {
  405. value_data[i++] = *value;
  406. continue;
  407. }
  408. if (!Variant::can_convert_strict(value->get_type(), typed_value.type)) {
  409. 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)));
  410. }
  411. Callable::CallError ce;
  412. Variant::construct(typed_value.type, value_data[i++], &value, 1, ce);
  413. 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)));
  414. }
  415. } else if (Variant::can_convert_strict(typed_value_source.type, typed_value.type)) {
  416. // From primitives to different convertible primitives.
  417. int i = 0;
  418. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  419. const Variant *value = &E.value;
  420. Callable::CallError ce;
  421. Variant::construct(typed_value.type, value_data[i++], &value, 1, ce);
  422. 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)));
  423. }
  424. } else {
  425. 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),
  426. Variant::get_type_name(typed_key.type), Variant::get_type_name(typed_value.type)));
  427. }
  428. for (int i = 0; i < size; i++) {
  429. variant_map.insert(key_data[i], value_data[i]);
  430. }
  431. _p->variant_map = variant_map;
  432. }
  433. const Variant *Dictionary::next(const Variant *p_key) const {
  434. if (p_key == nullptr) {
  435. // caller wants to get the first element
  436. if (_p->variant_map.begin()) {
  437. return &_p->variant_map.begin()->key;
  438. }
  439. return nullptr;
  440. }
  441. Variant key = *p_key;
  442. ERR_FAIL_COND_V(!_p->typed_key.validate(key, "next"), nullptr);
  443. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(key);
  444. if (!E) {
  445. return nullptr;
  446. }
  447. ++E;
  448. if (E) {
  449. return &E->key;
  450. }
  451. return nullptr;
  452. }
  453. Dictionary Dictionary::duplicate(bool p_deep) const {
  454. return recursive_duplicate(p_deep, 0);
  455. }
  456. void Dictionary::make_read_only() {
  457. if (_p->read_only == nullptr) {
  458. _p->read_only = memnew(Variant);
  459. }
  460. }
  461. bool Dictionary::is_read_only() const {
  462. return _p->read_only != nullptr;
  463. }
  464. Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
  465. Dictionary n;
  466. n._p->typed_key = _p->typed_key;
  467. n._p->typed_value = _p->typed_value;
  468. if (recursion_count > MAX_RECURSION) {
  469. ERR_PRINT("Max recursion reached");
  470. return n;
  471. }
  472. if (p_deep) {
  473. recursion_count++;
  474. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  475. n[E.key.recursive_duplicate(true, recursion_count)] = E.value.recursive_duplicate(true, recursion_count);
  476. }
  477. } else {
  478. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  479. n[E.key] = E.value;
  480. }
  481. }
  482. return n;
  483. }
  484. 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) {
  485. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  486. ERR_FAIL_COND_MSG(_p->variant_map.size() > 0, "Type can only be set when dictionary is empty.");
  487. ERR_FAIL_COND_MSG(_p->refcount.get() > 1, "Type can only be set when dictionary has no more than one user.");
  488. ERR_FAIL_COND_MSG(_p->typed_key.type != Variant::NIL || _p->typed_value.type != Variant::NIL, "Type can only be set once.");
  489. 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.");
  490. Ref<Script> key_script = p_key_script;
  491. ERR_FAIL_COND_MSG(key_script.is_valid() && p_key_class_name == StringName(), "Script class can only be set together with base class name.");
  492. Ref<Script> value_script = p_value_script;
  493. ERR_FAIL_COND_MSG(value_script.is_valid() && p_value_class_name == StringName(), "Script class can only be set together with base class name.");
  494. _p->typed_key.type = Variant::Type(p_key_type);
  495. _p->typed_key.class_name = p_key_class_name;
  496. _p->typed_key.script = key_script;
  497. _p->typed_key.where = "TypedDictionary.Key";
  498. _p->typed_value.type = Variant::Type(p_value_type);
  499. _p->typed_value.class_name = p_value_class_name;
  500. _p->typed_value.script = value_script;
  501. _p->typed_value.where = "TypedDictionary.Value";
  502. }
  503. bool Dictionary::is_typed() const {
  504. return is_typed_key() || is_typed_value();
  505. }
  506. bool Dictionary::is_typed_key() const {
  507. return _p->typed_key.type != Variant::NIL;
  508. }
  509. bool Dictionary::is_typed_value() const {
  510. return _p->typed_value.type != Variant::NIL;
  511. }
  512. bool Dictionary::is_same_typed(const Dictionary &p_other) const {
  513. return is_same_typed_key(p_other) && is_same_typed_value(p_other);
  514. }
  515. bool Dictionary::is_same_typed_key(const Dictionary &p_other) const {
  516. return _p->typed_key == p_other._p->typed_key;
  517. }
  518. bool Dictionary::is_same_typed_value(const Dictionary &p_other) const {
  519. return _p->typed_value == p_other._p->typed_value;
  520. }
  521. uint32_t Dictionary::get_typed_key_builtin() const {
  522. return _p->typed_key.type;
  523. }
  524. uint32_t Dictionary::get_typed_value_builtin() const {
  525. return _p->typed_value.type;
  526. }
  527. StringName Dictionary::get_typed_key_class_name() const {
  528. return _p->typed_key.class_name;
  529. }
  530. StringName Dictionary::get_typed_value_class_name() const {
  531. return _p->typed_value.class_name;
  532. }
  533. Variant Dictionary::get_typed_key_script() const {
  534. return _p->typed_key.script;
  535. }
  536. Variant Dictionary::get_typed_value_script() const {
  537. return _p->typed_value.script;
  538. }
  539. void Dictionary::operator=(const Dictionary &p_dictionary) {
  540. if (this == &p_dictionary) {
  541. return;
  542. }
  543. _ref(p_dictionary);
  544. }
  545. const void *Dictionary::id() const {
  546. return _p;
  547. }
  548. 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) {
  549. _p = memnew(DictionaryPrivate);
  550. _p->refcount.init();
  551. set_typed(p_key_type, p_key_class_name, p_key_script, p_value_type, p_value_class_name, p_value_script);
  552. assign(p_base);
  553. }
  554. Dictionary::Dictionary(const Dictionary &p_from) {
  555. _p = nullptr;
  556. _ref(p_from);
  557. }
  558. Dictionary::Dictionary() {
  559. _p = memnew(DictionaryPrivate);
  560. _p->refcount.init();
  561. }
  562. Dictionary::~Dictionary() {
  563. _unref();
  564. }