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.6 by root, Sun Sep 3 07:57:56 2006 UTC vs.
Revision 1.11 by root, Thu Sep 14 21:16:12 2006 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 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) const { 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 (const 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
104// only good for mass comparisons to shstr objects
105struct shstr_cmp
106{
107 const char *s;
108
109 explicit shstr_cmp (const char *s)
110 : s (shstr::find (s))
111 {
112 }
113
114 shstr_cmp (const shstr_cmp &sh)
115 : s (sh.s)
116 {
117 }
118
119 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
120 operator const char *() const { return s; }
121};
122
76inline bool operator ==(const shstr &a, const shstr &b) 123inline bool operator ==(const shstr_cmp &a, const shstr &b)
77{ 124{
78 return a.s == b.s; 125 return a.s == b.s;
79} 126}
80 127
81inline bool operator !=(const shstr &a, const shstr &b) 128inline bool operator ==(const shstr &a, const shstr_cmp &b)
82{ 129{
83 return !(a == b); 130 return b == a;
84} 131}
132
133extern const shstr undead_name; /* Used in hit_player() in main.c */
85 134
86#endif 135#endif
87 136

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines