… | |
… | |
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) |
16 | |
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 | |
17 | // makes dynamically allocated objects zero-initialised |
29 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
30 | struct zero_initialised |
19 | { |
31 | { |
20 | void *operator new (size_t s, void *p) |
32 | void *operator new (size_t s, void *p) |
21 | { |
33 | { |
… | |
… | |
42 | { |
54 | { |
43 | g_slice_free1 (s, p); |
55 | g_slice_free1 (s, p); |
44 | } |
56 | } |
45 | }; |
57 | }; |
46 | |
58 | |
|
|
59 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
60 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
61 | |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
62 | // strictly the same as g_slice_alloc, but never returns 0 |
48 | void *salloc (int size) throw (std::bad_alloc); |
63 | template<typename T> |
|
|
64 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
65 | |
49 | // also copies src into the new area, like "memdup" |
66 | // also copies src into the new area, like "memdup" |
50 | void *salloc (int size, void *src) throw (std::bad_alloc); |
67 | // if src is 0, clears the memory |
|
|
68 | template<typename T> |
|
|
69 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
70 | |
|
|
71 | // clears the memory |
|
|
72 | template<typename T> |
|
|
73 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
|
|
74 | |
51 | // for symmetry |
75 | // for symmetry |
|
|
76 | template<typename T> |
52 | inline void sfree (void *ptr, int size) throw () |
77 | inline void sfree (T *ptr, int n = 1) throw () |
53 | { |
78 | { |
54 | g_slice_free1 (size, ptr); |
79 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
55 | } |
80 | } |
56 | |
81 | |
57 | // a STL-compatible allocator that uses g_slice |
82 | // a STL-compatible allocator that uses g_slice |
58 | // boy, this is verbose |
83 | // boy, this is verbose |
59 | template<typename Tp> |
84 | template<typename Tp> |
… | |
… | |
83 | pointer address (reference x) const { return &x; } |
108 | pointer address (reference x) const { return &x; } |
84 | const_pointer address (const_reference x) const { return &x; } |
109 | const_pointer address (const_reference x) const { return &x; } |
85 | |
110 | |
86 | pointer allocate (size_type n, const_pointer = 0) |
111 | pointer allocate (size_type n, const_pointer = 0) |
87 | { |
112 | { |
88 | return static_cast<pointer>(salloc (n * sizeof (Tp))); |
113 | return salloc<Tp> (n); |
89 | } |
114 | } |
90 | |
115 | |
91 | void deallocate (pointer p, size_type n) |
116 | void deallocate (pointer p, size_type n) |
92 | { |
117 | { |
93 | sfree (static_cast<void *>(p), n * sizeof (Tp)); |
118 | sfree<Tp> (p, n); |
94 | } |
119 | } |
95 | |
120 | |
96 | size_type max_size ()const throw () |
121 | size_type max_size ()const throw () |
97 | { |
122 | { |
98 | return size_t (-1) / sizeof (Tp); |
123 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
105 | |
130 | |
106 | void destroy (pointer p) |
131 | void destroy (pointer p) |
107 | { |
132 | { |
108 | p->~Tp (); |
133 | p->~Tp (); |
109 | } |
134 | } |
110 | }; |
|
|
111 | |
|
|
112 | struct refcounted |
|
|
113 | { |
|
|
114 | refcounted () : refcnt (0) { } |
|
|
115 | // virtual ~refcounted (); |
|
|
116 | void refcnt_inc () { ++refcnt; } |
|
|
117 | void refcnt_dec () { --refcnt; } |
|
|
118 | bool dead () { return refcnt == 0; } |
|
|
119 | mutable int refcnt; |
|
|
120 | #if 0 |
|
|
121 | private: |
|
|
122 | static refcounted *rc_first; |
|
|
123 | refcounted *rc_next; |
|
|
124 | #endif |
|
|
125 | }; |
135 | }; |
126 | |
136 | |
127 | template<class T> |
137 | template<class T> |
128 | struct refptr |
138 | struct refptr |
129 | { |
139 | { |
… | |
… | |
152 | T &operator * () const { return *p; } |
162 | T &operator * () const { return *p; } |
153 | T *operator ->() const { return p; } |
163 | T *operator ->() const { return p; } |
154 | |
164 | |
155 | operator T *() const { return p; } |
165 | operator T *() const { return p; } |
156 | }; |
166 | }; |
|
|
167 | |
|
|
168 | typedef refptr<maptile> maptile_ptr; |
|
|
169 | typedef refptr<object> object_ptr; |
|
|
170 | typedef refptr<archetype> arch_ptr; |
|
|
171 | typedef refptr<client> client_ptr; |
|
|
172 | typedef refptr<player> player_ptr; |
157 | |
173 | |
158 | struct str_hash |
174 | struct str_hash |
159 | { |
175 | { |
160 | std::size_t operator ()(const char *s) const |
176 | std::size_t operator ()(const char *s) const |
161 | { |
177 | { |
… | |
… | |
187 | { |
203 | { |
188 | return !strcmp (a, b); |
204 | return !strcmp (a, b); |
189 | } |
205 | } |
190 | }; |
206 | }; |
191 | |
207 | |
192 | #include <vector> |
|
|
193 | |
|
|
194 | template<class obj> |
208 | template<class T> |
195 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
209 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
196 | { |
210 | { |
197 | typedef typename unordered_vector::iterator iterator; |
211 | typedef typename unordered_vector::iterator iterator; |
198 | |
212 | |
199 | void erase (unsigned int pos) |
213 | void erase (unsigned int pos) |
200 | { |
214 | { |
… | |
… | |
205 | } |
219 | } |
206 | |
220 | |
207 | void erase (iterator i) |
221 | void erase (iterator i) |
208 | { |
222 | { |
209 | erase ((unsigned int )(i - this->begin ())); |
223 | erase ((unsigned int )(i - this->begin ())); |
|
|
224 | } |
|
|
225 | }; |
|
|
226 | |
|
|
227 | template<class T, int T::* index> |
|
|
228 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
|
|
229 | { |
|
|
230 | void insert (T *obj) |
|
|
231 | { |
|
|
232 | assert (!(obj->*index)); |
|
|
233 | push_back (obj); |
|
|
234 | obj->*index = this->size (); |
|
|
235 | } |
|
|
236 | |
|
|
237 | void insert (T &obj) |
|
|
238 | { |
|
|
239 | insert (&obj); |
|
|
240 | } |
|
|
241 | |
|
|
242 | void erase (T *obj) |
|
|
243 | { |
|
|
244 | assert (obj->*index); |
|
|
245 | int pos = obj->*index; |
|
|
246 | obj->*index = 0; |
|
|
247 | |
|
|
248 | if (pos < this->size ()) |
|
|
249 | { |
|
|
250 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
251 | (*this)[pos - 1]->*index = pos; |
|
|
252 | } |
|
|
253 | |
|
|
254 | this->pop_back (); |
|
|
255 | } |
|
|
256 | |
|
|
257 | void erase (T &obj) |
|
|
258 | { |
|
|
259 | errase (&obj); |
210 | } |
260 | } |
211 | }; |
261 | }; |
212 | |
262 | |
213 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
263 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
214 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
264 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
… | |
… | |
229 | typedef double tstamp; |
279 | typedef double tstamp; |
230 | |
280 | |
231 | // return current time as timestampe |
281 | // return current time as timestampe |
232 | tstamp now (); |
282 | tstamp now (); |
233 | |
283 | |
|
|
284 | int similar_direction (int a, int b); |
|
|
285 | |
234 | #endif |
286 | #endif |
235 | |
287 | |