dictionary.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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. void Dictionary::erase(const Variant& p_key) {
  85. _copy_on_write();
  86. _p->variant_map.erase(p_key);
  87. }
  88. bool Dictionary::operator==(const Dictionary& p_dictionary) const {
  89. return _p==p_dictionary._p;
  90. }
  91. void Dictionary::_ref(const Dictionary& p_from) const {
  92. //make a copy first (thread safe)
  93. if (!p_from._p->refcount.ref())
  94. return; // couldn't copy
  95. //if this is the same, unreference the other one
  96. if (p_from._p==_p) {
  97. _p->refcount.unref();
  98. return;
  99. }
  100. if (_p)
  101. _unref();
  102. _p=p_from._p;
  103. }
  104. void Dictionary::clear() {
  105. _copy_on_write();
  106. _p->variant_map.clear();
  107. }
  108. bool Dictionary::is_shared() const {
  109. return _p->shared;
  110. }
  111. void Dictionary::_unref() const {
  112. ERR_FAIL_COND(!_p);
  113. if (_p->refcount.unref()) {
  114. memdelete(_p);
  115. }
  116. _p=NULL;
  117. }
  118. uint32_t Dictionary::hash() const {
  119. return hash_djb2_one_64(make_uint64_t(_p));
  120. }
  121. Array Dictionary::keys() const {
  122. Array karr;
  123. karr.resize(size());
  124. const Variant *K=NULL;
  125. int idx=0;
  126. while((K=next(K))) {
  127. karr[idx++]=(*K);
  128. }
  129. return karr;
  130. }
  131. const Variant* Dictionary::next(const Variant* p_key) const {
  132. return _p->variant_map.next(p_key);
  133. }
  134. Error Dictionary::parse_json(const String& p_json) {
  135. String errstr;
  136. int errline=0;
  137. if (p_json != ""){
  138. Error err = JSON::parse(p_json,*this,errstr,errline);
  139. if (err!=OK) {
  140. ERR_EXPLAIN("Error parsing JSON: "+errstr+" at line: "+itos(errline));
  141. ERR_FAIL_COND_V(err!=OK,err);
  142. }
  143. }
  144. return OK;
  145. }
  146. String Dictionary::to_json() const {
  147. return JSON::print(*this);
  148. }
  149. void Dictionary::operator=(const Dictionary& p_dictionary) {
  150. _ref(p_dictionary);
  151. }
  152. Dictionary::Dictionary(const Dictionary& p_from) {
  153. _p=NULL;
  154. _ref(p_from);
  155. }
  156. Dictionary::Dictionary(bool p_shared) {
  157. _p=memnew( DictionaryPrivate );
  158. _p->refcount.init();
  159. _p->shared=p_shared;
  160. }
  161. Dictionary::~Dictionary() {
  162. _unref();
  163. }