dictionary.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* dictionary.h */
  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. #pragma once
  31. #include "core/string/ustring.h"
  32. #include "core/templates/hash_map.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/pair.h"
  35. #include "core/variant/array.h"
  36. #include "core/variant/variant_deep_duplicate.h"
  37. class Variant;
  38. struct ContainerType;
  39. struct ContainerTypeValidate;
  40. struct DictionaryPrivate;
  41. struct StringLikeVariantComparator;
  42. struct VariantHasher;
  43. class Dictionary {
  44. mutable DictionaryPrivate *_p;
  45. void _ref(const Dictionary &p_from) const;
  46. void _unref() const;
  47. public:
  48. using ConstIterator = HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator;
  49. ConstIterator begin() const;
  50. ConstIterator end() const;
  51. LocalVector<Variant> get_key_list() const;
  52. Variant get_key_at_index(int p_index) const;
  53. Variant get_value_at_index(int p_index) const;
  54. Variant &operator[](const Variant &p_key);
  55. const Variant &operator[](const Variant &p_key) const;
  56. const Variant *getptr(const Variant &p_key) const;
  57. Variant *getptr(const Variant &p_key);
  58. Variant get_valid(const Variant &p_key) const;
  59. Variant get(const Variant &p_key, const Variant &p_default) const;
  60. Variant get_or_add(const Variant &p_key, const Variant &p_default);
  61. bool set(const Variant &p_key, const Variant &p_value);
  62. int size() const;
  63. bool is_empty() const;
  64. void clear();
  65. void sort();
  66. void merge(const Dictionary &p_dictionary, bool p_overwrite = false);
  67. Dictionary merged(const Dictionary &p_dictionary, bool p_overwrite = false) const;
  68. bool has(const Variant &p_key) const;
  69. bool has_all(const Array &p_keys) const;
  70. Variant find_key(const Variant &p_value) const;
  71. bool erase(const Variant &p_key);
  72. bool operator==(const Dictionary &p_dictionary) const;
  73. bool operator!=(const Dictionary &p_dictionary) const;
  74. bool recursive_equal(const Dictionary &p_dictionary, int recursion_count) const;
  75. uint32_t hash() const;
  76. uint32_t recursive_hash(int recursion_count) const;
  77. void operator=(const Dictionary &p_dictionary);
  78. void assign(const Dictionary &p_dictionary);
  79. const Variant *next(const Variant *p_key = nullptr) const;
  80. Array keys() const;
  81. Array values() const;
  82. Dictionary duplicate(bool p_deep = false) const;
  83. Dictionary duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
  84. Dictionary recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const;
  85. void set_typed(const ContainerType &p_key_type, const ContainerType &p_value_type);
  86. void 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);
  87. bool is_typed() const;
  88. bool is_typed_key() const;
  89. bool is_typed_value() const;
  90. bool is_same_instance(const Dictionary &p_other) const;
  91. bool is_same_typed(const Dictionary &p_other) const;
  92. bool is_same_typed_key(const Dictionary &p_other) const;
  93. bool is_same_typed_value(const Dictionary &p_other) const;
  94. ContainerType get_key_type() const;
  95. ContainerType get_value_type() const;
  96. uint32_t get_typed_key_builtin() const;
  97. uint32_t get_typed_value_builtin() const;
  98. StringName get_typed_key_class_name() const;
  99. StringName get_typed_value_class_name() const;
  100. Variant get_typed_key_script() const;
  101. Variant get_typed_value_script() const;
  102. const ContainerTypeValidate &get_key_validator() const;
  103. const ContainerTypeValidate &get_value_validator() const;
  104. void make_read_only();
  105. bool is_read_only() const;
  106. const void *id() const;
  107. 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);
  108. Dictionary(const Dictionary &p_from);
  109. Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init);
  110. Dictionary();
  111. ~Dictionary();
  112. };