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.32 by root, Thu Jan 1 20:49:48 2009 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 {
59 int plen = strlen (prefix); 68 int plen = strlen (prefix);
60 69
61 return length () >= plen && !strncasecmp (s, prefix, plen); 70 return length () >= plen && !strncasecmp (s, prefix, plen);
62 } 71 }
63 72
73 // returns true if the substring is contained in the shstr
74 // if the shstr is 0, then this always returns false.
75 // the shstr is (theoretically) treated as a comma/colon/space etc. separated list.
64 bool contains (const char *substring) const 76 bool contains (const char *substring) const
65 { 77 {
66 return strstr (s, substring); 78 return s != null () && strstr (s, substring);
79 }
80
81 //TODO: case sensitive should be eradicated
82 bool eq_nc (const char *otherstring) const
83 {
84 return !strcasecmp (s, otherstring);
67 } 85 }
68 86
69 shstr_tmp () 87 shstr_tmp ()
70 : s (null) 88 : s (null ())
71 { 89 {
72 } 90 }
73 91
74 shstr_tmp (const shstr_tmp &sh) 92 shstr_tmp (const shstr_tmp &sh)
75 : s (sh.s) 93 : s (sh.s)
84 } 102 }
85 103
86 // this is used for informational messages and the like 104 // this is used for informational messages and the like
87 const char *operator &() const { return s; } 105 const char *operator &() const { return s; }
88 106
89 operator const char *() const { return s == null ? 0 : s; } 107 operator const char *() const { return s == null () ? 0 : s; }
108
109protected:
110 // dummy is there so it isn't used as type converter accidentally
111 shstr_tmp (int dummy, const char *s)
112 : s(s)
113 {
114 }
90}; 115};
91 116
92inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) 117inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b)
93{ 118{
94 return a.s == b.s; 119 return a.s == b.s;
97inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) 122inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b)
98{ 123{
99 return a.s != b.s; 124 return a.s != b.s;
100} 125}
101 126
102inline int strlen (const shstr_tmp &sh) 127inline int strlen (shstr_tmp sh)
103{ 128{
104 return sh.length (); 129 return sh.length ();
105} 130}
106
107// undefined external reference to catch people using strcmp when they shouldn't
108int strcmp (const shstr_tmp &a, const shstr_tmp &b);
109 131
110static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) 132static std::ostream &operator <<(std::ostream &o, shstr_tmp sh)
111{ 133{
112 o.write (sh.s, sh.length ()); 134 o.write (sh.s, sh.length ());
113 135
146 { 168 {
147 ++refcnt (); 169 ++refcnt ();
148 } 170 }
149 171
150 explicit shstr (const char *str) 172 explicit shstr (const char *str)
151 {
152 s = is_constant (str) && !str ? null : intern (str); 173 : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str))
174 {
153 } 175 }
154 176
155 ~shstr () 177 ~shstr ()
156 { 178 {
157 --refcnt (); 179 --refcnt ();
178 200
179 // shstr_tmp doesn't have this one 201 // shstr_tmp doesn't have this one
180 shstr &operator =(const char *str) 202 shstr &operator =(const char *str)
181 { 203 {
182 --refcnt (); 204 --refcnt ();
183 s = is_constant (str) && !str ? null : intern (str); 205 s = is_constant (str) && !str ? null () : intern (str);
184 206
185 return *this; 207 return *this;
186 } 208 }
187}; 209};
188 210
190// temporary passing, basically a non-refcounted shstr 212// temporary passing, basically a non-refcounted shstr
191struct shstr_cmp 213struct shstr_cmp
192{ 214{
193 const char *s; 215 const char *s;
194 216
217 // initialies to the non-matching string (as opposed to the null string)
218 shstr_cmp ()
219 {
220 s = 0;
221 }
222
195 explicit shstr_cmp (const char *str) 223 shstr_cmp (const char *str)
196 : s (shstr::find (str)) 224 : s (shstr::find (str))
197 { 225 {
198 } 226 }
199 227
200 shstr_cmp (const shstr_cmp &sh) 228 shstr_cmp (shstr_tmp sh)
201 : s (sh.s)
202 {
203 }
204
205 shstr_cmp (const shstr &sh)
206 : s (sh.s) 229 : s (sh.s)
207 { 230 {
208 } 231 }
209 232
210 // this is used for informational messages and the like 233 // this is used for informational messages and the like
211 const char *operator &() const { return s; } 234 const char *operator &() const { return s; }
212 235
213 shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; }
214 operator const char *() const { return s; } 236 operator const char *() const { return s; }
215}; 237};
216 238
217inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) 239inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b)
218{ 240{
222inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) 244inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b)
223{ 245{
224 return a.s == b.s; 246 return a.s == b.s;
225} 247}
226 248
227extern const shstr shstr_null;
228
229#define def(str) extern const shstr shstr_ ## str; 249#define def2(id,str) extern const shstr id;
250#define def(id) def2(shstr_ ## id, # id)
230# include "shstrinc.h" 251# include "shstrinc.h"
231#undef def 252#undef def
253#undef def2
254
255// undefined external reference to catch people using str* functions when they shouldn't
256//template<class any> void strcmp (const shstr_tmp &a, any b);
257template<class any> void strstr (const shstr_tmp &a, any b);
232 258
233#endif 259#endif
234 260

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines