string_name.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* string_name.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. 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. int idx = 0;
  56. uint32_t hash = 0;
  57. _Data *prev = nullptr;
  58. _Data *next = nullptr;
  59. _Data() {}
  60. };
  61. static _Data *_table[STRING_TABLE_LEN];
  62. _Data *_data = nullptr;
  63. union _HashUnion {
  64. _Data *ptr;
  65. uint32_t hash;
  66. };
  67. void unref();
  68. friend void register_core_types();
  69. friend void unregister_core_types();
  70. friend class Main;
  71. static Mutex mutex;
  72. static void setup();
  73. static void cleanup();
  74. static bool configured;
  75. #ifdef DEBUG_ENABLED
  76. struct DebugSortReferences {
  77. bool operator()(const _Data *p_left, const _Data *p_right) const {
  78. return p_left->debug_references > p_right->debug_references;
  79. }
  80. };
  81. static bool debug_stringname;
  82. #endif
  83. StringName(_Data *p_data) { _data = p_data; }
  84. public:
  85. operator const void *() const { return (_data && (_data->cname || !_data->name.is_empty())) ? (void *)1 : nullptr; }
  86. bool operator==(const String &p_name) const;
  87. bool operator==(const char *p_name) const;
  88. bool operator!=(const String &p_name) const;
  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. // the real magic of all this mess happens here.
  94. // this is why path comparisons are very fast
  95. return _data == p_name._data;
  96. }
  97. _FORCE_INLINE_ uint32_t hash() const {
  98. if (_data) {
  99. return _data->hash;
  100. } else {
  101. return 0;
  102. }
  103. }
  104. _FORCE_INLINE_ const void *data_unique_pointer() const {
  105. return (void *)_data;
  106. }
  107. bool operator!=(const StringName &p_name) const;
  108. _FORCE_INLINE_ operator String() const {
  109. if (_data) {
  110. if (_data->cname) {
  111. return String(_data->cname);
  112. } else {
  113. return _data->name;
  114. }
  115. }
  116. return String();
  117. }
  118. static StringName search(const char *p_name);
  119. static StringName search(const char32_t *p_name);
  120. static StringName search(const String &p_name);
  121. struct AlphCompare {
  122. _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
  123. const char *l_cname = l._data ? l._data->cname : "";
  124. const char *r_cname = r._data ? r._data->cname : "";
  125. if (l_cname) {
  126. if (r_cname) {
  127. return is_str_less(l_cname, r_cname);
  128. } else {
  129. return is_str_less(l_cname, r._data->name.ptr());
  130. }
  131. } else {
  132. if (r_cname) {
  133. return is_str_less(l._data->name.ptr(), r_cname);
  134. } else {
  135. return is_str_less(l._data->name.ptr(), r._data->name.ptr());
  136. }
  137. }
  138. }
  139. };
  140. void operator=(const StringName &p_name);
  141. StringName(const char *p_name, bool p_static = false);
  142. StringName(const StringName &p_name);
  143. StringName(const String &p_name, bool p_static = false);
  144. StringName(const StaticCString &p_static_string, bool p_static = false);
  145. StringName() {}
  146. _FORCE_INLINE_ ~StringName() {
  147. if (likely(configured) && _data) { //only free if configured
  148. unref();
  149. }
  150. }
  151. #ifdef DEBUG_ENABLED
  152. static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; }
  153. #endif
  154. };
  155. bool operator==(const String &p_name, const StringName &p_string_name);
  156. bool operator!=(const String &p_name, const StringName &p_string_name);
  157. bool operator==(const char *p_name, const StringName &p_string_name);
  158. bool operator!=(const char *p_name, const StringName &p_string_name);
  159. StringName _scs_create(const char *p_chr, bool p_static = false);
  160. #define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = _scs_create(m_arg, true); return sname; })()
  161. #endif // STRING_NAME_H