--- deliantra/server/include/util.h 2006/12/30 10:16:10 1.25 +++ deliantra/server/include/util.h 2007/01/15 00:40:49 1.27 @@ -19,6 +19,21 @@ // use a gcc extension for auto declarations until ISO C++ sanctifies them #define AUTODECL(var,expr) typeof(expr) var = (expr) +// very ugly macro that basicaly declares and initialises a variable +// that is in scope for the next statement only +// works only for stuff that can be assigned 0 and converts to false +// (note: works great for pointers) +// most ugly macro I ever wrote +#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) + +// in range including end +#define IN_RANGE_INC(val,beg,end) \ + ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) + +// in range excluding end +#define IN_RANGE_EXC(val,beg,end) \ + ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) + // makes dynamically allocated objects zero-initialised struct zero_initialised { @@ -198,8 +213,8 @@ } }; -template -struct unordered_vector : std::vector > +template +struct unordered_vector : std::vector > { typedef typename unordered_vector::iterator iterator; @@ -217,6 +232,42 @@ } }; +template +struct object_vector : std::vector > +{ + void insert (T *obj) + { + assert (!(obj->*index)); + push_back (obj); + obj->*index = this->size (); + } + + void insert (T &obj) + { + insert (&obj); + } + + void erase (T *obj) + { + assert (obj->*index); + int pos = obj->*index; + obj->*index = 0; + + if (pos < this->size ()) + { + (*this)[pos - 1] = (*this)[this->size () - 1]; + (*this)[pos - 1]->*index = pos; + } + + this->pop_back (); + } + + void erase (T &obj) + { + errase (&obj); + } +}; + template static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } template static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } template static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; }