ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
(Generate patch)

Comparing deliantra/server/include/shstr.h (file contents):
Revision 1.24 by root, Wed Dec 31 17:35:37 2008 UTC vs.
Revision 1.27 by root, Wed Dec 31 18:29:44 2008 UTC

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
30extern size_t shstr_alloc; 30extern size_t shstr_alloc;
31 31
32extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); 32extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
33
34template<int size>
35struct 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.
39struct shstr_tmp 47struct 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; }
99
100protected:
101 // dummy is there so it isn't used as type converter accidentally
102 shstr_tmp (int dummy, const char *s)
103 : s(s)
104 {
105 }
90}; 106};
91 107
92inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) 108inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b)
93{ 109{
94 return a.s == b.s; 110 return a.s == b.s;
146 { 162 {
147 ++refcnt (); 163 ++refcnt ();
148 } 164 }
149 165
150 explicit shstr (const char *str) 166 explicit shstr (const char *str)
151 {
152 s = is_constant (str) && !str ? null : intern (str); 167 : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str))
168 {
153 } 169 }
154 170
155 ~shstr () 171 ~shstr ()
156 { 172 {
157 --refcnt (); 173 --refcnt ();
178 194
179 // shstr_tmp doesn't have this one 195 // shstr_tmp doesn't have this one
180 shstr &operator =(const char *str) 196 shstr &operator =(const char *str)
181 { 197 {
182 --refcnt (); 198 --refcnt ();
183 s = is_constant (str) && !str ? null : intern (str); 199 s = is_constant (str) && !str ? null () : intern (str);
184 200
185 return *this; 201 return *this;
186 } 202 }
187}; 203};
188 204
195 explicit shstr_cmp (const char *str) 211 explicit shstr_cmp (const char *str)
196 : s (shstr::find (str)) 212 : s (shstr::find (str))
197 { 213 {
198 } 214 }
199 215
200 shstr_cmp (const shstr_cmp &sh) 216 shstr_cmp (shstr_tmp sh)
201 : s (sh.s)
202 {
203 }
204
205 shstr_cmp (const shstr &sh)
206 : s (sh.s) 217 : s (sh.s)
207 { 218 {
208 } 219 }
209 220
210 // this is used for informational messages and the like 221 // this is used for informational messages and the like
211 const char *operator &() const { return s; } 222 const char *operator &() const { return s; }
212 223
213 shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; }
214 operator const char *() const { return s; } 224 operator const char *() const { return s; }
215}; 225};
216 226
217inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) 227inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b)
218{ 228{
221 231
222inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) 232inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b)
223{ 233{
224 return a.s == b.s; 234 return a.s == b.s;
225} 235}
226
227extern const shstr shstr_null;
228 236
229#define def(str) extern const shstr shstr_ ## str; 237#define def(str) extern const shstr shstr_ ## str;
230# include "shstrinc.h" 238# include "shstrinc.h"
231#undef def 239#undef def
232 240

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines