string_name.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = 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 = nullptr;
  49. String name;
  50. String get_name() const { return cname ? String(cname) : name; }
  51. int idx = 0;
  52. uint32_t hash = 0;
  53. _Data *prev = nullptr;
  54. _Data *next = nullptr;
  55. _Data() {}
  56. };
  57. static _Data *_table[STRING_TABLE_LEN];
  58. _Data *_data = nullptr;
  59. union _HashUnion {
  60. _Data *ptr;
  61. uint32_t hash;
  62. };
  63. void unref();
  64. friend void register_core_types();
  65. friend void unregister_core_types();
  66. friend class Main;
  67. static Mutex mutex;
  68. static void setup();
  69. static void cleanup();
  70. static bool configured;
  71. StringName(_Data *p_data) { _data = p_data; }
  72. public:
  73. operator const void *() const { return (_data && (_data->cname || !_data->name.is_empty())) ? (void *)1 : nullptr; }
  74. bool operator==(const String &p_name) const;
  75. bool operator==(const char *p_name) const;
  76. bool operator!=(const String &p_name) const;
  77. _FORCE_INLINE_ bool operator<(const StringName &p_name) const {
  78. return _data < p_name._data;
  79. }
  80. _FORCE_INLINE_ bool operator==(const StringName &p_name) const {
  81. // the real magic of all this mess happens here.
  82. // this is why path comparisons are very fast
  83. return _data == p_name._data;
  84. }
  85. _FORCE_INLINE_ uint32_t hash() const {
  86. if (_data) {
  87. return _data->hash;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. _FORCE_INLINE_ const void *data_unique_pointer() const {
  93. return (void *)_data;
  94. }
  95. bool operator!=(const StringName &p_name) const;
  96. _FORCE_INLINE_ operator String() const {
  97. if (_data) {
  98. if (_data->cname) {
  99. return String(_data->cname);
  100. } else {
  101. return _data->name;
  102. }
  103. }
  104. return String();
  105. }
  106. static StringName search(const char *p_name);
  107. static StringName search(const char32_t *p_name);
  108. static StringName search(const String &p_name);
  109. struct AlphCompare {
  110. _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
  111. const char *l_cname = l._data ? l._data->cname : "";
  112. const char *r_cname = r._data ? r._data->cname : "";
  113. if (l_cname) {
  114. if (r_cname) {
  115. return is_str_less(l_cname, r_cname);
  116. } else {
  117. return is_str_less(l_cname, r._data->name.ptr());
  118. }
  119. } else {
  120. if (r_cname) {
  121. return is_str_less(l._data->name.ptr(), r_cname);
  122. } else {
  123. return is_str_less(l._data->name.ptr(), r._data->name.ptr());
  124. }
  125. }
  126. }
  127. };
  128. void operator=(const StringName &p_name);
  129. StringName(const char *p_name);
  130. StringName(const StringName &p_name);
  131. StringName(const String &p_name);
  132. StringName(const StaticCString &p_static_string);
  133. StringName() {}
  134. ~StringName();
  135. };
  136. bool operator==(const String &p_name, const StringName &p_string_name);
  137. bool operator!=(const String &p_name, const StringName &p_string_name);
  138. bool operator==(const char *p_name, const StringName &p_string_name);
  139. bool operator!=(const char *p_name, const StringName &p_string_name);
  140. StringName _scs_create(const char *p_chr);
  141. #endif // STRING_NAME_H