string_name.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/os/mutex.h"
  32. #include "core/string/ustring.h"
  33. #include "core/templates/safe_refcount.h"
  34. #define UNIQUE_NODE_PREFIX "%"
  35. class Main;
  36. struct StaticCString {
  37. const char *ptr;
  38. static StaticCString create(const char *p_ptr);
  39. };
  40. class StringName {
  41. enum {
  42. STRING_TABLE_BITS = 16,
  43. STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
  44. STRING_TABLE_MASK = STRING_TABLE_LEN - 1
  45. };
  46. struct _Data {
  47. SafeRefCount refcount;
  48. SafeNumeric<uint32_t> static_count;
  49. const char *cname = nullptr;
  50. String name;
  51. #ifdef DEBUG_ENABLED
  52. uint32_t debug_references = 0;
  53. #endif
  54. String get_name() const { return cname ? String(cname) : name; }
  55. bool operator==(const String &p_name) const;
  56. bool operator!=(const String &p_name) const;
  57. bool operator==(const char *p_name) const;
  58. bool operator!=(const char *p_name) const;
  59. int idx = 0;
  60. uint32_t hash = 0;
  61. _Data *prev = nullptr;
  62. _Data *next = nullptr;
  63. _Data() {}
  64. };
  65. static inline _Data *_table[STRING_TABLE_LEN];
  66. _Data *_data = nullptr;
  67. void unref();
  68. friend void register_core_types();
  69. friend void unregister_core_types();
  70. friend class Main;
  71. static inline Mutex mutex;
  72. static void setup();
  73. static void cleanup();
  74. static uint32_t get_empty_hash();
  75. static inline bool configured = false;
  76. #ifdef DEBUG_ENABLED
  77. struct DebugSortReferences {
  78. bool operator()(const _Data *p_left, const _Data *p_right) const {
  79. return p_left->debug_references > p_right->debug_references;
  80. }
  81. };
  82. static inline bool debug_stringname = false;
  83. #endif
  84. StringName(_Data *p_data) { _data = p_data; }
  85. public:
  86. operator const void *() const { return (_data && (_data->cname || !_data->name.is_empty())) ? (void *)1 : nullptr; }
  87. bool operator==(const String &p_name) const;
  88. bool operator==(const char *p_name) const;
  89. bool operator!=(const String &p_name) const;
  90. bool operator!=(const char *p_name) const;
  91. char32_t operator[](int p_index) const;
  92. int length() const;
  93. bool is_empty() const;
  94. _FORCE_INLINE_ bool is_node_unique_name() const {
  95. if (!_data) {
  96. return false;
  97. }
  98. if (_data->cname != nullptr) {
  99. return (char32_t)_data->cname[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
  100. } else {
  101. return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
  102. }
  103. }
  104. _FORCE_INLINE_ bool operator<(const StringName &p_name) const {
  105. return _data < p_name._data;
  106. }
  107. _FORCE_INLINE_ bool operator<=(const StringName &p_name) const {
  108. return _data <= p_name._data;
  109. }
  110. _FORCE_INLINE_ bool operator>(const StringName &p_name) const {
  111. return _data > p_name._data;
  112. }
  113. _FORCE_INLINE_ bool operator>=(const StringName &p_name) const {
  114. return _data >= p_name._data;
  115. }
  116. _FORCE_INLINE_ bool operator==(const StringName &p_name) const {
  117. // The real magic of all this mess happens here.
  118. // This is why path comparisons are very fast.
  119. return _data == p_name._data;
  120. }
  121. _FORCE_INLINE_ bool operator!=(const StringName &p_name) const {
  122. return _data != p_name._data;
  123. }
  124. _FORCE_INLINE_ uint32_t hash() const {
  125. if (_data) {
  126. return _data->hash;
  127. } else {
  128. return get_empty_hash();
  129. }
  130. }
  131. _FORCE_INLINE_ const void *data_unique_pointer() const {
  132. return (void *)_data;
  133. }
  134. _FORCE_INLINE_ operator String() const {
  135. if (_data) {
  136. if (_data->cname) {
  137. return String(_data->cname);
  138. } else {
  139. return _data->name;
  140. }
  141. }
  142. return String();
  143. }
  144. static StringName search(const char *p_name);
  145. static StringName search(const char32_t *p_name);
  146. static StringName search(const String &p_name);
  147. struct AlphCompare {
  148. _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
  149. const char *l_cname = l._data ? l._data->cname : "";
  150. const char *r_cname = r._data ? r._data->cname : "";
  151. if (l_cname) {
  152. if (r_cname) {
  153. return is_str_less(l_cname, r_cname);
  154. } else {
  155. return is_str_less(l_cname, r._data->name.ptr());
  156. }
  157. } else {
  158. if (r_cname) {
  159. return is_str_less(l._data->name.ptr(), r_cname);
  160. } else {
  161. return is_str_less(l._data->name.ptr(), r._data->name.ptr());
  162. }
  163. }
  164. }
  165. };
  166. StringName &operator=(const StringName &p_name);
  167. StringName &operator=(StringName &&p_name) {
  168. if (_data == p_name._data) {
  169. return *this;
  170. }
  171. unref();
  172. _data = p_name._data;
  173. p_name._data = nullptr;
  174. return *this;
  175. }
  176. StringName(const char *p_name, bool p_static = false);
  177. StringName(const StringName &p_name);
  178. StringName(StringName &&p_name) {
  179. _data = p_name._data;
  180. p_name._data = nullptr;
  181. }
  182. StringName(const String &p_name, bool p_static = false);
  183. StringName(const StaticCString &p_static_string, bool p_static = false);
  184. StringName() {}
  185. static void assign_static_unique_class_name(StringName *ptr, const char *p_name);
  186. _FORCE_INLINE_ ~StringName() {
  187. if (likely(configured) && _data) { //only free if configured
  188. unref();
  189. }
  190. }
  191. #ifdef DEBUG_ENABLED
  192. static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; }
  193. #endif
  194. };
  195. bool operator==(const String &p_name, const StringName &p_string_name);
  196. bool operator!=(const String &p_name, const StringName &p_string_name);
  197. bool operator==(const char *p_name, const StringName &p_string_name);
  198. bool operator!=(const char *p_name, const StringName &p_string_name);
  199. StringName _scs_create(const char *p_chr, bool p_static = false);
  200. /*
  201. * The SNAME macro is used to speed up StringName creation, as it allows caching it after the first usage in a very efficient way.
  202. * It should NOT be used everywhere, but instead in places where high performance is required and the creation of a StringName
  203. * can be costly. Places where it should be used are:
  204. * - Control::get_theme_*(<name> and Window::get_theme_*(<name> functions.
  205. * - emit_signal(<name>,..) function
  206. * - call_deferred(<name>,..) function
  207. * - Comparisons to a StringName in overridden _set and _get methods.
  208. *
  209. * 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.
  210. */
  211. #define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = _scs_create(m_arg, true); return sname; })()