dictionary.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "io/json.h"
  32. #include "safe_refcount.h"
  33. #include "variant.h"
  34. struct _DictionaryVariantHash {
  35. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  36. };
  37. struct DictionaryPrivate {
  38. SafeRefCount refcount;
  39. HashMap<Variant, Variant, _DictionaryVariantHash> variant_map;
  40. bool shared;
  41. };
  42. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  43. _p->variant_map.get_key_list(p_keys);
  44. }
  45. void Dictionary::_copy_on_write() const {
  46. //make a copy of what we have
  47. if (_p->shared)
  48. return;
  49. DictionaryPrivate *p = memnew(DictionaryPrivate);
  50. p->shared = _p->shared;
  51. p->variant_map = _p->variant_map;
  52. p->refcount.init();
  53. _unref();
  54. _p = p;
  55. }
  56. Variant &Dictionary::operator[](const Variant &p_key) {
  57. _copy_on_write();
  58. return _p->variant_map[p_key];
  59. }
  60. const Variant &Dictionary::operator[](const Variant &p_key) const {
  61. return _p->variant_map[p_key];
  62. }
  63. const Variant *Dictionary::getptr(const Variant &p_key) const {
  64. return _p->variant_map.getptr(p_key);
  65. }
  66. Variant *Dictionary::getptr(const Variant &p_key) {
  67. _copy_on_write();
  68. return _p->variant_map.getptr(p_key);
  69. }
  70. Variant Dictionary::get_valid(const Variant &p_key) const {
  71. const Variant *v = getptr(p_key);
  72. if (!v)
  73. return Variant();
  74. return *v;
  75. }
  76. int Dictionary::size() const {
  77. return _p->variant_map.size();
  78. }
  79. bool Dictionary::empty() const {
  80. return !_p->variant_map.size();
  81. }
  82. bool Dictionary::has(const Variant &p_key) const {
  83. return _p->variant_map.has(p_key);
  84. }
  85. bool Dictionary::has_all(const Array &p_keys) const {
  86. for (int i = 0; i < p_keys.size(); i++) {
  87. if (!has(p_keys[i])) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. void Dictionary::erase(const Variant &p_key) {
  94. _copy_on_write();
  95. _p->variant_map.erase(p_key);
  96. }
  97. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  98. return _p == p_dictionary._p;
  99. }
  100. void Dictionary::_ref(const Dictionary &p_from) const {
  101. //make a copy first (thread safe)
  102. if (!p_from._p->refcount.ref())
  103. return; // couldn't copy
  104. //if this is the same, unreference the other one
  105. if (p_from._p == _p) {
  106. _p->refcount.unref();
  107. return;
  108. }
  109. if (_p)
  110. _unref();
  111. _p = p_from._p;
  112. }
  113. void Dictionary::clear() {
  114. _copy_on_write();
  115. _p->variant_map.clear();
  116. }
  117. bool Dictionary::is_shared() const {
  118. return _p->shared;
  119. }
  120. void Dictionary::_unref() const {
  121. ERR_FAIL_COND(!_p);
  122. if (_p->refcount.unref()) {
  123. memdelete(_p);
  124. }
  125. _p = NULL;
  126. }
  127. uint32_t Dictionary::hash() const {
  128. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  129. List<Variant> keys;
  130. get_key_list(&keys);
  131. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  132. h = hash_djb2_one_32(E->get().hash(), h);
  133. h = hash_djb2_one_32(operator[](E->get()).hash(), h);
  134. }
  135. return h;
  136. }
  137. Array Dictionary::keys() const {
  138. Array karr;
  139. karr.resize(size());
  140. const Variant *K = NULL;
  141. int idx = 0;
  142. while ((K = next(K))) {
  143. karr[idx++] = (*K);
  144. }
  145. return karr;
  146. }
  147. Array Dictionary::values() const {
  148. Array varr;
  149. varr.resize(size());
  150. const Variant *key = NULL;
  151. int i = 0;
  152. while ((key = next(key))) {
  153. varr[i++] = _p->variant_map[*key];
  154. }
  155. return varr;
  156. }
  157. const Variant *Dictionary::next(const Variant *p_key) const {
  158. return _p->variant_map.next(p_key);
  159. }
  160. Error Dictionary::parse_json(const String &p_json) {
  161. String errstr;
  162. int errline = 0;
  163. if (p_json != "") {
  164. Error err = JSON::parse(p_json, *this, errstr, errline);
  165. if (err != OK) {
  166. ERR_EXPLAIN("Error parsing JSON: " + errstr + " at line: " + itos(errline));
  167. ERR_FAIL_COND_V(err != OK, err);
  168. }
  169. }
  170. return OK;
  171. }
  172. String Dictionary::to_json() const {
  173. return JSON::print(*this);
  174. }
  175. void Dictionary::operator=(const Dictionary &p_dictionary) {
  176. _ref(p_dictionary);
  177. }
  178. Dictionary::Dictionary(const Dictionary &p_from) {
  179. _p = NULL;
  180. _ref(p_from);
  181. }
  182. Dictionary::Dictionary(bool p_shared) {
  183. _p = memnew(DictionaryPrivate);
  184. _p->refcount.init();
  185. _p->shared = p_shared;
  186. }
  187. Dictionary::~Dictionary() {
  188. _unref();
  189. }