1 |
root |
1.1 |
#ifndef UTIL_H__ |
2 |
|
|
#define UTIL_H__ |
3 |
|
|
|
4 |
root |
1.2 |
#if __GNUC__ >= 3 |
5 |
|
|
# define is_constant(c) __builtin_constant_p (c) |
6 |
|
|
#else |
7 |
|
|
# define is_constant(c) 0 |
8 |
|
|
#endif |
9 |
|
|
|
10 |
root |
1.11 |
#include <cstddef> |
11 |
root |
1.25 |
#include <new> |
12 |
|
|
#include <vector> |
13 |
root |
1.11 |
|
14 |
|
|
#include <glib.h> |
15 |
|
|
|
16 |
root |
1.25 |
#include <shstr.h> |
17 |
|
|
#include <traits.h> |
18 |
|
|
|
19 |
root |
1.14 |
// use a gcc extension for auto declarations until ISO C++ sanctifies them |
20 |
|
|
#define AUTODECL(var,expr) typeof(expr) var = (expr) |
21 |
|
|
|
22 |
root |
1.26 |
// very ugly macro that basicaly declares and initialises a variable |
23 |
|
|
// that is in scope for the next statement only |
24 |
|
|
// works only for stuff that can be assigned 0 and converts to false |
25 |
|
|
// (note: works great for pointers) |
26 |
|
|
// most ugly macro I ever wrote |
27 |
|
|
#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
28 |
|
|
|
29 |
root |
1.27 |
// in range including end |
30 |
|
|
#define IN_RANGE_INC(val,beg,end) \ |
31 |
|
|
((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
32 |
|
|
|
33 |
|
|
// in range excluding end |
34 |
|
|
#define IN_RANGE_EXC(val,beg,end) \ |
35 |
|
|
((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
36 |
|
|
|
37 |
root |
1.1 |
// makes dynamically allocated objects zero-initialised |
38 |
|
|
struct zero_initialised |
39 |
|
|
{ |
40 |
root |
1.11 |
void *operator new (size_t s, void *p) |
41 |
|
|
{ |
42 |
|
|
memset (p, 0, s); |
43 |
|
|
return p; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
void *operator new (size_t s) |
47 |
|
|
{ |
48 |
|
|
return g_slice_alloc0 (s); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
void *operator new[] (size_t s) |
52 |
|
|
{ |
53 |
|
|
return g_slice_alloc0 (s); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
void operator delete (void *p, size_t s) |
57 |
|
|
{ |
58 |
|
|
g_slice_free1 (s, p); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
void operator delete[] (void *p, size_t s) |
62 |
|
|
{ |
63 |
|
|
g_slice_free1 (s, p); |
64 |
|
|
} |
65 |
|
|
}; |
66 |
|
|
|
67 |
root |
1.20 |
void *salloc_ (int n) throw (std::bad_alloc); |
68 |
|
|
void *salloc_ (int n, void *src) throw (std::bad_alloc); |
69 |
|
|
|
70 |
root |
1.12 |
// strictly the same as g_slice_alloc, but never returns 0 |
71 |
root |
1.20 |
template<typename T> |
72 |
|
|
inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
73 |
|
|
|
74 |
root |
1.17 |
// also copies src into the new area, like "memdup" |
75 |
root |
1.18 |
// if src is 0, clears the memory |
76 |
|
|
template<typename T> |
77 |
root |
1.20 |
inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
78 |
root |
1.18 |
|
79 |
root |
1.21 |
// clears the memory |
80 |
|
|
template<typename T> |
81 |
|
|
inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
82 |
|
|
|
83 |
root |
1.12 |
// for symmetry |
84 |
root |
1.18 |
template<typename T> |
85 |
root |
1.20 |
inline void sfree (T *ptr, int n = 1) throw () |
86 |
root |
1.12 |
{ |
87 |
root |
1.20 |
g_slice_free1 (n * sizeof (T), (void *)ptr); |
88 |
root |
1.12 |
} |
89 |
root |
1.11 |
|
90 |
|
|
// a STL-compatible allocator that uses g_slice |
91 |
|
|
// boy, this is verbose |
92 |
|
|
template<typename Tp> |
93 |
|
|
struct slice_allocator |
94 |
|
|
{ |
95 |
|
|
typedef size_t size_type; |
96 |
|
|
typedef ptrdiff_t difference_type; |
97 |
|
|
typedef Tp *pointer; |
98 |
|
|
typedef const Tp *const_pointer; |
99 |
|
|
typedef Tp &reference; |
100 |
|
|
typedef const Tp &const_reference; |
101 |
|
|
typedef Tp value_type; |
102 |
|
|
|
103 |
|
|
template <class U> |
104 |
|
|
struct rebind |
105 |
|
|
{ |
106 |
|
|
typedef slice_allocator<U> other; |
107 |
|
|
}; |
108 |
|
|
|
109 |
|
|
slice_allocator () throw () { } |
110 |
|
|
slice_allocator (const slice_allocator &o) throw () { } |
111 |
|
|
template<typename Tp2> |
112 |
|
|
slice_allocator (const slice_allocator<Tp2> &) throw () { } |
113 |
|
|
|
114 |
|
|
~slice_allocator () { } |
115 |
|
|
|
116 |
|
|
pointer address (reference x) const { return &x; } |
117 |
|
|
const_pointer address (const_reference x) const { return &x; } |
118 |
|
|
|
119 |
|
|
pointer allocate (size_type n, const_pointer = 0) |
120 |
|
|
{ |
121 |
root |
1.18 |
return salloc<Tp> (n); |
122 |
root |
1.11 |
} |
123 |
|
|
|
124 |
|
|
void deallocate (pointer p, size_type n) |
125 |
|
|
{ |
126 |
root |
1.19 |
sfree<Tp> (p, n); |
127 |
root |
1.11 |
} |
128 |
|
|
|
129 |
|
|
size_type max_size ()const throw () |
130 |
|
|
{ |
131 |
|
|
return size_t (-1) / sizeof (Tp); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
void construct (pointer p, const Tp &val) |
135 |
|
|
{ |
136 |
|
|
::new (p) Tp (val); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void destroy (pointer p) |
140 |
|
|
{ |
141 |
|
|
p->~Tp (); |
142 |
|
|
} |
143 |
root |
1.1 |
}; |
144 |
|
|
|
145 |
root |
1.7 |
template<class T> |
146 |
|
|
struct refptr |
147 |
|
|
{ |
148 |
|
|
T *p; |
149 |
|
|
|
150 |
|
|
refptr () : p(0) { } |
151 |
|
|
refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
152 |
|
|
refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
153 |
|
|
~refptr () { if (p) p->refcnt_dec (); } |
154 |
|
|
|
155 |
|
|
const refptr<T> &operator =(T *o) |
156 |
|
|
{ |
157 |
|
|
if (p) p->refcnt_dec (); |
158 |
|
|
p = o; |
159 |
|
|
if (p) p->refcnt_inc (); |
160 |
|
|
|
161 |
|
|
return *this; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
const refptr<T> &operator =(const refptr<T> o) |
165 |
|
|
{ |
166 |
|
|
*this = o.p; |
167 |
|
|
return *this; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
T &operator * () const { return *p; } |
171 |
|
|
T *operator ->() const { return p; } |
172 |
|
|
|
173 |
|
|
operator T *() const { return p; } |
174 |
|
|
}; |
175 |
|
|
|
176 |
root |
1.24 |
typedef refptr<maptile> maptile_ptr; |
177 |
root |
1.22 |
typedef refptr<object> object_ptr; |
178 |
|
|
typedef refptr<archetype> arch_ptr; |
179 |
root |
1.24 |
typedef refptr<client> client_ptr; |
180 |
|
|
typedef refptr<player> player_ptr; |
181 |
root |
1.22 |
|
182 |
root |
1.4 |
struct str_hash |
183 |
|
|
{ |
184 |
|
|
std::size_t operator ()(const char *s) const |
185 |
|
|
{ |
186 |
|
|
unsigned long hash = 0; |
187 |
|
|
|
188 |
|
|
/* use the one-at-a-time hash function, which supposedly is |
189 |
|
|
* better than the djb2-like one used by perl5.005, but |
190 |
|
|
* certainly is better then the bug used here before. |
191 |
|
|
* see http://burtleburtle.net/bob/hash/doobs.html |
192 |
|
|
*/ |
193 |
|
|
while (*s) |
194 |
|
|
{ |
195 |
|
|
hash += *s++; |
196 |
|
|
hash += hash << 10; |
197 |
|
|
hash ^= hash >> 6; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
hash += hash << 3; |
201 |
|
|
hash ^= hash >> 11; |
202 |
|
|
hash += hash << 15; |
203 |
|
|
|
204 |
|
|
return hash; |
205 |
|
|
} |
206 |
|
|
}; |
207 |
|
|
|
208 |
|
|
struct str_equal |
209 |
|
|
{ |
210 |
|
|
bool operator ()(const char *a, const char *b) const |
211 |
|
|
{ |
212 |
|
|
return !strcmp (a, b); |
213 |
|
|
} |
214 |
|
|
}; |
215 |
|
|
|
216 |
root |
1.26 |
template<class T> |
217 |
|
|
struct unordered_vector : std::vector<T, slice_allocator<T> > |
218 |
root |
1.6 |
{ |
219 |
root |
1.11 |
typedef typename unordered_vector::iterator iterator; |
220 |
root |
1.6 |
|
221 |
|
|
void erase (unsigned int pos) |
222 |
|
|
{ |
223 |
|
|
if (pos < this->size () - 1) |
224 |
|
|
(*this)[pos] = (*this)[this->size () - 1]; |
225 |
|
|
|
226 |
|
|
this->pop_back (); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
void erase (iterator i) |
230 |
|
|
{ |
231 |
|
|
erase ((unsigned int )(i - this->begin ())); |
232 |
|
|
} |
233 |
|
|
}; |
234 |
|
|
|
235 |
root |
1.26 |
template<class T, int T::* index> |
236 |
|
|
struct object_vector : std::vector<T *, slice_allocator<T *> > |
237 |
|
|
{ |
238 |
|
|
void insert (T *obj) |
239 |
|
|
{ |
240 |
|
|
assert (!(obj->*index)); |
241 |
|
|
push_back (obj); |
242 |
|
|
obj->*index = this->size (); |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
void insert (T &obj) |
246 |
|
|
{ |
247 |
|
|
insert (&obj); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
void erase (T *obj) |
251 |
|
|
{ |
252 |
|
|
assert (obj->*index); |
253 |
|
|
int pos = obj->*index; |
254 |
|
|
obj->*index = 0; |
255 |
|
|
|
256 |
|
|
if (pos < this->size ()) |
257 |
|
|
{ |
258 |
|
|
(*this)[pos - 1] = (*this)[this->size () - 1]; |
259 |
|
|
(*this)[pos - 1]->*index = pos; |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
this->pop_back (); |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
void erase (T &obj) |
266 |
|
|
{ |
267 |
|
|
errase (&obj); |
268 |
|
|
} |
269 |
|
|
}; |
270 |
|
|
|
271 |
root |
1.8 |
template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
272 |
|
|
template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
273 |
|
|
template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
274 |
|
|
|
275 |
|
|
template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
276 |
|
|
|
277 |
root |
1.10 |
// basically does what strncpy should do, but appends "..." to strings exceeding length |
278 |
|
|
void assign (char *dst, const char *src, int maxlen); |
279 |
|
|
|
280 |
|
|
// type-safe version of assign |
281 |
root |
1.9 |
template<int N> |
282 |
|
|
inline void assign (char (&dst)[N], const char *src) |
283 |
|
|
{ |
284 |
root |
1.10 |
assign ((char *)&dst, src, N); |
285 |
root |
1.9 |
} |
286 |
|
|
|
287 |
root |
1.17 |
typedef double tstamp; |
288 |
|
|
|
289 |
|
|
// return current time as timestampe |
290 |
|
|
tstamp now (); |
291 |
|
|
|
292 |
root |
1.25 |
int similar_direction (int a, int b); |
293 |
|
|
|
294 |
root |
1.1 |
#endif |
295 |
|
|
|