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.1 |
// makes dynamically allocated objects zero-initialised |
23 |
|
|
struct zero_initialised |
24 |
|
|
{ |
25 |
root |
1.11 |
void *operator new (size_t s, void *p) |
26 |
|
|
{ |
27 |
|
|
memset (p, 0, s); |
28 |
|
|
return p; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
void *operator new (size_t s) |
32 |
|
|
{ |
33 |
|
|
return g_slice_alloc0 (s); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
void *operator new[] (size_t s) |
37 |
|
|
{ |
38 |
|
|
return g_slice_alloc0 (s); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
void operator delete (void *p, size_t s) |
42 |
|
|
{ |
43 |
|
|
g_slice_free1 (s, p); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
void operator delete[] (void *p, size_t s) |
47 |
|
|
{ |
48 |
|
|
g_slice_free1 (s, p); |
49 |
|
|
} |
50 |
|
|
}; |
51 |
|
|
|
52 |
root |
1.20 |
void *salloc_ (int n) throw (std::bad_alloc); |
53 |
|
|
void *salloc_ (int n, void *src) throw (std::bad_alloc); |
54 |
|
|
|
55 |
root |
1.12 |
// strictly the same as g_slice_alloc, but never returns 0 |
56 |
root |
1.20 |
template<typename T> |
57 |
|
|
inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
58 |
|
|
|
59 |
root |
1.17 |
// also copies src into the new area, like "memdup" |
60 |
root |
1.18 |
// if src is 0, clears the memory |
61 |
|
|
template<typename T> |
62 |
root |
1.20 |
inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
63 |
root |
1.18 |
|
64 |
root |
1.21 |
// 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 |
root |
1.12 |
// for symmetry |
69 |
root |
1.18 |
template<typename T> |
70 |
root |
1.20 |
inline void sfree (T *ptr, int n = 1) throw () |
71 |
root |
1.12 |
{ |
72 |
root |
1.20 |
g_slice_free1 (n * sizeof (T), (void *)ptr); |
73 |
root |
1.12 |
} |
74 |
root |
1.11 |
|
75 |
|
|
// a STL-compatible allocator that uses g_slice |
76 |
|
|
// boy, this is verbose |
77 |
|
|
template<typename Tp> |
78 |
|
|
struct slice_allocator |
79 |
|
|
{ |
80 |
|
|
typedef size_t size_type; |
81 |
|
|
typedef ptrdiff_t difference_type; |
82 |
|
|
typedef Tp *pointer; |
83 |
|
|
typedef const Tp *const_pointer; |
84 |
|
|
typedef Tp &reference; |
85 |
|
|
typedef const Tp &const_reference; |
86 |
|
|
typedef Tp value_type; |
87 |
|
|
|
88 |
|
|
template <class U> |
89 |
|
|
struct rebind |
90 |
|
|
{ |
91 |
|
|
typedef slice_allocator<U> other; |
92 |
|
|
}; |
93 |
|
|
|
94 |
|
|
slice_allocator () throw () { } |
95 |
|
|
slice_allocator (const slice_allocator &o) throw () { } |
96 |
|
|
template<typename Tp2> |
97 |
|
|
slice_allocator (const slice_allocator<Tp2> &) throw () { } |
98 |
|
|
|
99 |
|
|
~slice_allocator () { } |
100 |
|
|
|
101 |
|
|
pointer address (reference x) const { return &x; } |
102 |
|
|
const_pointer address (const_reference x) const { return &x; } |
103 |
|
|
|
104 |
|
|
pointer allocate (size_type n, const_pointer = 0) |
105 |
|
|
{ |
106 |
root |
1.18 |
return salloc<Tp> (n); |
107 |
root |
1.11 |
} |
108 |
|
|
|
109 |
|
|
void deallocate (pointer p, size_type n) |
110 |
|
|
{ |
111 |
root |
1.19 |
sfree<Tp> (p, n); |
112 |
root |
1.11 |
} |
113 |
|
|
|
114 |
|
|
size_type max_size ()const throw () |
115 |
|
|
{ |
116 |
|
|
return size_t (-1) / sizeof (Tp); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
void construct (pointer p, const Tp &val) |
120 |
|
|
{ |
121 |
|
|
::new (p) Tp (val); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
void destroy (pointer p) |
125 |
|
|
{ |
126 |
|
|
p->~Tp (); |
127 |
|
|
} |
128 |
root |
1.1 |
}; |
129 |
|
|
|
130 |
root |
1.7 |
template<class T> |
131 |
|
|
struct refptr |
132 |
|
|
{ |
133 |
|
|
T *p; |
134 |
|
|
|
135 |
|
|
refptr () : p(0) { } |
136 |
|
|
refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
137 |
|
|
refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
138 |
|
|
~refptr () { if (p) p->refcnt_dec (); } |
139 |
|
|
|
140 |
|
|
const refptr<T> &operator =(T *o) |
141 |
|
|
{ |
142 |
|
|
if (p) p->refcnt_dec (); |
143 |
|
|
p = o; |
144 |
|
|
if (p) p->refcnt_inc (); |
145 |
|
|
|
146 |
|
|
return *this; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
const refptr<T> &operator =(const refptr<T> o) |
150 |
|
|
{ |
151 |
|
|
*this = o.p; |
152 |
|
|
return *this; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
T &operator * () const { return *p; } |
156 |
|
|
T *operator ->() const { return p; } |
157 |
|
|
|
158 |
|
|
operator T *() const { return p; } |
159 |
|
|
}; |
160 |
|
|
|
161 |
root |
1.24 |
typedef refptr<maptile> maptile_ptr; |
162 |
root |
1.22 |
typedef refptr<object> object_ptr; |
163 |
|
|
typedef refptr<archetype> arch_ptr; |
164 |
root |
1.24 |
typedef refptr<client> client_ptr; |
165 |
|
|
typedef refptr<player> player_ptr; |
166 |
root |
1.22 |
|
167 |
root |
1.4 |
struct str_hash |
168 |
|
|
{ |
169 |
|
|
std::size_t operator ()(const char *s) const |
170 |
|
|
{ |
171 |
|
|
unsigned long hash = 0; |
172 |
|
|
|
173 |
|
|
/* use the one-at-a-time hash function, which supposedly is |
174 |
|
|
* better than the djb2-like one used by perl5.005, but |
175 |
|
|
* certainly is better then the bug used here before. |
176 |
|
|
* see http://burtleburtle.net/bob/hash/doobs.html |
177 |
|
|
*/ |
178 |
|
|
while (*s) |
179 |
|
|
{ |
180 |
|
|
hash += *s++; |
181 |
|
|
hash += hash << 10; |
182 |
|
|
hash ^= hash >> 6; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
hash += hash << 3; |
186 |
|
|
hash ^= hash >> 11; |
187 |
|
|
hash += hash << 15; |
188 |
|
|
|
189 |
|
|
return hash; |
190 |
|
|
} |
191 |
|
|
}; |
192 |
|
|
|
193 |
|
|
struct str_equal |
194 |
|
|
{ |
195 |
|
|
bool operator ()(const char *a, const char *b) const |
196 |
|
|
{ |
197 |
|
|
return !strcmp (a, b); |
198 |
|
|
} |
199 |
|
|
}; |
200 |
|
|
|
201 |
root |
1.6 |
template<class obj> |
202 |
root |
1.11 |
struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
203 |
root |
1.6 |
{ |
204 |
root |
1.11 |
typedef typename unordered_vector::iterator iterator; |
205 |
root |
1.6 |
|
206 |
|
|
void erase (unsigned int pos) |
207 |
|
|
{ |
208 |
|
|
if (pos < this->size () - 1) |
209 |
|
|
(*this)[pos] = (*this)[this->size () - 1]; |
210 |
|
|
|
211 |
|
|
this->pop_back (); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
void erase (iterator i) |
215 |
|
|
{ |
216 |
|
|
erase ((unsigned int )(i - this->begin ())); |
217 |
|
|
} |
218 |
|
|
}; |
219 |
|
|
|
220 |
root |
1.8 |
template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
221 |
|
|
template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
222 |
|
|
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; } |
223 |
|
|
|
224 |
|
|
template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
225 |
|
|
|
226 |
root |
1.10 |
// basically does what strncpy should do, but appends "..." to strings exceeding length |
227 |
|
|
void assign (char *dst, const char *src, int maxlen); |
228 |
|
|
|
229 |
|
|
// type-safe version of assign |
230 |
root |
1.9 |
template<int N> |
231 |
|
|
inline void assign (char (&dst)[N], const char *src) |
232 |
|
|
{ |
233 |
root |
1.10 |
assign ((char *)&dst, src, N); |
234 |
root |
1.9 |
} |
235 |
|
|
|
236 |
root |
1.17 |
typedef double tstamp; |
237 |
|
|
|
238 |
|
|
// return current time as timestampe |
239 |
|
|
tstamp now (); |
240 |
|
|
|
241 |
root |
1.25 |
int similar_direction (int a, int b); |
242 |
|
|
|
243 |
root |
1.1 |
#endif |
244 |
|
|
|