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.7 by root, Sun Sep 3 09:00:09 2006 UTC vs.
Revision 1.10 by root, Tue Sep 12 18:15:34 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 static int &refcnt (const char *s) 14 static int &refcnt (const char *s)
11 { 15 {
12 return *((int *)s - 1); 16 return *((int *)s - 1);
22 return refcnt (s); 26 return refcnt (s);
23 } 27 }
24 28
25 int length () const 29 int length () const
26 { 30 {
27 return s ? length (s) : 0; 31 return length (s);
28 } 32 }
29 33
30 static const char *find (const char *s); 34 static const char *find (const char *s);
31 static const char *intern (const char *s); 35 static const char *intern (const char *s);
32 36
33 static void gc (); // garbage collect a few strings 37 static void gc (); // garbage collect a few strings
34 38
35 // this is used for informational messages and the like 39 // this is used for informational messages and the like
36 const char *operator &() const { return s ? s : "<nil>"; } 40 const char *operator &() const { return s; }
37 41
38 const char &operator [](int i) const { return s[i]; } 42 const char &operator [](int i) const { return s[i]; }
39 operator const char *() const { return s; } 43 operator const char *() const { return s == null ? 0 : s; }
40 44
41 shstr () 45 shstr ()
42 : s (0) 46 : s (null)
43 { 47 {
44 } 48 }
45 49
46 shstr (const shstr &sh) 50 shstr (const shstr &sh)
47 : s (sh.s) 51 : s (sh.s)
48 { 52 {
49 if (s) ++refcnt (); 53 ++refcnt ();
50 } 54 }
51 55
52 explicit shstr (const char *s) 56 explicit shstr (const char *s)
53 : s (intern (s)) 57 : s (intern (s))
54 { 58 {
55 } 59 }
56 60
57 ~shstr () 61 ~shstr ()
58 { 62 {
59 if (s) --refcnt (); 63 --refcnt ();
60 } 64 }
61 65
62 const shstr &operator =(const shstr &sh) 66 const shstr &operator =(const shstr &sh)
63 { 67 {
64 if (s) --refcnt (); 68 --refcnt ();
65 s = sh.s; 69 s = sh.s;
66 if (s) ++refcnt (); 70 ++refcnt ();
67 71
68 return *this; 72 return *this;
69 } 73 }
70 74
71 const shstr &operator =(const char *str) 75 const shstr &operator =(const char *str)
72 { 76 {
73 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
74 s = intern (str); 83 s = intern (str);
75 84
76 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);
77 } 96 }
78}; 97};
79 98
80inline int strlen (const shstr &sh) 99inline int strlen (const shstr &sh)
81{ 100{
82 return sh.length (); 101 return sh.length ();
83} 102}
84 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 operator const char *() const { return s; }
115};
116
85inline bool operator ==(const shstr &a, const shstr &b) 117inline bool operator ==(const shstr_cmp &a, const shstr &b)
86{ 118{
87 return a.s == b.s; 119 return a.s == b.s;
88} 120}
89 121
90inline bool operator !=(const shstr &a, const shstr &b) 122inline bool operator ==(const shstr &a, const shstr_cmp &b)
91{ 123{
92 return !(a == b); 124 return b == a;
93} 125}
126
127extern const shstr undead_name; /* Used in hit_player() in main.c */
94 128
95#endif 129#endif
96 130

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines