ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
Revision: 1.14
Committed: Mon Apr 23 16:07:00 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +1 -1 lines
Log Message:
add ostream << operator to for shstr

File Contents

# Content
1 #ifndef SHSTR_H__
2 #define SHSTR_H__
3
4 #include <sstream>
5
6 #include "util.h"
7
8 extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
9
10 struct shstr
11 {
12 static const char *null;
13
14 const char *s;
15
16 static int &refcnt (const char *s)
17 {
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);
34 }
35
36 static const char *find (const char *s);
37 static const char *intern (const char *s);
38
39 static void gc (); // garbage collect a few strings
40
41 // this is used for informational messages and the like
42 const char *operator &() const { return s; }
43
44 const char &operator [](int i) const { return s[i]; }
45 operator const char *() const { return s == null ? 0 : s; }
46
47 shstr ()
48 : s (null)
49 {
50 }
51
52 shstr (const shstr &sh)
53 : s (sh.s)
54 {
55 ++refcnt ();
56 }
57
58 explicit shstr (const char *s)
59 : s (intern (s))
60 {
61 }
62
63 ~shstr ()
64 {
65 --refcnt ();
66 }
67
68 const shstr &operator =(const shstr &sh)
69 {
70 --refcnt ();
71 s = sh.s;
72 ++refcnt ();
73
74 return *this;
75 }
76
77 const shstr &operator =(const char *str)
78 {
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
85 s = intern (str);
86
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);
98 }
99 };
100
101 inline int strlen (const shstr &sh)
102 {
103 return sh.length ();
104 }
105
106 inline 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
118 static 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
125 struct 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
143 inline bool operator ==(const shstr_cmp &a, const shstr &b)
144 {
145 return a.s == b.s;
146 }
147
148 inline bool operator ==(const shstr &a, const shstr_cmp &b)
149 {
150 return b == a;
151 }
152
153 extern const shstr undead_name; /* Used in hit_player() in main.c */
154
155 #endif
156