ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.25 by root, Sat Dec 30 10:16:10 2006 UTC vs.
Revision 1.27 by root, Mon Jan 15 00:40:49 2007 UTC

16#include <shstr.h> 16#include <shstr.h>
17#include <traits.h> 17#include <traits.h>
18 18
19// 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
20#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))
21 36
22// makes dynamically allocated objects zero-initialised 37// makes dynamically allocated objects zero-initialised
23struct zero_initialised 38struct zero_initialised
24{ 39{
25 void *operator new (size_t s, void *p) 40 void *operator new (size_t s, void *p)
196 { 211 {
197 return !strcmp (a, b); 212 return !strcmp (a, b);
198 } 213 }
199}; 214};
200 215
201template<class obj> 216template<class T>
202struct unordered_vector : std::vector<obj, slice_allocator<obj> > 217struct unordered_vector : std::vector<T, slice_allocator<T> >
203{ 218{
204 typedef typename unordered_vector::iterator iterator; 219 typedef typename unordered_vector::iterator iterator;
205 220
206 void erase (unsigned int pos) 221 void erase (unsigned int pos)
207 { 222 {
212 } 227 }
213 228
214 void erase (iterator i) 229 void erase (iterator i)
215 { 230 {
216 erase ((unsigned int )(i - this->begin ())); 231 erase ((unsigned int )(i - this->begin ()));
232 }
233};
234
235template<class T, int T::* index>
236struct 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);
217 } 268 }
218}; 269};
219 270
220template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 271template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
221template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 272template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines