Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/string_name.h
20838 views
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
#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
// The real magic of all this mess happens here.
111
// This is why path comparisons are very fast.
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) { //only free if configured
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
// Zero-constructing StringName initializes _data to nullptr (and thus empty).
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
* The SNAME macro is used to speed up StringName creation, as it allows caching it after the first usage in a very efficient way.
202
* It should NOT be used everywhere, but instead in places where high performance is required and the creation of a StringName
203
* can be costly. Places where it should be used are:
204
* - Control::get_theme_*(<name> and Window::get_theme_*(<name> functions.
205
* - emit_signal(<name>,..) function
206
* - call_deferred(<name>,..) function
207
* - Comparisons to a StringName in overridden _set and _get methods.
208
*
209
* 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.
210
*/
211
212
#define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = StringName(m_arg, true); return sname; })()
213
214