string_name.h 8.2 KB

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