--- deliantra/server/include/util.h 2006/12/23 16:05:19 1.23 +++ deliantra/server/include/util.h 2007/01/15 00:40:49 1.27 @@ -8,12 +8,32 @@ #endif #include +#include +#include #include +#include +#include + // 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 { @@ -122,21 +142,6 @@ } }; -struct refcounted -{ - refcounted () : refcnt (0) { } - virtual ~refcounted (); - void refcnt_inc () { ++refcnt; } - void refcnt_dec () { --refcnt; } - bool dead () { return refcnt == 0; } - mutable int refcnt; -#if 0 -private: - static refcounted *rc_first; - refcounted *rc_next; -#endif -}; - template struct refptr { @@ -168,9 +173,11 @@ operator T *() const { return p; } }; -typedef refptr player_ptr; +typedef refptr maptile_ptr; typedef refptr object_ptr; typedef refptr arch_ptr; +typedef refptr client_ptr; +typedef refptr player_ptr; struct str_hash { @@ -206,10 +213,8 @@ } }; -#include - -template -struct unordered_vector : std::vector > +template +struct unordered_vector : std::vector > { typedef typename unordered_vector::iterator iterator; @@ -227,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; } @@ -248,5 +289,7 @@ // return current time as timestampe tstamp now (); +int similar_direction (int a, int b); + #endif