--- deliantra/server/include/util.h 2006/09/12 20:55:40 1.11 +++ deliantra/server/include/util.h 2007/01/07 02:39:14 1.26 @@ -8,9 +8,24 @@ #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) + // makes dynamically allocated objects zero-initialised struct zero_initialised { @@ -41,10 +56,28 @@ } }; -void throw_bad_alloc () throw (std::bad_alloc); +void *salloc_ (int n) throw (std::bad_alloc); +void *salloc_ (int n, void *src) throw (std::bad_alloc); -void *alloc (int s) throw (std::bad_alloc); -void dealloc (void *p, int s) throw (); +// strictly the same as g_slice_alloc, but never returns 0 +template +inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } + +// also copies src into the new area, like "memdup" +// if src is 0, clears the memory +template +inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } + +// clears the memory +template +inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } + +// for symmetry +template +inline void sfree (T *ptr, int n = 1) throw () +{ + g_slice_free1 (n * sizeof (T), (void *)ptr); +} // a STL-compatible allocator that uses g_slice // boy, this is verbose @@ -77,12 +110,12 @@ pointer allocate (size_type n, const_pointer = 0) { - return static_cast(alloc (n * sizeof (Tp))); + return salloc (n); } void deallocate (pointer p, size_type n) { - dealloc (static_cast(p), n); + sfree (p, n); } size_type max_size ()const throw () @@ -101,15 +134,6 @@ } }; -struct refcounted -{ - mutable int refcnt; - refcounted () : refcnt (0) { } - void refcnt_inc () { ++refcnt; } - void refcnt_dec () { --refcnt; - if (refcnt < 0)abort();}//D -}; - template struct refptr { @@ -141,6 +165,12 @@ operator T *() const { return p; } }; +typedef refptr maptile_ptr; +typedef refptr object_ptr; +typedef refptr arch_ptr; +typedef refptr client_ptr; +typedef refptr player_ptr; + struct str_hash { std::size_t operator ()(const char *s) const @@ -175,10 +205,8 @@ } }; -#include - -template -struct unordered_vector : std::vector > +template +struct unordered_vector : std::vector > { typedef typename unordered_vector::iterator iterator; @@ -196,6 +224,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; } @@ -212,5 +276,12 @@ assign ((char *)&dst, src, N); } +typedef double tstamp; + +// return current time as timestampe +tstamp now (); + +int similar_direction (int a, int b); + #endif