string_name.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**************************************************************************/
  2. /* string_name.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/safe_refcount.h"
  33. #define UNIQUE_NODE_PREFIX "%"
  34. class Main;
  35. class [[nodiscard]] StringName {
  36. struct Table;
  37. struct _Data {
  38. SafeRefCount refcount;
  39. SafeNumeric<uint32_t> static_count;
  40. String name;
  41. #ifdef DEBUG_ENABLED
  42. uint32_t debug_references = 0;
  43. #endif
  44. uint32_t hash = 0;
  45. _Data *prev = nullptr;
  46. _Data *next = nullptr;
  47. };
  48. _Data *_data = nullptr;
  49. void unref();
  50. friend void register_core_types();
  51. friend void unregister_core_types();
  52. friend class Main;
  53. static void setup();
  54. static void cleanup();
  55. static uint32_t get_empty_hash();
  56. static inline bool configured = false;
  57. #ifdef DEBUG_ENABLED
  58. struct DebugSortReferences {
  59. bool operator()(const _Data *p_left, const _Data *p_right) const {
  60. return p_left->debug_references > p_right->debug_references;
  61. }
  62. };
  63. static inline bool debug_stringname = false;
  64. #endif
  65. StringName(_Data *p_data) { _data = p_data; }
  66. public:
  67. _FORCE_INLINE_ explicit operator bool() const { return _data; }
  68. bool operator==(const String &p_name) const;
  69. bool operator==(const char *p_name) const;
  70. bool operator!=(const String &p_name) const;
  71. bool operator!=(const char *p_name) const;
  72. const char32_t *get_data() const { return _data ? _data->name.ptr() : U""; }
  73. char32_t operator[](int p_index) const;
  74. int length() const;
  75. _FORCE_INLINE_ bool is_empty() const { return !_data; }
  76. _FORCE_INLINE_ bool is_node_unique_name() const {
  77. if (!_data) {
  78. return false;
  79. }
  80. return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
  81. }
  82. _FORCE_INLINE_ bool operator<(const StringName &p_name) const {
  83. return _data < p_name._data;
  84. }
  85. _FORCE_INLINE_ bool operator<=(const StringName &p_name) const {
  86. return _data <= p_name._data;
  87. }
  88. _FORCE_INLINE_ bool operator>(const StringName &p_name) const {
  89. return _data > p_name._data;
  90. }
  91. _FORCE_INLINE_ bool operator>=(const StringName &p_name) const {
  92. return _data >= p_name._data;
  93. }
  94. _FORCE_INLINE_ bool operator==(const StringName &p_name) const {
  95. // The real magic of all this mess happens here.
  96. // This is why path comparisons are very fast.
  97. return _data == p_name._data;
  98. }
  99. _FORCE_INLINE_ bool operator!=(const StringName &p_name) const {
  100. return _data != p_name._data;
  101. }
  102. _FORCE_INLINE_ uint32_t hash() const {
  103. if (_data) {
  104. return _data->hash;
  105. } else {
  106. return get_empty_hash();
  107. }
  108. }
  109. _FORCE_INLINE_ const void *data_unique_pointer() const {
  110. return (void *)_data;
  111. }
  112. _FORCE_INLINE_ operator String() const {
  113. if (_data) {
  114. return _data->name;
  115. }
  116. return String();
  117. }
  118. struct AlphCompare {
  119. template <typename LT, typename RT>
  120. _FORCE_INLINE_ bool operator()(const LT &l, const RT &r) const {
  121. return compare(l, r);
  122. }
  123. _FORCE_INLINE_ static bool compare(const StringName &l, const StringName &r) {
  124. return str_compare(l.get_data(), r.get_data()) < 0;
  125. }
  126. _FORCE_INLINE_ static bool compare(const String &l, const StringName &r) {
  127. return str_compare(l.get_data(), r.get_data()) < 0;
  128. }
  129. _FORCE_INLINE_ static bool compare(const StringName &l, const String &r) {
  130. return str_compare(l.get_data(), r.get_data()) < 0;
  131. }
  132. _FORCE_INLINE_ static bool compare(const String &l, const String &r) {
  133. return str_compare(l.get_data(), r.get_data()) < 0;
  134. }
  135. };
  136. StringName &operator=(const StringName &p_name);
  137. StringName &operator=(StringName &&p_name) {
  138. if (_data == p_name._data) {
  139. return *this;
  140. }
  141. unref();
  142. _data = p_name._data;
  143. p_name._data = nullptr;
  144. return *this;
  145. }
  146. StringName(const char *p_name, bool p_static = false);
  147. StringName(const StringName &p_name);
  148. StringName(StringName &&p_name) {
  149. _data = p_name._data;
  150. p_name._data = nullptr;
  151. }
  152. StringName(const String &p_name, bool p_static = false);
  153. StringName() {}
  154. #ifdef SIZE_EXTRA
  155. _NO_INLINE_
  156. #else
  157. _FORCE_INLINE_
  158. #endif
  159. ~StringName() {
  160. if (likely(configured) && _data) { //only free if configured
  161. unref();
  162. }
  163. }
  164. #ifdef DEBUG_ENABLED
  165. static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; }
  166. #endif
  167. };
  168. // Zero-constructing StringName initializes _data to nullptr (and thus empty).
  169. template <>
  170. struct is_zero_constructible<StringName> : std::true_type {};
  171. bool operator==(const String &p_name, const StringName &p_string_name);
  172. bool operator!=(const String &p_name, const StringName &p_string_name);
  173. bool operator==(const char *p_name, const StringName &p_string_name);
  174. bool operator!=(const char *p_name, const StringName &p_string_name);
  175. /*
  176. * The SNAME macro is used to speed up StringName creation, as it allows caching it after the first usage in a very efficient way.
  177. * It should NOT be used everywhere, but instead in places where high performance is required and the creation of a StringName
  178. * can be costly. Places where it should be used are:
  179. * - Control::get_theme_*(<name> and Window::get_theme_*(<name> functions.
  180. * - emit_signal(<name>,..) function
  181. * - call_deferred(<name>,..) function
  182. * - Comparisons to a StringName in overridden _set and _get methods.
  183. *
  184. * Use in places that can be called hundreds of times per frame (or more) is recommended, but this situation is very rare. If in doubt, do not use.
  185. */
  186. #define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = StringName(m_arg, true); return sname; })()