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.26 by root, Sun Jan 7 02:39:14 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)
21 28
22// makes dynamically allocated objects zero-initialised 29// makes dynamically allocated objects zero-initialised
23struct zero_initialised 30struct zero_initialised
24{ 31{
25 void *operator new (size_t s, void *p) 32 void *operator new (size_t s, void *p)
196 { 203 {
197 return !strcmp (a, b); 204 return !strcmp (a, b);
198 } 205 }
199}; 206};
200 207
201template<class obj> 208template<class T>
202struct unordered_vector : std::vector<obj, slice_allocator<obj> > 209struct unordered_vector : std::vector<T, slice_allocator<T> >
203{ 210{
204 typedef typename unordered_vector::iterator iterator; 211 typedef typename unordered_vector::iterator iterator;
205 212
206 void erase (unsigned int pos) 213 void erase (unsigned int pos)
207 { 214 {
212 } 219 }
213 220
214 void erase (iterator i) 221 void erase (iterator i)
215 { 222 {
216 erase ((unsigned int )(i - this->begin ())); 223 erase ((unsigned int )(i - this->begin ()));
224 }
225};
226
227template<class T, int T::* index>
228struct 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);
217 } 260 }
218}; 261};
219 262
220template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 263template<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; } 264template<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