2
0

string_db.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* string_db.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_DB_H
  31. #define STRING_DB_H
  32. #include "hash_map.h"
  33. #include "safe_refcount.h"
  34. #include "ustring.h"
  35. /**
  36. @author Juan Linietsky <[email protected]>
  37. */
  38. struct StaticCString {
  39. const char *ptr;
  40. static StaticCString create(const char *p_ptr);
  41. };
  42. class StringName {
  43. enum {
  44. STRING_TABLE_BITS = 12,
  45. STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
  46. STRING_TABLE_MASK = STRING_TABLE_LEN - 1
  47. };
  48. struct _Data {
  49. SafeRefCount refcount;
  50. const char *cname;
  51. String name;
  52. String get_name() const { return cname ? String(cname) : name; }
  53. int idx;
  54. uint32_t hash;
  55. _Data *prev;
  56. _Data *next;
  57. _Data() {
  58. cname = NULL;
  59. next = prev = NULL;
  60. hash = 0;
  61. }
  62. };
  63. static _Data *_table[STRING_TABLE_LEN];
  64. _Data *_data;
  65. union _HashUnion {
  66. _Data *ptr;
  67. uint32_t hash;
  68. };
  69. void unref();
  70. friend void register_core_types();
  71. friend void unregister_core_types();
  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 : 0; }
  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 operator<(const StringName &p_name) const {
  82. return _data < p_name._data;
  83. }
  84. _FORCE_INLINE_ bool operator==(const StringName &p_name) const {
  85. // the real magic of all this mess happens here.
  86. // this is why path comparisons are very fast
  87. return _data == p_name._data;
  88. }
  89. _FORCE_INLINE_ uint32_t hash() const {
  90. if (_data)
  91. return _data->hash;
  92. else
  93. return 0;
  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. return String();
  104. }
  105. static StringName search(const char *p_name);
  106. static StringName search(const CharType *p_name);
  107. static StringName search(const String &p_name);
  108. struct AlphCompare {
  109. _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
  110. return l.operator String() < r.operator String();
  111. }
  112. };
  113. void operator=(const StringName &p_name);
  114. StringName(const char *p_name);
  115. StringName(const StringName &p_name);
  116. StringName(const String &p_name);
  117. StringName(const StaticCString &p_static_string);
  118. StringName();
  119. ~StringName();
  120. };
  121. struct StringNameHasher {
  122. static _FORCE_INLINE_ uint32_t hash(const StringName &p_string) { return p_string.hash(); }
  123. };
  124. StringName _scs_create(const char *p_chr);
  125. //#define _SCS(m_cstr) (m_cstr[0]?StringName(StaticCString::create(m_cstr)):StringName())
  126. #define _SCS(m_cstr) _scs_create(m_cstr)
  127. #endif