ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
Revision: 1.9
Committed: Mon Sep 4 11:07:59 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.8: +2 -2 lines
Log Message:
Changes...

- alternative shstr representation, saves code
- use glibs splice memory allocator (seems slower)
- use simpler memory/lifetime management for objects, no recycling

File Contents

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