string_db.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*************************************************************************/
  2. /* string_db.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/mutex.h"
  34. #include "safe_refcount.h"
  35. #include "ustring.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. struct StaticCString {
  40. const char *ptr;
  41. static StaticCString create(const char *p_ptr);
  42. };
  43. class StringName {
  44. enum {
  45. STRING_TABLE_BITS = 12,
  46. STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
  47. STRING_TABLE_MASK = STRING_TABLE_LEN - 1
  48. };
  49. struct _Data {
  50. SafeRefCount refcount;
  51. const char *cname;
  52. String name;
  53. String get_name() const { return cname ? String(cname) : name; }
  54. int idx;
  55. uint32_t hash;
  56. _Data *prev;
  57. _Data *next;
  58. _Data() {
  59. cname = NULL;
  60. next = prev = NULL;
  61. hash = 0;
  62. }
  63. };
  64. static _Data *_table[STRING_TABLE_LEN];
  65. _Data *_data;
  66. union _HashUnion {
  67. _Data *ptr;
  68. uint32_t hash;
  69. };
  70. void unref();
  71. friend void register_core_types();
  72. friend void unregister_core_types();
  73. static Mutex *lock;
  74. static void setup();
  75. static void cleanup();
  76. static bool configured;
  77. StringName(_Data *p_data) { _data = p_data; }
  78. public:
  79. operator const void *() const { return (_data && (_data->cname || !_data->name.empty())) ? (void *)1 : 0; }
  80. bool operator==(const String &p_name) const;
  81. bool operator==(const char *p_name) const;
  82. bool operator!=(const String &p_name) const;
  83. _FORCE_INLINE_ bool operator<(const StringName &p_name) const {
  84. return _data < p_name._data;
  85. }
  86. _FORCE_INLINE_ bool operator==(const StringName &p_name) const {
  87. // the real magic of all this mess happens here.
  88. // this is why path comparisons are very fast
  89. return _data == p_name._data;
  90. }
  91. _FORCE_INLINE_ uint32_t hash() const {
  92. if (_data)
  93. return _data->hash;
  94. else
  95. return 0;
  96. }
  97. _FORCE_INLINE_ const void *data_unique_pointer() const {
  98. return (void *)_data;
  99. }
  100. bool operator!=(const StringName &p_name) const;
  101. _FORCE_INLINE_ operator String() const {
  102. if (_data) {
  103. if (_data->cname)
  104. return String(_data->cname);
  105. else
  106. return _data->name;
  107. }
  108. return String();
  109. }
  110. static StringName search(const char *p_name);
  111. static StringName search(const CharType *p_name);
  112. static StringName search(const String &p_name);
  113. struct AlphCompare {
  114. _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
  115. return l.operator String() < r.operator String();
  116. }
  117. };
  118. void operator=(const StringName &p_name);
  119. StringName(const char *p_name);
  120. StringName(const StringName &p_name);
  121. StringName(const String &p_name);
  122. StringName(const StaticCString &p_static_string);
  123. StringName();
  124. ~StringName();
  125. };
  126. struct StringNameHasher {
  127. static _FORCE_INLINE_ uint32_t hash(const StringName &p_string) { return p_string.hash(); }
  128. };
  129. StringName _scs_create(const char *p_chr);
  130. #endif