… | |
… | |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
10 | #include <cstddef> |
10 | #include <cstddef> |
|
|
11 | #include <new> |
|
|
12 | #include <vector> |
11 | |
13 | |
12 | #include <glib.h> |
14 | #include <glib.h> |
|
|
15 | |
|
|
16 | #include <shstr.h> |
|
|
17 | #include <traits.h> |
|
|
18 | |
|
|
19 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
20 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
13 | |
21 | |
14 | // makes dynamically allocated objects zero-initialised |
22 | // makes dynamically allocated objects zero-initialised |
15 | struct zero_initialised |
23 | struct zero_initialised |
16 | { |
24 | { |
17 | void *operator new (size_t s, void *p) |
25 | void *operator new (size_t s, void *p) |
… | |
… | |
39 | { |
47 | { |
40 | g_slice_free1 (s, p); |
48 | g_slice_free1 (s, p); |
41 | } |
49 | } |
42 | }; |
50 | }; |
43 | |
51 | |
44 | void throw_bad_alloc () throw (std::bad_alloc); |
52 | void *salloc_ (int n) throw (std::bad_alloc); |
45 | |
|
|
46 | void *alloc (int s) throw (std::bad_alloc); |
53 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
47 | void dealloc (void *p, int s) throw (); |
54 | |
|
|
55 | // strictly the same as g_slice_alloc, but never returns 0 |
|
|
56 | template<typename T> |
|
|
57 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
58 | |
|
|
59 | // also copies src into the new area, like "memdup" |
|
|
60 | // if src is 0, clears the memory |
|
|
61 | template<typename T> |
|
|
62 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
63 | |
|
|
64 | // clears the memory |
|
|
65 | template<typename T> |
|
|
66 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
|
|
67 | |
|
|
68 | // for symmetry |
|
|
69 | template<typename T> |
|
|
70 | inline void sfree (T *ptr, int n = 1) throw () |
|
|
71 | { |
|
|
72 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
73 | } |
48 | |
74 | |
49 | // a STL-compatible allocator that uses g_slice |
75 | // a STL-compatible allocator that uses g_slice |
50 | // boy, this is verbose |
76 | // boy, this is verbose |
51 | template<typename Tp> |
77 | template<typename Tp> |
52 | struct slice_allocator |
78 | struct slice_allocator |
… | |
… | |
75 | pointer address (reference x) const { return &x; } |
101 | pointer address (reference x) const { return &x; } |
76 | const_pointer address (const_reference x) const { return &x; } |
102 | const_pointer address (const_reference x) const { return &x; } |
77 | |
103 | |
78 | pointer allocate (size_type n, const_pointer = 0) |
104 | pointer allocate (size_type n, const_pointer = 0) |
79 | { |
105 | { |
80 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
106 | return salloc<Tp> (n); |
81 | } |
107 | } |
82 | |
108 | |
83 | void deallocate (pointer p, size_type n) |
109 | void deallocate (pointer p, size_type n) |
84 | { |
110 | { |
85 | dealloc (static_cast<void *>(p), n); |
111 | sfree<Tp> (p, n); |
86 | } |
112 | } |
87 | |
113 | |
88 | size_type max_size ()const throw () |
114 | size_type max_size ()const throw () |
89 | { |
115 | { |
90 | return size_t (-1) / sizeof (Tp); |
116 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
97 | |
123 | |
98 | void destroy (pointer p) |
124 | void destroy (pointer p) |
99 | { |
125 | { |
100 | p->~Tp (); |
126 | p->~Tp (); |
101 | } |
127 | } |
102 | }; |
|
|
103 | |
|
|
104 | struct refcounted |
|
|
105 | { |
|
|
106 | mutable int refcnt; |
|
|
107 | refcounted () : refcnt (0) { } |
|
|
108 | void refcnt_inc () { ++refcnt; } |
|
|
109 | void refcnt_dec () { --refcnt; |
|
|
110 | if (refcnt < 0)abort();}//D |
|
|
111 | }; |
128 | }; |
112 | |
129 | |
113 | template<class T> |
130 | template<class T> |
114 | struct refptr |
131 | struct refptr |
115 | { |
132 | { |
… | |
… | |
138 | T &operator * () const { return *p; } |
155 | T &operator * () const { return *p; } |
139 | T *operator ->() const { return p; } |
156 | T *operator ->() const { return p; } |
140 | |
157 | |
141 | operator T *() const { return p; } |
158 | operator T *() const { return p; } |
142 | }; |
159 | }; |
|
|
160 | |
|
|
161 | typedef refptr<maptile> maptile_ptr; |
|
|
162 | typedef refptr<object> object_ptr; |
|
|
163 | typedef refptr<archetype> arch_ptr; |
|
|
164 | typedef refptr<client> client_ptr; |
|
|
165 | typedef refptr<player> player_ptr; |
143 | |
166 | |
144 | struct str_hash |
167 | struct str_hash |
145 | { |
168 | { |
146 | std::size_t operator ()(const char *s) const |
169 | std::size_t operator ()(const char *s) const |
147 | { |
170 | { |
… | |
… | |
173 | { |
196 | { |
174 | return !strcmp (a, b); |
197 | return !strcmp (a, b); |
175 | } |
198 | } |
176 | }; |
199 | }; |
177 | |
200 | |
178 | #include <vector> |
|
|
179 | |
|
|
180 | template<class obj> |
201 | template<class obj> |
181 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
202 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
182 | { |
203 | { |
183 | typedef typename unordered_vector::iterator iterator; |
204 | typedef typename unordered_vector::iterator iterator; |
184 | |
205 | |
… | |
… | |
210 | inline void assign (char (&dst)[N], const char *src) |
231 | inline void assign (char (&dst)[N], const char *src) |
211 | { |
232 | { |
212 | assign ((char *)&dst, src, N); |
233 | assign ((char *)&dst, src, N); |
213 | } |
234 | } |
214 | |
235 | |
|
|
236 | typedef double tstamp; |
|
|
237 | |
|
|
238 | // return current time as timestampe |
|
|
239 | tstamp now (); |
|
|
240 | |
|
|
241 | int similar_direction (int a, int b); |
|
|
242 | |
215 | #endif |
243 | #endif |
216 | |
244 | |