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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines