|
|
1 | /* |
|
|
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
|
|
5 | * |
|
|
6 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
|
|
7 | * under the terms of the GNU General Public License as published by the Free |
|
|
8 | * Software Foundation; either version 2 of the License, or (at your option) |
|
|
9 | * any later version. |
|
|
10 | * |
|
|
11 | * This program is distributed in the hope that it will be useful, but |
|
|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|
|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
|
14 | * for more details. |
|
|
15 | * |
|
|
16 | * You should have received a copy of the GNU General Public License along |
|
|
17 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
|
|
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
19 | * |
|
|
20 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
|
|
21 | */ |
|
|
22 | |
1 | #ifndef UTIL_H__ |
23 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
24 | #define UTIL_H__ |
3 | |
25 | |
4 | //#define PREFER_MALLOC |
26 | //#define PREFER_MALLOC |
5 | |
27 | |
6 | #if __GNUC__ >= 3 |
28 | #if __GNUC__ >= 3 |
7 | # define is_constant(c) __builtin_constant_p (c) |
29 | # define is_constant(c) __builtin_constant_p (c) |
|
|
30 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
31 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
8 | #else |
32 | #else |
9 | # define is_constant(c) 0 |
33 | # define is_constant(c) 0 |
|
|
34 | # define expect(expr,value) (expr) |
|
|
35 | # define prefetch(addr,rw,locality) |
10 | #endif |
36 | #endif |
|
|
37 | |
|
|
38 | #if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
|
|
39 | # define decltype(x) typeof(x) |
|
|
40 | #endif |
|
|
41 | |
|
|
42 | // put into ifs if you are very sure that the expression |
|
|
43 | // is mostly true or mosty false. note that these return |
|
|
44 | // booleans, not the expression. |
|
|
45 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
46 | #define expect_true(expr) expect ((expr) != 0, 1) |
11 | |
47 | |
12 | #include <cstddef> |
48 | #include <cstddef> |
13 | #include <cmath> |
49 | #include <cmath> |
14 | #include <new> |
50 | #include <new> |
15 | #include <vector> |
51 | #include <vector> |
… | |
… | |
18 | |
54 | |
19 | #include <shstr.h> |
55 | #include <shstr.h> |
20 | #include <traits.h> |
56 | #include <traits.h> |
21 | |
57 | |
22 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
58 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
23 | #define auto(var,expr) typeof(expr) var = (expr) |
59 | #define auto(var,expr) decltype(expr) var = (expr) |
24 | |
60 | |
25 | // very ugly macro that basicaly declares and initialises a variable |
61 | // very ugly macro that basicaly declares and initialises a variable |
26 | // that is in scope for the next statement only |
62 | // that is in scope for the next statement only |
27 | // works only for stuff that can be assigned 0 and converts to false |
63 | // works only for stuff that can be assigned 0 and converts to false |
28 | // (note: works great for pointers) |
64 | // (note: works great for pointers) |
29 | // most ugly macro I ever wrote |
65 | // most ugly macro I ever wrote |
30 | #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
66 | #define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
31 | |
67 | |
32 | // in range including end |
68 | // in range including end |
33 | #define IN_RANGE_INC(val,beg,end) \ |
69 | #define IN_RANGE_INC(val,beg,end) \ |
34 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
70 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
35 | |
71 | |
… | |
… | |
391 | }; |
427 | }; |
392 | |
428 | |
393 | template<class T, int T::* index> |
429 | template<class T, int T::* index> |
394 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
430 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
395 | { |
431 | { |
|
|
432 | typedef typename object_vector::iterator iterator; |
|
|
433 | |
|
|
434 | bool contains (const T *obj) const |
|
|
435 | { |
|
|
436 | return obj->*index; |
|
|
437 | } |
|
|
438 | |
|
|
439 | iterator find (const T *obj) |
|
|
440 | { |
|
|
441 | return obj->*index |
|
|
442 | ? this->begin () + obj->*index - 1 |
|
|
443 | : this->end (); |
|
|
444 | } |
|
|
445 | |
396 | void insert (T *obj) |
446 | void insert (T *obj) |
397 | { |
447 | { |
398 | assert (!(obj->*index)); |
448 | assert (!(obj->*index)); |
399 | push_back (obj); |
449 | push_back (obj); |
400 | obj->*index = this->size (); |
450 | obj->*index = this->size (); |