… | |
… | |
23 | #define SHSTR_H__ |
23 | #define SHSTR_H__ |
24 | |
24 | |
25 | #include <cstring> |
25 | #include <cstring> |
26 | #include <sstream> |
26 | #include <sstream> |
27 | |
27 | |
28 | #include "util.h" |
28 | #include "traits.h" |
29 | |
29 | |
30 | extern size_t shstr_alloc; |
30 | extern size_t shstr_alloc; |
31 | |
31 | |
32 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
32 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
|
|
33 | |
|
|
34 | template<int size> |
|
|
35 | struct shstr_vec |
|
|
36 | { |
|
|
37 | uint32_t len; |
|
|
38 | uint32_t refcnt; |
|
|
39 | char string[size]; |
|
|
40 | }; |
33 | |
41 | |
34 | // this class is a non-refcounted shared string |
42 | // this class is a non-refcounted shared string |
35 | // it cannot be used to create or store shared strings, but |
43 | // it cannot be used to create or store shared strings, but |
36 | // it can be used to apss shared strings around, i.e. as function arguments |
44 | // it can be used to apss shared strings around, i.e. as function arguments |
37 | // or return values. their lifetime must not span a gc () call, i.e. |
45 | // or return values. their lifetime must not span a gc () call, i.e. |
38 | // they are only valid as temporary values within the same server tick. |
46 | // they are only valid as temporary values within the same server tick. |
39 | struct shstr_tmp |
47 | struct shstr_tmp |
40 | { |
48 | { |
41 | static const char *null; |
49 | static shstr_vec<sizeof ("(null)")> nullvec; |
|
|
50 | static const char *null () { return nullvec.string; } // this is the null pointer value |
42 | |
51 | |
43 | const char *s; |
52 | const char *s; |
44 | |
53 | |
45 | static unsigned int &length (const char *s) |
54 | static unsigned int &length (const char *s) |
46 | { |
55 | { |
… | |
… | |
65 | { |
74 | { |
66 | return strstr (s, substring); |
75 | return strstr (s, substring); |
67 | } |
76 | } |
68 | |
77 | |
69 | shstr_tmp () |
78 | shstr_tmp () |
70 | : s (null) |
79 | : s (null ()) |
71 | { |
80 | { |
72 | } |
81 | } |
73 | |
82 | |
74 | shstr_tmp (const shstr_tmp &sh) |
83 | shstr_tmp (const shstr_tmp &sh) |
75 | : s (sh.s) |
84 | : s (sh.s) |
… | |
… | |
84 | } |
93 | } |
85 | |
94 | |
86 | // this is used for informational messages and the like |
95 | // this is used for informational messages and the like |
87 | const char *operator &() const { return s; } |
96 | const char *operator &() const { return s; } |
88 | |
97 | |
89 | operator const char *() const { return s == null ? 0 : s; } |
98 | operator const char *() const { return s == null () ? 0 : s; } |
90 | }; |
99 | }; |
91 | |
100 | |
92 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
101 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
93 | { |
102 | { |
94 | return a.s == b.s; |
103 | return a.s == b.s; |
… | |
… | |
147 | ++refcnt (); |
156 | ++refcnt (); |
148 | } |
157 | } |
149 | |
158 | |
150 | explicit shstr (const char *str) |
159 | explicit shstr (const char *str) |
151 | { |
160 | { |
152 | s = is_constant (str) && !str ? null : intern (str); |
161 | s = is_constant (str) && !str ? null () : intern (str); |
153 | } |
162 | } |
154 | |
163 | |
155 | ~shstr () |
164 | ~shstr () |
156 | { |
165 | { |
157 | --refcnt (); |
166 | --refcnt (); |
… | |
… | |
178 | |
187 | |
179 | // shstr_tmp doesn't have this one |
188 | // shstr_tmp doesn't have this one |
180 | shstr &operator =(const char *str) |
189 | shstr &operator =(const char *str) |
181 | { |
190 | { |
182 | --refcnt (); |
191 | --refcnt (); |
183 | s = is_constant (str) && !str ? null : intern (str); |
192 | s = is_constant (str) && !str ? null () : intern (str); |
184 | |
193 | |
185 | return *this; |
194 | return *this; |
186 | } |
195 | } |
187 | }; |
196 | }; |
188 | |
197 | |
… | |
… | |
195 | explicit shstr_cmp (const char *str) |
204 | explicit shstr_cmp (const char *str) |
196 | : s (shstr::find (str)) |
205 | : s (shstr::find (str)) |
197 | { |
206 | { |
198 | } |
207 | } |
199 | |
208 | |
200 | shstr_cmp (const shstr_cmp &sh) |
209 | shstr_cmp (shstr_tmp sh) |
201 | : s (sh.s) |
|
|
202 | { |
|
|
203 | } |
|
|
204 | |
|
|
205 | shstr_cmp (const shstr &sh) |
|
|
206 | : s (sh.s) |
210 | : s (sh.s) |
207 | { |
211 | { |
208 | } |
212 | } |
209 | |
213 | |
210 | // this is used for informational messages and the like |
214 | // this is used for informational messages and the like |
211 | const char *operator &() const { return s; } |
215 | const char *operator &() const { return s; } |
212 | |
216 | |
213 | shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
|
|
214 | operator const char *() const { return s; } |
217 | operator const char *() const { return s; } |
215 | }; |
218 | }; |
216 | |
219 | |
217 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
220 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
218 | { |
221 | { |
… | |
… | |
221 | |
224 | |
222 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
225 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
223 | { |
226 | { |
224 | return a.s == b.s; |
227 | return a.s == b.s; |
225 | } |
228 | } |
226 | |
|
|
227 | extern const shstr shstr_null; |
|
|
228 | |
229 | |
229 | #define def(str) extern const shstr shstr_ ## str; |
230 | #define def(str) extern const shstr shstr_ ## str; |
230 | # include "shstrinc.h" |
231 | # include "shstrinc.h" |
231 | #undef def |
232 | #undef def |
232 | |
233 | |