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.34 by root, Tue Nov 3 23:44:21 2009 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Deliantra is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify it under
7 * it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
8 * the Free Software Foundation, either version 3 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
9 * (at your option) any later version. 9 * option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
18 * 19 *
19 * The authors can be reached via e-mail to <support@deliantra.net> 20 * The authors can be reached via e-mail to <support@deliantra.net>
20 */ 21 */
21 22
22#ifndef SHSTR_H__ 23#ifndef SHSTR_H__
23#define SHSTR_H__ 24#define SHSTR_H__
24 25
25#include <cstring> 26#include <cstring>
26#include <sstream> 27#include <sstream>
27 28
28#include "util.h" 29#include "traits.h"
29 30
30extern size_t shstr_alloc; 31extern size_t shstr_alloc;
31 32
32extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); 33extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
34
35template<int size>
36struct shstr_vec
37{
38 uint32_t len;
39 uint32_t refcnt;
40 char string[size];
41};
33 42
34// this class is a non-refcounted shared string 43// this class is a non-refcounted shared string
35// it cannot be used to create or store shared strings, but 44// 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 45// 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. 46// 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. 47// they are only valid as temporary values within the same server tick.
39struct shstr_tmp 48struct shstr_tmp
40{ 49{
41 static const char *null; 50 static shstr_vec<sizeof ("(null)")> nullvec;
51 static const char *null () { return nullvec.string; } // this is the null pointer value
42 52
43 const char *s; 53 const char *s;
44 54
45 static unsigned int &length (const char *s) 55 static unsigned int &length (const char *s)
46 { 56 {
59 int plen = strlen (prefix); 69 int plen = strlen (prefix);
60 70
61 return length () >= plen && !strncasecmp (s, prefix, plen); 71 return length () >= plen && !strncasecmp (s, prefix, plen);
62 } 72 }
63 73
74 // returns true if the substring is contained in the shstr
75 // if the shstr is 0, then this always returns false.
76 // the shstr is (theoretically) treated as a comma/colon/space etc. separated list.
64 bool contains (const char *substring) const 77 bool contains (const char *substring) const
65 { 78 {
66 return strstr (s, substring); 79 return s != null () && strstr (s, substring);
80 }
81
82 //TODO: case sensitive should be eradicated
83 bool eq_nc (const char *otherstring) const
84 {
85 return !strcasecmp (s, otherstring);
67 } 86 }
68 87
69 shstr_tmp () 88 shstr_tmp ()
70 : s (null) 89 : s (null ())
71 { 90 {
72 } 91 }
73 92
74 shstr_tmp (const shstr_tmp &sh) 93 shstr_tmp (const shstr_tmp &sh)
75 : s (sh.s) 94 : s (sh.s)
84 } 103 }
85 104
86 // this is used for informational messages and the like 105 // this is used for informational messages and the like
87 const char *operator &() const { return s; } 106 const char *operator &() const { return s; }
88 107
89 operator const char *() const { return s == null ? 0 : s; } 108 operator const char *() const { return s == null () ? 0 : s; }
109
110protected:
111 // dummy is there so it isn't used as type converter accidentally
112 shstr_tmp (int dummy, const char *s)
113 : s(s)
114 {
115 }
90}; 116};
91 117
92inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) 118inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b)
93{ 119{
94 return a.s == b.s; 120 return a.s == b.s;
97inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) 123inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b)
98{ 124{
99 return a.s != b.s; 125 return a.s != b.s;
100} 126}
101 127
102inline int strlen (const shstr_tmp &sh) 128inline int strlen (shstr_tmp sh)
103{ 129{
104 return sh.length (); 130 return sh.length ();
105} 131}
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 132
110static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) 133static std::ostream &operator <<(std::ostream &o, shstr_tmp sh)
111{ 134{
112 o.write (sh.s, sh.length ()); 135 o.write (sh.s, sh.length ());
113 136
146 { 169 {
147 ++refcnt (); 170 ++refcnt ();
148 } 171 }
149 172
150 explicit shstr (const char *str) 173 explicit shstr (const char *str)
151 {
152 s = is_constant (str) && !str ? null : intern (str); 174 : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str))
175 {
153 } 176 }
154 177
155 ~shstr () 178 ~shstr ()
156 { 179 {
157 --refcnt (); 180 --refcnt ();
178 201
179 // shstr_tmp doesn't have this one 202 // shstr_tmp doesn't have this one
180 shstr &operator =(const char *str) 203 shstr &operator =(const char *str)
181 { 204 {
182 --refcnt (); 205 --refcnt ();
183 s = is_constant (str) && !str ? null : intern (str); 206 s = is_constant (str) && !str ? null () : intern (str);
184 207
185 return *this; 208 return *this;
186 } 209 }
187}; 210};
188 211
190// temporary passing, basically a non-refcounted shstr 213// temporary passing, basically a non-refcounted shstr
191struct shstr_cmp 214struct shstr_cmp
192{ 215{
193 const char *s; 216 const char *s;
194 217
218 // initialies to the non-matching string (as opposed to the null string)
219 shstr_cmp ()
220 {
221 s = 0;
222 }
223
195 explicit shstr_cmp (const char *str) 224 shstr_cmp (const char *str)
196 : s (shstr::find (str)) 225 : s (shstr::find (str))
197 { 226 {
198 } 227 }
199 228
200 shstr_cmp (const shstr_cmp &sh) 229 shstr_cmp (shstr_tmp sh)
201 : s (sh.s)
202 {
203 }
204
205 shstr_cmp (const shstr &sh)
206 : s (sh.s) 230 : s (sh.s)
207 { 231 {
208 } 232 }
209 233
210 // this is used for informational messages and the like 234 // this is used for informational messages and the like
211 const char *operator &() const { return s; } 235 const char *operator &() const { return s; }
212 236
213 shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; }
214 operator const char *() const { return s; } 237 operator const char *() const { return s; }
215}; 238};
216 239
217inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) 240inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b)
218{ 241{
222inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) 245inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b)
223{ 246{
224 return a.s == b.s; 247 return a.s == b.s;
225} 248}
226 249
227extern const shstr shstr_null;
228
229#define def(str) extern const shstr shstr_ ## str; 250#define def2(id,str) extern const shstr id;
251#define def(id) def2(shstr_ ## id, # id)
230# include "shstrinc.h" 252# include "shstrinc.h"
231#undef def 253#undef def
254#undef def2
255
256// undefined external reference to catch people using str* functions when they shouldn't
257//template<class any> void strcmp (const shstr_tmp &a, any b);
258template<class any> void strstr (const shstr_tmp &a, any b);
232 259
233#endif 260#endif
234 261

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines