Always use String as StringName backing internally.

This commit is contained in:
Lukas Tenbrink
2025-04-03 22:09:35 +02:00
parent 1696ab0cb6
commit 91fe434a86
16 changed files with 245 additions and 383 deletions

View File

@@ -38,11 +38,6 @@
class Main;
struct StaticCString {
const char *ptr;
static StaticCString create(const char *p_ptr);
};
class StringName {
enum {
STRING_TABLE_BITS = 16,
@@ -53,12 +48,11 @@ class StringName {
struct _Data {
SafeRefCount refcount;
SafeNumeric<uint32_t> static_count;
const char *cname = nullptr;
String name;
#ifdef DEBUG_ENABLED
uint32_t debug_references = 0;
#endif
String get_name() const { return cname ? String(cname) : name; }
const String &get_name() const { return name; }
bool operator==(const String &p_name) const;
bool operator!=(const String &p_name) const;
bool operator==(const char *p_name) const;
@@ -97,13 +91,14 @@ class StringName {
StringName(_Data *p_data) { _data = p_data; }
public:
explicit operator bool() const { return _data && (_data->cname || !_data->name.is_empty()); }
explicit operator bool() const { return _data && !_data->name.is_empty(); }
bool operator==(const String &p_name) const;
bool operator==(const char *p_name) const;
bool operator!=(const String &p_name) const;
bool operator!=(const char *p_name) const;
const char32_t *get_data() const { return _data ? _data->name.ptr() : U""; }
char32_t operator[](int p_index) const;
int length() const;
bool is_empty() const;
@@ -112,11 +107,7 @@ public:
if (!_data) {
return false;
}
if (_data->cname != nullptr) {
return (char32_t)_data->cname[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
} else {
return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
}
return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
}
_FORCE_INLINE_ bool operator<(const StringName &p_name) const {
return _data < p_name._data;
@@ -151,11 +142,7 @@ public:
_FORCE_INLINE_ operator String() const {
if (_data) {
if (_data->cname) {
return String(_data->cname);
} else {
return _data->name;
}
return _data->name;
}
return String();
@@ -171,40 +158,13 @@ public:
return compare(l, r);
}
_FORCE_INLINE_ static bool compare(const StringName &l, const StringName &r) {
const char *l_cname = l._data ? l._data->cname : "";
const char *r_cname = r._data ? r._data->cname : "";
if (l_cname) {
if (r_cname) {
return str_compare(l_cname, r_cname) < 0;
} else {
return str_compare(l_cname, r._data->name.ptr()) < 0;
}
} else {
if (r_cname) {
return str_compare(l._data->name.ptr(), r_cname) < 0;
} else {
return str_compare(l._data->name.ptr(), r._data->name.ptr()) < 0;
}
}
return str_compare(l.get_data(), r.get_data()) < 0;
}
_FORCE_INLINE_ static bool compare(const String &l, const StringName &r) {
const char *r_cname = r._data ? r._data->cname : "";
if (r_cname) {
return str_compare(l.get_data(), r_cname) < 0;
} else {
return str_compare(l.get_data(), r._data->name.ptr()) < 0;
}
return str_compare(l.get_data(), r.get_data()) < 0;
}
_FORCE_INLINE_ static bool compare(const StringName &l, const String &r) {
const char *l_cname = l._data ? l._data->cname : "";
if (l_cname) {
return str_compare(l_cname, r.get_data()) < 0;
} else {
return str_compare(l._data->name.ptr(), r.get_data()) < 0;
}
return str_compare(l.get_data(), r.get_data()) < 0;
}
_FORCE_INLINE_ static bool compare(const String &l, const String &r) {
return str_compare(l.get_data(), r.get_data()) < 0;
@@ -229,7 +189,6 @@ public:
p_name._data = nullptr;
}
StringName(const String &p_name, bool p_static = false);
StringName(const StaticCString &p_static_string, bool p_static = false);
StringName() {}
static void assign_static_unique_class_name(StringName *ptr, const char *p_name);
@@ -259,8 +218,6 @@ bool operator!=(const String &p_name, const StringName &p_string_name);
bool operator==(const char *p_name, const StringName &p_string_name);
bool operator!=(const char *p_name, const StringName &p_string_name);
StringName _scs_create(const char *p_chr, bool p_static = false);
/*
* The SNAME macro is used to speed up StringName creation, as it allows caching it after the first usage in a very efficient way.
* It should NOT be used everywhere, but instead in places where high performance is required and the creation of a StringName
@@ -273,4 +230,4 @@ StringName _scs_create(const char *p_chr, bool p_static = false);
* 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.
*/
#define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = _scs_create(m_arg, true); return sname; })()
#define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = StringName(m_arg, true); return sname; })()