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.16 by root, Mon May 28 21:15:56 2007 UTC vs.
Revision 1.24 by root, Wed Dec 31 17:35:37 2008 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Crossfire TRT is free software; you can redistribute it and/or modify it 6 * Deliantra is free software: you can redistribute it and/or modify
7 * under the terms of the GNU General Public License as published by the Free 7 * it under the terms of the GNU General Public License as published by
8 * Software Foundation; either version 2 of the License, or (at your option) 8 * the Free Software Foundation, either version 3 of the License, or
9 * any later version. 9 * (at your option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, but 11 * This program is distributed in the hope that it will be useful,
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * 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 along 16 * You should have received a copy of the GNU General Public License
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * 18 *
20 * The authors can be reached via e-mail to <crossfire@schmorp.de> 19 * The authors can be reached via e-mail to <support@deliantra.net>
21 */ 20 */
22 21
23#ifndef SHSTR_H__ 22#ifndef SHSTR_H__
24#define SHSTR_H__ 23#define SHSTR_H__
25 24
25#include <cstring>
26#include <sstream> 26#include <sstream>
27 27
28#include "util.h" 28#include "util.h"
29 29
30extern size_t shstr_alloc;
31
30extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); 32extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
31 33
34// this class is a non-refcounted shared string
35// 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
37// 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.
32struct shstr 39struct shstr_tmp
33{ 40{
34 static const char *null; 41 static const char *null;
35 42
36 const char *s; 43 const char *s;
37 44
38 static int &refcnt (const char *s)
39 {
40 return *((int *)s - 1);
41 }
42
43 static int &length (const char *s) 45 static unsigned int &length (const char *s)
44 { 46 {
45 return *((int *)s - 2); 47 return *((unsigned int *)s - 2);
46 } 48 }
47 49
48 int &refcnt () const 50 int length () const
51 {
52 return length (s);
53 }
54
55 // returns whether this shared string begins with the given prefix,
56 // used mainly for searches when users give only the start of a name.
57 bool starts_with (const char *prefix) const
58 {
59 int plen = strlen (prefix);
60
61 return length () >= plen && !strncasecmp (s, prefix, plen);
62 }
63
64 bool contains (const char *substring) const
65 {
66 return strstr (s, substring);
67 }
68
69 shstr_tmp ()
70 : s (null)
71 {
72 }
73
74 shstr_tmp (const shstr_tmp &sh)
75 : s (sh.s)
76 {
77 }
78
79 shstr_tmp operator =(const shstr_tmp &sh)
80 {
81 s = sh.s;
82
83 return *this;
84 }
85
86 // this is used for informational messages and the like
87 const char *operator &() const { return s; }
88
89 operator const char *() const { return s == null ? 0 : s; }
90};
91
92inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b)
93{
94 return a.s == b.s;
95}
96
97inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b)
98{
99 return a.s != b.s;
100}
101
102inline int strlen (const shstr_tmp &sh)
103{
104 return sh.length ();
105}
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
110static std::ostream &operator <<(std::ostream &o, shstr_tmp sh)
111{
112 o.write (sh.s, sh.length ());
113
114 return o;
115}
116
117struct shstr : shstr_tmp
118{
119 static unsigned int &refcnt (const char *s)
120 {
121 return *((unsigned int *)s - 1);
122 }
123
124 unsigned int &refcnt () const
49 { 125 {
50 return refcnt (s); 126 return refcnt (s);
51 } 127 }
52 128
53 int length () const 129 shstr ()
54 { 130 {
55 return length (s);
56 }
57
58 // returns wether this shared string begins with the given prefix,
59 // used mainly for searched when users give only the start of a name.
60 bool begins_with (const char *prefix) const
61 {
62 int plen = strlen (prefix);
63 return !strncasecmp (s, prefix, plen) && length () >= plen;
64 } 131 }
65 132
66 static const char *find (const char *s); 133 static const char *find (const char *s);
67 static const char *intern (const char *s); 134 static const char *intern (const char *s);
68 135
69 static void gc (); // garbage collect a few strings 136 static void gc (); // garbage collect a few strings
70 137
71 // this is used for informational messages and the like
72 const char *operator &() const { return s; }
73
74 const char &operator [](int i) const { return s[i]; }
75 operator const char *() const { return s == null ? 0 : s; }
76
77 shstr ()
78 : s (null)
79 {
80 }
81
82 shstr (const shstr &sh) 138 shstr (const shstr &sh)
83 : s (sh.s) 139 : shstr_tmp (sh)
84 { 140 {
85 ++refcnt (); 141 ++refcnt ();
86 } 142 }
87 143
144 shstr (const shstr_tmp &sh)
145 : shstr_tmp (sh)
146 {
147 ++refcnt ();
148 }
149
88 explicit shstr (const char *s) 150 explicit shstr (const char *str)
89 : s (intern (s))
90 { 151 {
152 s = is_constant (str) && !str ? null : intern (str);
91 } 153 }
92 154
93 ~shstr () 155 ~shstr ()
94 { 156 {
95 --refcnt (); 157 --refcnt ();
96 } 158 }
97 159
160 using shstr_tmp::operator &;
161 using shstr_tmp::operator const char *;
162
163 // (note: not the copy constructor)
98 const shstr &operator =(const shstr &sh) 164 shstr &operator =(const shstr_tmp &sh)
99 { 165 {
100 --refcnt (); 166 --refcnt ();
101 s = sh.s; 167 s = sh.s;
102 ++refcnt (); 168 ++refcnt ();
103 169
104 return *this; 170 return *this;
105 } 171 }
106 172
173 // here it comes
174 shstr &operator =(const shstr &sh)
175 {
176 return (*this) = (shstr_tmp)sh;
177 }
178
179 // shstr_tmp doesn't have this one
107 const shstr &operator =(const char *str) 180 shstr &operator =(const char *str)
108 { 181 {
109 --refcnt (); 182 --refcnt ();
110 183 s = is_constant (str) && !str ? null : intern (str);
111 // this optimises the important case of str == constant 0
112 if (is_constant (str))
113 s = str ? intern (str) : null;
114 else
115 s = intern (str);
116 184
117 return *this; 185 return *this;
118 } 186 }
119
120 bool operator ==(const shstr &b)
121 {
122 return s == b.s;
123 }
124
125 bool operator !=(const shstr &b)
126 {
127 return !(*this == b);
128 }
129}; 187};
130 188
131inline int strlen (const shstr &sh)
132{
133 return sh.length ();
134}
135
136inline int strcmp (const shstr &a, const shstr &b)
137{
138 // TODO: use this to find all the occurences of people using strcmp
139 // all uses should be bogus, as we should be never interested in
140 // comparing shstr's alphabetically
141#if 0
142 extern void do_not_use_strcmp_to_compare_shstr_values ();
143 do_not_use_strcmp_to_compare_shstr_values ();
144#endif
145 return a != b;
146}
147
148static std::ostream &operator <<(std::ostream &o, const shstr &sh)
149{
150 o.write (sh.s, sh.length ());
151 return o;
152}
153
154// only good for mass comparisons to shstr objects 189// only good for mass comparisons to shstr objects, or very
190// temporary passing, basically a non-refcounted shstr
155struct shstr_cmp 191struct shstr_cmp
156{ 192{
157 const char *s; 193 const char *s;
158 194
159 explicit shstr_cmp (const char *s) 195 explicit shstr_cmp (const char *str)
160 : s (shstr::find (s)) 196 : s (shstr::find (str))
161 { 197 {
162 } 198 }
163 199
164 shstr_cmp (const shstr_cmp &sh) 200 shstr_cmp (const shstr_cmp &sh)
165 : s (sh.s) 201 : s (sh.s)
166 { 202 {
167 } 203 }
168 204
205 shstr_cmp (const shstr &sh)
206 : s (sh.s)
207 {
208 }
209
210 // this is used for informational messages and the like
211 const char *operator &() const { return s; }
212
169 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; } 213 shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; }
170 operator const char *() const { return s; } 214 operator const char *() const { return s; }
171}; 215};
172 216
173inline bool operator ==(const shstr_cmp &a, const shstr &b) 217inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b)
174{ 218{
175 return a.s == b.s; 219 return a.s == b.s;
176} 220}
177 221
178inline bool operator ==(const shstr &a, const shstr_cmp &b) 222inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b)
179{ 223{
180 return b == a; 224 return a.s == b.s;
181} 225}
182 226
183extern const shstr undead_name; /* Used in hit_player() in main.c */ 227extern const shstr shstr_null;
228
229#define def(str) extern const shstr shstr_ ## str;
230# include "shstrinc.h"
231#undef def
184 232
185#endif 233#endif
186 234

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines