string_name.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/safe_refcount.h"
  34. #include "core/ustring.h"
  35. struct StaticCString {
  36. const char *ptr;
  37. static StaticCString create(const char *p_ptr);
  38. };
  39. class StringName {
  40. enum {
  41. STRING_TABLE_BITS = 12,
  42. STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
  43. STRING_TABLE_MASK = STRING_TABLE_LEN - 1
  44. };
  45. struct _Data {
  46. SafeRefCount refcount;
  47. const char *cname;
  48. String name;
  49. String get_name() const { return cname ? String(cname) : name; }
  50. int idx;
  51. uint32_t hash;
  52. _Data *prev;
  53. _Data *next;
  54. _Data() {
  55. cname = NULL;
  56. next = prev = NULL;
  57. idx = 0;
  58. hash = 0;
  59. }
  60. };
  61. static _Data *_table[STRING_TABLE_LEN];
  62. _Data *_data;
  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. static Mutex lock;
  71. static void setup();
  72. static void cleanup();
  73. static bool configured;
  74. StringName(_Data *p_data) { _data = p_data; }
  75. public:
  76. operator const void *() const { return (_data && (_data->cname || !_data->name.empty())) ? (void *)1 : 0; }
  77. bool operator==(const String &p_name) const;
  78. bool operator==(const char *p_name) const;
  79. bool operator!=(const String &p_name) const;
  80. _FORCE_INLINE_ bool operator<(const StringName &p_name) const {
  81. return _data < p_name._data;
  82. }
  83. _FORCE_INLINE_ bool operator==(const StringName &p_name) const {
  84. // the real magic of all this mess happens here.
  85. // this is why path comparisons are very fast
  86. return _data == p_name._data;
  87. }
  88. _FORCE_INLINE_ uint32_t hash() const {
  89. if (_data)
  90. return _data->hash;
  91. else
  92. return 0;
  93. }
  94. _FORCE_INLINE_ const void *data_unique_pointer() const {
  95. return (void *)_data;
  96. }
  97. bool operator!=(const StringName &p_name) const;
  98. _FORCE_INLINE_ operator String() const {
  99. if (_data) {
  100. if (_data->cname)
  101. return String(_data->cname);
  102. else
  103. return _data->name;
  104. }
  105. return String();
  106. }
  107. static StringName search(const char *p_name);
  108. static StringName search(const CharType *p_name);
  109. static StringName search(const String &p_name);
  110. struct AlphCompare {
  111. _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
  112. const char *l_cname = l._data ? l._data->cname : "";
  113. const char *r_cname = r._data ? r._data->cname : "";
  114. if (l_cname) {
  115. if (r_cname)
  116. return is_str_less(l_cname, r_cname);
  117. else
  118. return is_str_less(l_cname, r._data->name.ptr());
  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. void operator=(const StringName &p_name);
  128. StringName(const char *p_name);
  129. StringName(const StringName &p_name);
  130. StringName(const String &p_name);
  131. StringName(const StaticCString &p_static_string);
  132. StringName();
  133. ~StringName();
  134. };
  135. StringName _scs_create(const char *p_chr);
  136. #endif // STRING_NAME_H