dictionary.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "dictionary.h"
  30. #include "safe_refcount.h"
  31. #include "variant.h"
  32. #include "io/json.h"
  33. struct _DictionaryVariantHash {
  34. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  35. };
  36. struct DictionaryPrivate {
  37. SafeRefCount refcount;
  38. HashMap<Variant,Variant,_DictionaryVariantHash> variant_map;
  39. bool shared;
  40. };
  41. void Dictionary::get_key_list( List<Variant> *p_keys) const {
  42. _p->variant_map.get_key_list(p_keys);
  43. }
  44. void Dictionary::_copy_on_write() const {
  45. //make a copy of what we have
  46. if (_p->shared)
  47. return;
  48. DictionaryPrivate *p = memnew(DictionaryPrivate);
  49. p->shared=_p->shared;
  50. p->variant_map=_p->variant_map;
  51. p->refcount.init();
  52. _unref();
  53. _p=p;
  54. }
  55. Variant& Dictionary::operator[](const Variant& p_key) {
  56. _copy_on_write();
  57. return _p->variant_map[p_key];
  58. }
  59. const Variant& Dictionary::operator[](const Variant& p_key) const {
  60. return _p->variant_map[p_key];
  61. }
  62. const Variant* Dictionary::getptr(const Variant& p_key) const {
  63. return _p->variant_map.getptr(p_key);
  64. }
  65. Variant* Dictionary::getptr(const Variant& p_key) {
  66. _copy_on_write();
  67. return _p->variant_map.getptr(p_key);
  68. }
  69. Variant Dictionary::get_valid(const Variant& p_key) const {
  70. const Variant *v = getptr(p_key);
  71. if (!v)
  72. return Variant();
  73. return *v;
  74. }
  75. int Dictionary::size() const {
  76. return _p->variant_map.size();
  77. }
  78. bool Dictionary::empty() const {
  79. return !_p->variant_map.size();
  80. }
  81. bool Dictionary::has(const Variant& p_key) const {
  82. return _p->variant_map.has(p_key);
  83. }
  84. bool Dictionary::has_all(const Array& p_keys) const {
  85. for (int i=0;i<p_keys.size();i++) {
  86. if( !has(p_keys[i]) ) {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. void Dictionary::erase(const Variant& p_key) {
  93. _copy_on_write();
  94. _p->variant_map.erase(p_key);
  95. }
  96. bool Dictionary::operator==(const Dictionary& p_dictionary) const {
  97. return _p==p_dictionary._p;
  98. }
  99. void Dictionary::_ref(const Dictionary& p_from) const {
  100. //make a copy first (thread safe)
  101. if (!p_from._p->refcount.ref())
  102. return; // couldn't copy
  103. //if this is the same, unreference the other one
  104. if (p_from._p==_p) {
  105. _p->refcount.unref();
  106. return;
  107. }
  108. if (_p)
  109. _unref();
  110. _p=p_from._p;
  111. }
  112. void Dictionary::clear() {
  113. _copy_on_write();
  114. _p->variant_map.clear();
  115. }
  116. bool Dictionary::is_shared() const {
  117. return _p->shared;
  118. }
  119. void Dictionary::_unref() const {
  120. ERR_FAIL_COND(!_p);
  121. if (_p->refcount.unref()) {
  122. memdelete(_p);
  123. }
  124. _p=NULL;
  125. }
  126. uint32_t Dictionary::hash() const {
  127. uint32_t h=hash_djb2_one_32(Variant::DICTIONARY);
  128. List<Variant> keys;
  129. get_key_list(&keys);
  130. for (List<Variant>::Element *E=keys.front();E;E=E->next()) {
  131. h = hash_djb2_one_32( E->get().hash(), h);
  132. h = hash_djb2_one_32( operator[](E->get()).hash(), h);
  133. }
  134. return h;
  135. }
  136. Array Dictionary::keys() const {
  137. Array karr;
  138. karr.resize(size());
  139. const Variant *K=NULL;
  140. int idx=0;
  141. while((K=next(K))) {
  142. karr[idx++]=(*K);
  143. }
  144. return karr;
  145. }
  146. const Variant* Dictionary::next(const Variant* p_key) const {
  147. return _p->variant_map.next(p_key);
  148. }
  149. Error Dictionary::parse_json(const String& p_json) {
  150. String errstr;
  151. int errline=0;
  152. if (p_json != ""){
  153. Error err = JSON::parse(p_json,*this,errstr,errline);
  154. if (err!=OK) {
  155. ERR_EXPLAIN("Error parsing JSON: "+errstr+" at line: "+itos(errline));
  156. ERR_FAIL_COND_V(err!=OK,err);
  157. }
  158. }
  159. return OK;
  160. }
  161. String Dictionary::to_json() const {
  162. return JSON::print(*this);
  163. }
  164. void Dictionary::operator=(const Dictionary& p_dictionary) {
  165. _ref(p_dictionary);
  166. }
  167. Dictionary::Dictionary(const Dictionary& p_from) {
  168. _p=NULL;
  169. _ref(p_from);
  170. }
  171. Dictionary::Dictionary(bool p_shared) {
  172. _p=memnew( DictionaryPrivate );
  173. _p->refcount.init();
  174. _p->shared=p_shared;
  175. }
  176. Dictionary::~Dictionary() {
  177. _unref();
  178. }