Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/string_name.h
9903 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
_Data() {}
55
};
56
57
_Data *_data = nullptr;
58
59
void unref();
60
friend void register_core_types();
61
friend void unregister_core_types();
62
friend class Main;
63
static void setup();
64
static void cleanup();
65
static uint32_t get_empty_hash();
66
static inline bool configured = false;
67
#ifdef DEBUG_ENABLED
68
struct DebugSortReferences {
69
bool operator()(const _Data *p_left, const _Data *p_right) const {
70
return p_left->debug_references > p_right->debug_references;
71
}
72
};
73
74
static inline bool debug_stringname = false;
75
#endif
76
77
StringName(_Data *p_data) { _data = p_data; }
78
79
public:
80
_FORCE_INLINE_ explicit operator bool() const { return _data; }
81
82
bool operator==(const String &p_name) const;
83
bool operator==(const char *p_name) const;
84
bool operator!=(const String &p_name) const;
85
bool operator!=(const char *p_name) const;
86
87
const char32_t *get_data() const { return _data ? _data->name.ptr() : U""; }
88
char32_t operator[](int p_index) const;
89
int length() const;
90
_FORCE_INLINE_ bool is_empty() const { return !_data; }
91
92
_FORCE_INLINE_ bool is_node_unique_name() const {
93
if (!_data) {
94
return false;
95
}
96
return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
97
}
98
_FORCE_INLINE_ bool operator<(const StringName &p_name) const {
99
return _data < p_name._data;
100
}
101
_FORCE_INLINE_ bool operator<=(const StringName &p_name) const {
102
return _data <= p_name._data;
103
}
104
_FORCE_INLINE_ bool operator>(const StringName &p_name) const {
105
return _data > p_name._data;
106
}
107
_FORCE_INLINE_ bool operator>=(const StringName &p_name) const {
108
return _data >= p_name._data;
109
}
110
_FORCE_INLINE_ bool operator==(const StringName &p_name) const {
111
// The real magic of all this mess happens here.
112
// This is why path comparisons are very fast.
113
return _data == p_name._data;
114
}
115
_FORCE_INLINE_ bool operator!=(const StringName &p_name) const {
116
return _data != p_name._data;
117
}
118
_FORCE_INLINE_ uint32_t hash() const {
119
if (_data) {
120
return _data->hash;
121
} else {
122
return get_empty_hash();
123
}
124
}
125
_FORCE_INLINE_ const void *data_unique_pointer() const {
126
return (void *)_data;
127
}
128
129
_FORCE_INLINE_ operator String() const {
130
if (_data) {
131
return _data->name;
132
}
133
134
return String();
135
}
136
137
struct AlphCompare {
138
template <typename LT, typename RT>
139
_FORCE_INLINE_ bool operator()(const LT &l, const RT &r) const {
140
return compare(l, r);
141
}
142
_FORCE_INLINE_ static bool compare(const StringName &l, const StringName &r) {
143
return str_compare(l.get_data(), r.get_data()) < 0;
144
}
145
_FORCE_INLINE_ static bool compare(const String &l, const StringName &r) {
146
return str_compare(l.get_data(), r.get_data()) < 0;
147
}
148
_FORCE_INLINE_ static bool compare(const StringName &l, const String &r) {
149
return str_compare(l.get_data(), r.get_data()) < 0;
150
}
151
_FORCE_INLINE_ static bool compare(const String &l, const String &r) {
152
return str_compare(l.get_data(), r.get_data()) < 0;
153
}
154
};
155
156
StringName &operator=(const StringName &p_name);
157
StringName &operator=(StringName &&p_name) {
158
if (_data == p_name._data) {
159
return *this;
160
}
161
162
unref();
163
_data = p_name._data;
164
p_name._data = nullptr;
165
return *this;
166
}
167
StringName(const char *p_name, bool p_static = false);
168
StringName(const StringName &p_name);
169
StringName(StringName &&p_name) {
170
_data = p_name._data;
171
p_name._data = nullptr;
172
}
173
StringName(const String &p_name, bool p_static = false);
174
StringName() {}
175
176
#ifdef SIZE_EXTRA
177
_NO_INLINE_
178
#else
179
_FORCE_INLINE_
180
#endif
181
~StringName() {
182
if (likely(configured) && _data) { //only free if configured
183
unref();
184
}
185
}
186
187
#ifdef DEBUG_ENABLED
188
static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; }
189
#endif
190
};
191
192
// Zero-constructing StringName initializes _data to nullptr (and thus empty).
193
template <>
194
struct is_zero_constructible<StringName> : std::true_type {};
195
196
bool operator==(const String &p_name, const StringName &p_string_name);
197
bool operator!=(const String &p_name, const StringName &p_string_name);
198
bool operator==(const char *p_name, const StringName &p_string_name);
199
bool operator!=(const char *p_name, const StringName &p_string_name);
200
201
/*
202
* The SNAME macro is used to speed up StringName creation, as it allows caching it after the first usage in a very efficient way.
203
* It should NOT be used everywhere, but instead in places where high performance is required and the creation of a StringName
204
* can be costly. Places where it should be used are:
205
* - Control::get_theme_*(<name> and Window::get_theme_*(<name> functions.
206
* - emit_signal(<name>,..) function
207
* - call_deferred(<name>,..) function
208
* - Comparisons to a StringName in overridden _set and _get methods.
209
*
210
* 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.
211
*/
212
213
#define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = StringName(m_arg, true); return sname; })()
214
215