string_name.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/safe_refcount.h"
  34. #include "core/ustring.h"
  35. #define UNIQUE_NODE_PREFIX "%"
  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 = 12,
  43. STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
  44. STRING_TABLE_MASK = STRING_TABLE_LEN - 1
  45. };
  46. struct _Data {
  47. SafeRefCount refcount;
  48. const char *cname;
  49. String name;
  50. String get_name() const { return cname ? String(cname) : name; }
  51. int idx;
  52. uint32_t hash;
  53. _Data *prev;
  54. _Data *next;
  55. _Data() {
  56. cname = nullptr;
  57. next = prev = nullptr;
  58. idx = 0;
  59. hash = 0;
  60. }
  61. };
  62. static _Data *_table[STRING_TABLE_LEN];
  63. _Data *_data;
  64. union _HashUnion {
  65. _Data *ptr;
  66. uint32_t hash;
  67. };
  68. void unref();
  69. friend void register_core_types();
  70. friend void unregister_core_types();
  71. static Mutex lock;
  72. static void setup();
  73. static void cleanup();
  74. static bool configured;
  75. StringName(_Data *p_data) { _data = p_data; }
  76. public:
  77. operator const void *() const { return (_data && (_data->cname || !_data->name.empty())) ? (void *)1 : nullptr; }
  78. bool operator==(const String &p_name) const;
  79. bool operator==(const char *p_name) const;
  80. bool operator!=(const String &p_name) const;
  81. _FORCE_INLINE_ bool is_node_unique_name() const {
  82. if (!_data) {
  83. return false;
  84. }
  85. if (_data->cname != nullptr) {
  86. return (char32_t)_data->cname[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
  87. } else {
  88. return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
  89. }
  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_ uint32_t hash() const {
  100. if (_data) {
  101. return _data->hash;
  102. } else {
  103. return 0;
  104. }
  105. }
  106. _FORCE_INLINE_ const void *data_unique_pointer() const {
  107. return (void *)_data;
  108. }
  109. bool operator!=(const StringName &p_name) const;
  110. _FORCE_INLINE_ operator String() const {
  111. if (_data) {
  112. if (_data->cname) {
  113. return String(_data->cname);
  114. } else {
  115. return _data->name;
  116. }
  117. }
  118. return String();
  119. }
  120. static StringName search(const char *p_name);
  121. static StringName search(const CharType *p_name);
  122. static StringName search(const String &p_name);
  123. struct AlphCompare {
  124. _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
  125. const char *l_cname = l._data ? l._data->cname : "";
  126. const char *r_cname = r._data ? r._data->cname : "";
  127. if (l_cname) {
  128. if (r_cname) {
  129. return is_str_less(l_cname, r_cname);
  130. } else {
  131. return is_str_less(l_cname, r._data->name.ptr());
  132. }
  133. } else {
  134. if (r_cname) {
  135. return is_str_less(l._data->name.ptr(), r_cname);
  136. } else {
  137. return is_str_less(l._data->name.ptr(), r._data->name.ptr());
  138. }
  139. }
  140. }
  141. };
  142. void operator=(const StringName &p_name);
  143. StringName(const char *p_name);
  144. StringName(const StringName &p_name);
  145. StringName(const String &p_name);
  146. StringName(const StaticCString &p_static_string);
  147. StringName();
  148. ~StringName();
  149. };
  150. StringName _scs_create(const char *p_chr);
  151. #endif // STRING_NAME_H