ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
Revision: 1.7
Committed: Sun Sep 3 09:00:09 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +16 -7 lines
Log Message:
everything seems to work so far

File Contents

# Content
1 #ifndef SHSTR_H__
2 #define SHSTR_H__
3
4 extern int buf_overflow(const char *buf1, const char *buf2, int bufsize);
5
6 struct shstr
7 {
8 const char *s;
9
10 static int &refcnt (const char *s)
11 {
12 return *((int *)s - 1);
13 }
14
15 static int &length (const char *s)
16 {
17 return *((int *)s - 2);
18 }
19
20 int &refcnt () const
21 {
22 return refcnt (s);
23 }
24
25 int length () const
26 {
27 return s ? length (s) : 0;
28 }
29
30 static const char *find (const char *s);
31 static const char *intern (const char *s);
32
33 static void gc (); // garbage collect a few strings
34
35 // this is used for informational messages and the like
36 const char *operator &() const { return s ? s : "<nil>"; }
37
38 const char &operator [](int i) const { return s[i]; }
39 operator const char *() const { return s; }
40
41 shstr ()
42 : s (0)
43 {
44 }
45
46 shstr (const shstr &sh)
47 : s (sh.s)
48 {
49 if (s) ++refcnt ();
50 }
51
52 explicit shstr (const char *s)
53 : s (intern (s))
54 {
55 }
56
57 ~shstr ()
58 {
59 if (s) --refcnt ();
60 }
61
62 const shstr &operator =(const shstr &sh)
63 {
64 if (s) --refcnt ();
65 s = sh.s;
66 if (s) ++refcnt ();
67
68 return *this;
69 }
70
71 const shstr &operator =(const char *str)
72 {
73 if (s) --refcnt ();
74 s = intern (str);
75
76 return *this;
77 }
78 };
79
80 inline int strlen (const shstr &sh)
81 {
82 return sh.length ();
83 }
84
85 inline bool operator ==(const shstr &a, const shstr &b)
86 {
87 return a.s == b.s;
88 }
89
90 inline bool operator !=(const shstr &a, const shstr &b)
91 {
92 return !(a == b);
93 }
94
95 #endif
96