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.12 by root, Mon Feb 5 02:07:40 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines