string_name.h 7.5 KB

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