| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/string/ustring.h" |
| 34 |
#include "core/templates/safe_refcount.h" |
| 35 |
|
| 36 |
#define UNIQUE_NODE_PREFIX "%" |
| 37 |
|
| 38 |
class Main; |
| 39 |
|
| 40 |
class [[nodiscard]] StringName { |
| 41 |
struct Table; |
| 42 |
|
| 43 |
struct _Data { |
| 44 |
SafeRefCount refcount; |
| 45 |
SafeNumeric<uint32_t> static_count; |
| 46 |
String name; |
| 47 |
#ifdef DEBUG_ENABLED |
| 48 |
uint32_t debug_references = 0; |
| 49 |
#endif |
| 50 |
|
| 51 |
uint32_t hash = 0; |
| 52 |
_Data *prev = nullptr; |
| 53 |
_Data *next = nullptr; |
| 54 |
}; |
| 55 |
|
| 56 |
_Data *_data = nullptr; |
| 57 |
|
| 58 |
void unref(); |
| 59 |
friend void register_core_types(); |
| 60 |
friend void unregister_core_types(); |
| 61 |
friend class Main; |
| 62 |
static void setup(); |
| 63 |
static void cleanup(); |
| 64 |
static uint32_t get_empty_hash(); |
| 65 |
static inline bool configured = false; |
| 66 |
#ifdef DEBUG_ENABLED |
| 67 |
struct DebugSortReferences { |
| 68 |
bool operator()(const _Data *p_left, const _Data *p_right) const { |
| 69 |
return p_left->debug_references > p_right->debug_references; |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
static inline bool debug_stringname = false; |
| 74 |
#endif |
| 75 |
|
| 76 |
StringName(_Data *p_data) { _data = p_data; } |
| 77 |
|
| 78 |
public: |
| 79 |
_FORCE_INLINE_ explicit operator bool() const { return _data; } |
| 80 |
|
| 81 |
bool operator==(const String &p_name) const; |
| 82 |
bool operator==(const char *p_name) const; |
| 83 |
bool operator!=(const String &p_name) const; |
| 84 |
bool operator!=(const char *p_name) const; |
| 85 |
|
| 86 |
const char32_t *get_data() const { return _data ? _data->name.ptr() : U""; } |
| 87 |
char32_t operator[](int p_index) const; |
| 88 |
int length() const; |
| 89 |
_FORCE_INLINE_ bool is_empty() const { return !_data; } |
| 90 |
|
| 91 |
_FORCE_INLINE_ bool is_node_unique_name() const { |
| 92 |
if (!_data) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0]; |
| 96 |
} |
| 97 |
_FORCE_INLINE_ bool operator<(const StringName &p_name) const { |
| 98 |
return _data < p_name._data; |
| 99 |
} |
| 100 |
_FORCE_INLINE_ bool operator<=(const StringName &p_name) const { |
| 101 |
return _data <= p_name._data; |
| 102 |
} |
| 103 |
_FORCE_INLINE_ bool operator>(const StringName &p_name) const { |
| 104 |
return _data > p_name._data; |
| 105 |
} |
| 106 |
_FORCE_INLINE_ bool operator>=(const StringName &p_name) const { |
| 107 |
return _data >= p_name._data; |
| 108 |
} |
| 109 |
_FORCE_INLINE_ bool operator==(const StringName &p_name) const { |
| 110 |
|
| 111 |
|
| 112 |
return _data == p_name._data; |
| 113 |
} |
| 114 |
_FORCE_INLINE_ bool operator!=(const StringName &p_name) const { |
| 115 |
return _data != p_name._data; |
| 116 |
} |
| 117 |
_FORCE_INLINE_ uint32_t hash() const { |
| 118 |
if (_data) { |
| 119 |
return _data->hash; |
| 120 |
} else { |
| 121 |
return get_empty_hash(); |
| 122 |
} |
| 123 |
} |
| 124 |
_FORCE_INLINE_ const void *data_unique_pointer() const { |
| 125 |
return (void *)_data; |
| 126 |
} |
| 127 |
|
| 128 |
_FORCE_INLINE_ operator String() const { |
| 129 |
if (_data) { |
| 130 |
return _data->name; |
| 131 |
} |
| 132 |
|
| 133 |
return String(); |
| 134 |
} |
| 135 |
|
| 136 |
struct AlphCompare { |
| 137 |
template <typename LT, typename RT> |
| 138 |
_FORCE_INLINE_ bool operator()(const LT &l, const RT &r) const { |
| 139 |
return compare(l, r); |
| 140 |
} |
| 141 |
_FORCE_INLINE_ static bool compare(const StringName &l, const StringName &r) { |
| 142 |
return str_compare(l.get_data(), r.get_data()) < 0; |
| 143 |
} |
| 144 |
_FORCE_INLINE_ static bool compare(const String &l, const StringName &r) { |
| 145 |
return str_compare(l.get_data(), r.get_data()) < 0; |
| 146 |
} |
| 147 |
_FORCE_INLINE_ static bool compare(const StringName &l, const String &r) { |
| 148 |
return str_compare(l.get_data(), r.get_data()) < 0; |
| 149 |
} |
| 150 |
_FORCE_INLINE_ static bool compare(const String &l, const String &r) { |
| 151 |
return str_compare(l.get_data(), r.get_data()) < 0; |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
StringName &operator=(const StringName &p_name); |
| 156 |
StringName &operator=(StringName &&p_name) { |
| 157 |
if (_data == p_name._data) { |
| 158 |
return *this; |
| 159 |
} |
| 160 |
|
| 161 |
unref(); |
| 162 |
_data = p_name._data; |
| 163 |
p_name._data = nullptr; |
| 164 |
return *this; |
| 165 |
} |
| 166 |
StringName(const char *p_name, bool p_static = false); |
| 167 |
StringName(const StringName &p_name); |
| 168 |
StringName(StringName &&p_name) { |
| 169 |
_data = p_name._data; |
| 170 |
p_name._data = nullptr; |
| 171 |
} |
| 172 |
StringName(const String &p_name, bool p_static = false); |
| 173 |
StringName() {} |
| 174 |
|
| 175 |
#ifdef SIZE_EXTRA |
| 176 |
_NO_INLINE_ |
| 177 |
#else |
| 178 |
_FORCE_INLINE_ |
| 179 |
#endif |
| 180 |
~StringName() { |
| 181 |
if (likely(configured) && _data) { |
| 182 |
unref(); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
#ifdef DEBUG_ENABLED |
| 187 |
static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; } |
| 188 |
#endif |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
template <> |
| 193 |
struct is_zero_constructible<StringName> : std::true_type {}; |
| 194 |
|
| 195 |
bool operator==(const String &p_name, const StringName &p_string_name); |
| 196 |
bool operator!=(const String &p_name, const StringName &p_string_name); |
| 197 |
bool operator==(const char *p_name, const StringName &p_string_name); |
| 198 |
bool operator!=(const char *p_name, const StringName &p_string_name); |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = StringName(m_arg, true); return sname; })() |