| 1 | /**************************************************************************/ |
| 2 | /* string_name.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #ifndef STRING_NAME_H |
| 32 | #define STRING_NAME_H |
| 33 | |
| 34 | #include "core/os/mutex.h" |
| 35 | #include "core/string/ustring.h" |
| 36 | #include "core/templates/safe_refcount.h" |
| 37 | |
| 38 | #define UNIQUE_NODE_PREFIX "%" |
| 39 | |
| 40 | class Main; |
| 41 | |
| 42 | struct StaticCString { |
| 43 | const char *ptr; |
| 44 | static StaticCString create(const char *p_ptr); |
| 45 | }; |
| 46 | |
| 47 | class StringName { |
| 48 | enum { |
| 49 | STRING_TABLE_BITS = 16, |
| 50 | STRING_TABLE_LEN = 1 << STRING_TABLE_BITS, |
| 51 | STRING_TABLE_MASK = STRING_TABLE_LEN - 1 |
| 52 | }; |
| 53 | |
| 54 | struct _Data { |
| 55 | SafeRefCount refcount; |
| 56 | SafeNumeric<uint32_t> static_count; |
| 57 | const char *cname = nullptr; |
| 58 | String name; |
| 59 | #ifdef DEBUG_ENABLED |
| 60 | uint32_t debug_references = 0; |
| 61 | #endif |
| 62 | String get_name() const { return cname ? String(cname) : name; } |
| 63 | int idx = 0; |
| 64 | uint32_t hash = 0; |
| 65 | _Data *prev = nullptr; |
| 66 | _Data *next = nullptr; |
| 67 | _Data() {} |
| 68 | }; |
| 69 | |
| 70 | static _Data *_table[STRING_TABLE_LEN]; |
| 71 | |
| 72 | _Data *_data = nullptr; |
| 73 | |
| 74 | union _HashUnion { |
| 75 | _Data *ptr = nullptr; |
| 76 | uint32_t hash; |
| 77 | }; |
| 78 | |
| 79 | void unref(); |
| 80 | friend void register_core_types(); |
| 81 | friend void unregister_core_types(); |
| 82 | friend class Main; |
| 83 | static Mutex mutex; |
| 84 | static void setup(); |
| 85 | static void cleanup(); |
| 86 | static bool configured; |
| 87 | #ifdef DEBUG_ENABLED |
| 88 | struct DebugSortReferences { |
| 89 | bool operator()(const _Data *p_left, const _Data *p_right) const { |
| 90 | return p_left->debug_references > p_right->debug_references; |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | static bool debug_stringname; |
| 95 | #endif |
| 96 | |
| 97 | StringName(_Data *p_data) { _data = p_data; } |
| 98 | |
| 99 | public: |
| 100 | operator const void *() const { return (_data && (_data->cname || !_data->name.is_empty())) ? (void *)1 : nullptr; } |
| 101 | |
| 102 | bool operator==(const String &p_name) const; |
| 103 | bool operator==(const char *p_name) const; |
| 104 | bool operator!=(const String &p_name) const; |
| 105 | bool operator!=(const char *p_name) const; |
| 106 | |
| 107 | _FORCE_INLINE_ bool is_node_unique_name() const { |
| 108 | if (!_data) { |
| 109 | return false; |
| 110 | } |
| 111 | if (_data->cname != nullptr) { |
| 112 | return (char32_t)_data->cname[0] == (char32_t)UNIQUE_NODE_PREFIX[0]; |
| 113 | } else { |
| 114 | return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0]; |
| 115 | } |
| 116 | } |
| 117 | _FORCE_INLINE_ bool operator<(const StringName &p_name) const { |
| 118 | return _data < p_name._data; |
| 119 | } |
| 120 | _FORCE_INLINE_ bool operator<=(const StringName &p_name) const { |
| 121 | return _data <= p_name._data; |
| 122 | } |
| 123 | _FORCE_INLINE_ bool operator>(const StringName &p_name) const { |
| 124 | return _data > p_name._data; |
| 125 | } |
| 126 | _FORCE_INLINE_ bool operator>=(const StringName &p_name) const { |
| 127 | return _data >= p_name._data; |
| 128 | } |
| 129 | _FORCE_INLINE_ bool operator==(const StringName &p_name) const { |
| 130 | // the real magic of all this mess happens here. |
| 131 | // this is why path comparisons are very fast |
| 132 | return _data == p_name._data; |
| 133 | } |
| 134 | _FORCE_INLINE_ uint32_t hash() const { |
| 135 | if (_data) { |
| 136 | return _data->hash; |
| 137 | } else { |
| 138 | return 0; |
| 139 | } |
| 140 | } |
| 141 | _FORCE_INLINE_ const void *data_unique_pointer() const { |
| 142 | return (void *)_data; |
| 143 | } |
| 144 | bool operator!=(const StringName &p_name) const; |
| 145 | |
| 146 | _FORCE_INLINE_ operator String() const { |
| 147 | if (_data) { |
| 148 | if (_data->cname) { |
| 149 | return String(_data->cname); |
| 150 | } else { |
| 151 | return _data->name; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return String(); |
| 156 | } |
| 157 | |
| 158 | static StringName search(const char *p_name); |
| 159 | static StringName search(const char32_t *p_name); |
| 160 | static StringName search(const String &p_name); |
| 161 | |
| 162 | struct AlphCompare { |
| 163 | _FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const { |
| 164 | const char *l_cname = l._data ? l._data->cname : "" ; |
| 165 | const char *r_cname = r._data ? r._data->cname : "" ; |
| 166 | |
| 167 | if (l_cname) { |
| 168 | if (r_cname) { |
| 169 | return is_str_less(l_cname, r_cname); |
| 170 | } else { |
| 171 | return is_str_less(l_cname, r._data->name.ptr()); |
| 172 | } |
| 173 | } else { |
| 174 | if (r_cname) { |
| 175 | return is_str_less(l._data->name.ptr(), r_cname); |
| 176 | } else { |
| 177 | return is_str_less(l._data->name.ptr(), r._data->name.ptr()); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | void operator=(const StringName &p_name); |
| 184 | StringName(const char *p_name, bool p_static = false); |
| 185 | StringName(const StringName &p_name); |
| 186 | StringName(const String &p_name, bool p_static = false); |
| 187 | StringName(const StaticCString &p_static_string, bool p_static = false); |
| 188 | StringName() {} |
| 189 | |
| 190 | static void assign_static_unique_class_name(StringName *ptr, const char *p_name); |
| 191 | _FORCE_INLINE_ ~StringName() { |
| 192 | if (likely(configured) && _data) { //only free if configured |
| 193 | unref(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #ifdef DEBUG_ENABLED |
| 198 | static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; } |
| 199 | #endif |
| 200 | }; |
| 201 | |
| 202 | bool operator==(const String &p_name, const StringName &p_string_name); |
| 203 | bool operator!=(const String &p_name, const StringName &p_string_name); |
| 204 | bool operator==(const char *p_name, const StringName &p_string_name); |
| 205 | bool operator!=(const char *p_name, const StringName &p_string_name); |
| 206 | |
| 207 | StringName _scs_create(const char *p_chr, bool p_static = false); |
| 208 | |
| 209 | /* |
| 210 | * The SNAME macro is used to speed up StringName creation, as it allows caching it after the first usage in a very efficient way. |
| 211 | * It should NOT be used everywhere, but instead in places where high performance is required and the creation of a StringName |
| 212 | * can be costly. Places where it should be used are: |
| 213 | * - Control::get_theme_*(<name> and Window::get_theme_*(<name> functions. |
| 214 | * - emit_signal(<name>,..) function |
| 215 | * - call_deferred(<name>,..) function |
| 216 | * - Comparisons to a StringName in overridden _set and _get methods. |
| 217 | * |
| 218 | * Use in places that can be called hundreds of times per frame (or more) is recommended, but this situation is very rare. If in doubt, do not use. |
| 219 | */ |
| 220 | |
| 221 | #define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = _scs_create(m_arg, true); return sname; })() |
| 222 | |
| 223 | #endif // STRING_NAME_H |
| 224 | |