string_db.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* string_db.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef STRING_DB_H
  30. #define STRING_DB_H
  31. #include "hash_map.h"
  32. #include "ustring.h"
  33. #include "safe_refcount.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. struct StaticCString {
  38. const char *ptr;
  39. static StaticCString create(const char *p_ptr);
  40. };
  41. class StringName {
  42. enum {
  43. STRING_TABLE_BITS=12,
  44. STRING_TABLE_LEN=1<<STRING_TABLE_BITS,
  45. STRING_TABLE_MASK=STRING_TABLE_LEN-1
  46. };
  47. struct _Data {
  48. SafeRefCount refcount;
  49. const char* cname;
  50. String name;
  51. String get_name() const { return cname?String(cname):name; }
  52. int idx;
  53. uint32_t hash;
  54. _Data *prev;
  55. _Data *next;
  56. _Data() { cname=NULL; next=prev=NULL; hash=0; }
  57. };
  58. static _Data *_table[STRING_TABLE_LEN];
  59. _Data *_data;
  60. union _HashUnion {
  61. _Data *ptr;
  62. uint32_t hash;
  63. };
  64. void unref();
  65. friend void register_core_types();
  66. friend void unregister_core_types();
  67. static void setup();
  68. static void cleanup();
  69. static bool configured;
  70. StringName(_Data *p_data) { _data=p_data; }
  71. public:
  72. operator const void*() const { return (_data && (_data->cname || !_data->name.empty()))?(void*)1:0; }
  73. bool operator==(const String& p_name) const;
  74. bool operator==(const char* p_name) const;
  75. bool operator!=(const String& p_name) const;
  76. _FORCE_INLINE_ bool operator<(const StringName& p_name) const {
  77. return _data<p_name._data;
  78. }
  79. _FORCE_INLINE_ bool operator==(const StringName& p_name) const {
  80. // the real magic of all this mess happens here.
  81. // this is why path comparisons are very fast
  82. return _data==p_name._data;
  83. }
  84. _FORCE_INLINE_ uint32_t hash() const {
  85. if (_data)
  86. return _data->hash;
  87. else
  88. return 0;
  89. }
  90. bool operator!=(const StringName& p_name) const;
  91. operator String() const;
  92. static StringName search(const char *p_name);
  93. static StringName search(const CharType *p_name);
  94. static StringName search(const String &p_name);
  95. struct AlphCompare {
  96. _FORCE_INLINE_ bool operator()(const StringName& l,const StringName& r) const {
  97. return l.operator String() < r.operator String();
  98. }
  99. };
  100. void operator=(const StringName& p_name);
  101. StringName(const char *p_name);
  102. StringName(const StringName& p_name);
  103. StringName(const String& p_name);
  104. StringName(const StaticCString& p_static_string);
  105. StringName();
  106. ~StringName();
  107. };
  108. struct StringNameHasher {
  109. static _FORCE_INLINE_ uint32_t hash(const StringName &p_string) { return p_string.hash(); }
  110. };
  111. StringName _scs_create(const char *p_chr);
  112. //#define _SCS(m_cstr) (m_cstr[0]?StringName(StaticCString::create(m_cstr)):StringName())
  113. #define _SCS(m_cstr) _scs_create(m_cstr)
  114. #endif