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.8 by root, Sun Sep 3 23:33:01 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#if 0
80 // this optimises the important case of str == constant 0
81 if (is_constant (str))
82 s = str ? intern (str) : null;
83 else
84#endif
74 s = intern (str); 85 s = intern (str);
75 86
76 return *this; 87 return *this;
77 } 88 }
78}; 89};
79 90

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines