… | |
… | |
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> |
13 | |
18 | |
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
19 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
20 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
21 | |
|
|
22 | // 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 | // 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)) |
16 | |
36 | |
17 | // makes dynamically allocated objects zero-initialised |
37 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
38 | struct zero_initialised |
19 | { |
39 | { |
20 | void *operator new (size_t s, void *p) |
40 | void *operator new (size_t s, void *p) |
… | |
… | |
120 | { |
140 | { |
121 | p->~Tp (); |
141 | p->~Tp (); |
122 | } |
142 | } |
123 | }; |
143 | }; |
124 | |
144 | |
125 | struct refcounted |
|
|
126 | { |
|
|
127 | refcounted () : refcnt (0) { } |
|
|
128 | // virtual ~refcounted (); |
|
|
129 | void refcnt_inc () { ++refcnt; } |
|
|
130 | void refcnt_dec () { --refcnt; } |
|
|
131 | bool dead () { return refcnt == 0; } |
|
|
132 | mutable int refcnt; |
|
|
133 | #if 0 |
|
|
134 | private: |
|
|
135 | static refcounted *rc_first; |
|
|
136 | refcounted *rc_next; |
|
|
137 | #endif |
|
|
138 | }; |
|
|
139 | |
|
|
140 | template<class T> |
145 | template<class T> |
141 | struct refptr |
146 | struct refptr |
142 | { |
147 | { |
143 | T *p; |
148 | T *p; |
144 | |
149 | |
… | |
… | |
166 | T *operator ->() const { return p; } |
171 | T *operator ->() const { return p; } |
167 | |
172 | |
168 | operator T *() const { return p; } |
173 | operator T *() const { return p; } |
169 | }; |
174 | }; |
170 | |
175 | |
171 | typedef refptr<player> player_ptr; |
176 | typedef refptr<maptile> maptile_ptr; |
172 | typedef refptr<object> object_ptr; |
177 | typedef refptr<object> object_ptr; |
173 | typedef refptr<archetype> arch_ptr; |
178 | typedef refptr<archetype> arch_ptr; |
|
|
179 | typedef refptr<client> client_ptr; |
|
|
180 | typedef refptr<player> player_ptr; |
174 | |
181 | |
175 | struct str_hash |
182 | struct str_hash |
176 | { |
183 | { |
177 | std::size_t operator ()(const char *s) const |
184 | std::size_t operator ()(const char *s) const |
178 | { |
185 | { |
… | |
… | |
204 | { |
211 | { |
205 | return !strcmp (a, b); |
212 | return !strcmp (a, b); |
206 | } |
213 | } |
207 | }; |
214 | }; |
208 | |
215 | |
209 | #include <vector> |
|
|
210 | |
|
|
211 | template<class obj> |
216 | template<class T> |
212 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
217 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
213 | { |
218 | { |
214 | typedef typename unordered_vector::iterator iterator; |
219 | typedef typename unordered_vector::iterator iterator; |
215 | |
220 | |
216 | void erase (unsigned int pos) |
221 | void erase (unsigned int pos) |
217 | { |
222 | { |
… | |
… | |
222 | } |
227 | } |
223 | |
228 | |
224 | void erase (iterator i) |
229 | void erase (iterator i) |
225 | { |
230 | { |
226 | erase ((unsigned int )(i - this->begin ())); |
231 | erase ((unsigned int )(i - this->begin ())); |
|
|
232 | } |
|
|
233 | }; |
|
|
234 | |
|
|
235 | 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); |
227 | } |
268 | } |
228 | }; |
269 | }; |
229 | |
270 | |
230 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
271 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
231 | template<typename T, typename U> static inline T max (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; } |
… | |
… | |
246 | typedef double tstamp; |
287 | typedef double tstamp; |
247 | |
288 | |
248 | // return current time as timestampe |
289 | // return current time as timestampe |
249 | tstamp now (); |
290 | tstamp now (); |
250 | |
291 | |
|
|
292 | int similar_direction (int a, int b); |
|
|
293 | |
251 | #endif |
294 | #endif |
252 | |
295 | |