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.5 by root, Sun Sep 3 00:18:41 2006 UTC vs.
Revision 1.14 by root, Mon Apr 23 16:07:00 2007 UTC

1#ifndef SHSTR_H__ 1#ifndef SHSTR_H__
2#define SHSTR_H__ 2#define SHSTR_H__
3 3
4#include <sstream>
5
6#include "util.h"
7
4extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); 8extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
5 9
6struct shstr 10struct shstr
7{ 11{
12 static const char *null;
13
8 const char *s; 14 const char *s;
9 15
10 int &refcnt () 16 static int &refcnt (const char *s)
11 { 17 {
12 return *((int *)s - 1); 18 return *((int *)s - 1);
19 }
20
21 static int &length (const char *s)
22 {
23 return *((int *)s - 2);
24 }
25
26 int &refcnt () const
27 {
28 return refcnt (s);
29 }
30
31 int length () const
32 {
33 return length (s);
13 } 34 }
14 35
15 static const char *find (const char *s); 36 static const char *find (const char *s);
16 static const char *intern (const char *s); 37 static const char *intern (const char *s);
17 38
18 static void gc (); // garbage collect a few strings 39 static void gc (); // garbage collect a few strings
19 40
20 // this is used for informational messages and the like I/O 41 // this is used for informational messages and the like
21 const char *operator &() const { return s ? s : "<nil>"; } 42 const char *operator &() const { return s; }
22 43
23 const char &operator [](int i) { return s[i]; } 44 const char &operator [](int i) const { return s[i]; }
24 operator const char *() const { return s; } 45 operator const char *() const { return s == null ? 0 : s; }
25
26 int length () const
27 {
28 return s ? *((int *)s - 2) : 0;
29 }
30 46
31 shstr () 47 shstr ()
32 : s (0) 48 : s (null)
33 { 49 {
34 } 50 }
35 51
36 shstr (shstr &sh) 52 shstr (const shstr &sh)
37 : s (sh.s) 53 : s (sh.s)
38 { 54 {
39 if (s) ++refcnt (); 55 ++refcnt ();
40 } 56 }
41 57
42 explicit shstr (const char *s) 58 explicit shstr (const char *s)
43 : s (intern (s)) 59 : s (intern (s))
44 { 60 {
45 if (s) ++refcnt ();
46 } 61 }
47 62
48 ~shstr () 63 ~shstr ()
49 { 64 {
50 if (s) --refcnt (); 65 --refcnt ();
51 } 66 }
52 67
53 const shstr &operator =(const shstr &sh) 68 const shstr &operator =(const shstr &sh)
54 { 69 {
55 if (s) --refcnt (); 70 --refcnt ();
56 s = sh.s; 71 s = sh.s;
57 if (s) ++refcnt (); 72 ++refcnt ();
58 73
59 return *this; 74 return *this;
60 } 75 }
61 76
62 const shstr &operator =(const char *str) 77 const shstr &operator =(const char *str)
63 { 78 {
64 if (s) --refcnt (); 79 --refcnt ();
80
81 // this optimises the important case of str == constant 0
82 if (is_constant (str))
83 s = str ? intern (str) : null;
84 else
65 s = intern (str); 85 s = intern (str);
66 86
67 return *this; 87 return *this;
88 }
89
90 bool operator ==(const shstr &b)
91 {
92 return s == b.s;
93 }
94
95 bool operator !=(const shstr &b)
96 {
97 return !(*this == b);
68 } 98 }
69}; 99};
70 100
71inline int strlen (const shstr &sh) 101inline int strlen (const shstr &sh)
72{ 102{
73 return sh.length (); 103 return sh.length ();
74} 104}
75 105
106inline int strcmp (const shstr &a, const shstr &b)
107{
108 // TODO: use this to find all the occurences of people using strcmp
109 // all uses should be bogus, as we should be never interested in
110 // comparing shstr's alphabetically
111#if 0
112 extern void do_not_use_strcmp_to_compare_shstr_values ();
113 do_not_use_strcmp_to_compare_shstr_values ();
114#endif
115 return a != b;
116}
117
118static std::ostream &operator <<(std::ostream &o, const shstr &sh)
119{
120 o.write (sh.s, sh.length ());
121 return o;
122}
123
124// only good for mass comparisons to shstr objects
125struct shstr_cmp
126{
127 const char *s;
128
129 explicit shstr_cmp (const char *s)
130 : s (shstr::find (s))
131 {
132 }
133
134 shstr_cmp (const shstr_cmp &sh)
135 : s (sh.s)
136 {
137 }
138
139 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
140 operator const char *() const { return s; }
141};
142
76inline bool operator ==(const shstr &a, const shstr &b) 143inline bool operator ==(const shstr_cmp &a, const shstr &b)
77{ 144{
78 return a.s == b.s; 145 return a.s == b.s;
79} 146}
80 147
81inline bool operator !=(const shstr &a, const shstr &b) 148inline bool operator ==(const shstr &a, const shstr_cmp &b)
82{ 149{
83 return !(a == b); 150 return b == a;
84} 151}
152
153extern const shstr undead_name; /* Used in hit_player() in main.c */
85 154
86#endif 155#endif
87 156

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines