ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
Revision: 1.6
Committed: Sun Sep 3 07:57:56 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.5: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

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