Dictionary.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*************************************************************************/
  2. /* Dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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.hpp"
  31. #include "Array.hpp"
  32. #include "GodotGlobal.hpp"
  33. #include "Variant.hpp"
  34. namespace godot {
  35. Dictionary::Dictionary() {
  36. godot::api->godot_dictionary_new(&_godot_dictionary);
  37. }
  38. Dictionary::Dictionary(const Dictionary &other) {
  39. godot::api->godot_dictionary_new_copy(&_godot_dictionary, &other._godot_dictionary);
  40. }
  41. Dictionary &Dictionary::operator=(const Dictionary &other) {
  42. godot::api->godot_dictionary_destroy(&_godot_dictionary);
  43. godot::api->godot_dictionary_new_copy(&_godot_dictionary, &other._godot_dictionary);
  44. return *this;
  45. }
  46. void Dictionary::clear() {
  47. godot::api->godot_dictionary_clear(&_godot_dictionary);
  48. }
  49. bool Dictionary::empty() const {
  50. return godot::api->godot_dictionary_empty(&_godot_dictionary);
  51. }
  52. void Dictionary::erase(const Variant &key) {
  53. godot::api->godot_dictionary_erase(&_godot_dictionary, (godot_variant *)&key);
  54. }
  55. bool Dictionary::has(const Variant &key) const {
  56. return godot::api->godot_dictionary_has(&_godot_dictionary, (godot_variant *)&key);
  57. }
  58. bool Dictionary::has_all(const Array &keys) const {
  59. return godot::api->godot_dictionary_has_all(&_godot_dictionary, (godot_array *)&keys);
  60. }
  61. uint32_t Dictionary::hash() const {
  62. return godot::api->godot_dictionary_hash(&_godot_dictionary);
  63. }
  64. Array Dictionary::keys() const {
  65. godot_array a = godot::api->godot_dictionary_keys(&_godot_dictionary);
  66. return Array(a);
  67. }
  68. Variant &Dictionary::operator[](const Variant &key) {
  69. godot_variant *v = godot::api->godot_dictionary_operator_index(&_godot_dictionary, (godot_variant *)&key);
  70. return *reinterpret_cast<Variant *>(v);
  71. }
  72. const Variant &Dictionary::operator[](const Variant &key) const {
  73. // oops I did it again
  74. godot_variant *v = godot::api->godot_dictionary_operator_index((godot_dictionary *)&_godot_dictionary, (godot_variant *)&key);
  75. return *reinterpret_cast<Variant *>(v);
  76. }
  77. int Dictionary::size() const {
  78. return godot::api->godot_dictionary_size(&_godot_dictionary);
  79. }
  80. String Dictionary::to_json() const {
  81. godot_string s = godot::api->godot_dictionary_to_json(&_godot_dictionary);
  82. return String(s);
  83. }
  84. Array Dictionary::values() const {
  85. godot_array a = godot::api->godot_dictionary_values(&_godot_dictionary);
  86. return Array(a);
  87. }
  88. Dictionary::~Dictionary() {
  89. godot::api->godot_dictionary_destroy(&_godot_dictionary);
  90. }
  91. } // namespace godot