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.11 by root, Tue Sep 12 20:55:40 2006 UTC vs.
Revision 1.31 by root, Mon Jan 15 02:39:41 2007 UTC

6#else 6#else
7# define is_constant(c) 0 7# define is_constant(c) 0
8#endif 8#endif
9 9
10#include <cstddef> 10#include <cstddef>
11#include <cmath>
12#include <new>
13#include <vector>
11 14
12#include <glib.h> 15#include <glib.h>
16
17#include <shstr.h>
18#include <traits.h>
19
20// use a gcc extension for auto declarations until ISO C++ sanctifies them
21#define AUTODECL(var,expr) typeof(expr) var = (expr)
22
23// very ugly macro that basicaly declares and initialises a variable
24// that is in scope for the next statement only
25// works only for stuff that can be assigned 0 and converts to false
26// (note: works great for pointers)
27// most ugly macro I ever wrote
28#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
29
30// in range including end
31#define IN_RANGE_INC(val,beg,end) \
32 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
33
34// in range excluding end
35#define IN_RANGE_EXC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
37
38void fork_abort (const char *msg);
39
40// this is much faster than crossfires original algorithm
41// on modern cpus
42inline int
43isqrt (int n)
44{
45 return (int)sqrtf ((float)n);
46}
47
48// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
49#if 0
50// and has a max. error of 6 in the range -100..+100.
51#else
52// and has a max. error of 9 in the range -100..+100.
53#endif
54inline int
55idistance (int dx, int dy)
56{
57 unsigned int dx_ = abs (dx);
58 unsigned int dy_ = abs (dy);
59
60#if 0
61 return dx_ > dy_
62 ? (dx_ * 61685 + dy_ * 26870) >> 16
63 : (dy_ * 61685 + dx_ * 26870) >> 16;
64#else
65 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
66#endif
67}
68
69/*
70 * absdir(int): Returns a number between 1 and 8, which represent
71 * the "absolute" direction of a number (it actually takes care of
72 * "overflow" in previous calculations of a direction).
73 */
74inline int
75absdir (int d)
76{
77 return ((d - 1) & 7) + 1;
78}
13 79
14// makes dynamically allocated objects zero-initialised 80// makes dynamically allocated objects zero-initialised
15struct zero_initialised 81struct zero_initialised
16{ 82{
17 void *operator new (size_t s, void *p) 83 void *operator new (size_t s, void *p)
39 { 105 {
40 g_slice_free1 (s, p); 106 g_slice_free1 (s, p);
41 } 107 }
42}; 108};
43 109
44void throw_bad_alloc () throw (std::bad_alloc); 110void *salloc_ (int n) throw (std::bad_alloc);
45
46void *alloc (int s) throw (std::bad_alloc); 111void *salloc_ (int n, void *src) throw (std::bad_alloc);
47void dealloc (void *p, int s) throw (); 112
113// strictly the same as g_slice_alloc, but never returns 0
114template<typename T>
115inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
116
117// also copies src into the new area, like "memdup"
118// if src is 0, clears the memory
119template<typename T>
120inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
121
122// clears the memory
123template<typename T>
124inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
125
126// for symmetry
127template<typename T>
128inline void sfree (T *ptr, int n = 1) throw ()
129{
130 g_slice_free1 (n * sizeof (T), (void *)ptr);
131}
48 132
49// a STL-compatible allocator that uses g_slice 133// a STL-compatible allocator that uses g_slice
50// boy, this is verbose 134// boy, this is verbose
51template<typename Tp> 135template<typename Tp>
52struct slice_allocator 136struct slice_allocator
75 pointer address (reference x) const { return &x; } 159 pointer address (reference x) const { return &x; }
76 const_pointer address (const_reference x) const { return &x; } 160 const_pointer address (const_reference x) const { return &x; }
77 161
78 pointer allocate (size_type n, const_pointer = 0) 162 pointer allocate (size_type n, const_pointer = 0)
79 { 163 {
80 return static_cast<pointer>(alloc (n * sizeof (Tp))); 164 return salloc<Tp> (n);
81 } 165 }
82 166
83 void deallocate (pointer p, size_type n) 167 void deallocate (pointer p, size_type n)
84 { 168 {
85 dealloc (static_cast<void *>(p), n); 169 sfree<Tp> (p, n);
86 } 170 }
87 171
88 size_type max_size ()const throw () 172 size_type max_size ()const throw ()
89 { 173 {
90 return size_t (-1) / sizeof (Tp); 174 return size_t (-1) / sizeof (Tp);
97 181
98 void destroy (pointer p) 182 void destroy (pointer p)
99 { 183 {
100 p->~Tp (); 184 p->~Tp ();
101 } 185 }
102};
103
104struct refcounted
105{
106 mutable int refcnt;
107 refcounted () : refcnt (0) { }
108 void refcnt_inc () { ++refcnt; }
109 void refcnt_dec () { --refcnt;
110 if (refcnt < 0)abort();}//D
111}; 186};
112 187
113template<class T> 188template<class T>
114struct refptr 189struct refptr
115{ 190{
138 T &operator * () const { return *p; } 213 T &operator * () const { return *p; }
139 T *operator ->() const { return p; } 214 T *operator ->() const { return p; }
140 215
141 operator T *() const { return p; } 216 operator T *() const { return p; }
142}; 217};
218
219typedef refptr<maptile> maptile_ptr;
220typedef refptr<object> object_ptr;
221typedef refptr<archetype> arch_ptr;
222typedef refptr<client> client_ptr;
223typedef refptr<player> player_ptr;
143 224
144struct str_hash 225struct str_hash
145{ 226{
146 std::size_t operator ()(const char *s) const 227 std::size_t operator ()(const char *s) const
147 { 228 {
173 { 254 {
174 return !strcmp (a, b); 255 return !strcmp (a, b);
175 } 256 }
176}; 257};
177 258
178#include <vector>
179
180template<class obj> 259template<class T>
181struct unordered_vector : std::vector<obj, slice_allocator<obj> > 260struct unordered_vector : std::vector<T, slice_allocator<T> >
182{ 261{
183 typedef typename unordered_vector::iterator iterator; 262 typedef typename unordered_vector::iterator iterator;
184 263
185 void erase (unsigned int pos) 264 void erase (unsigned int pos)
186 { 265 {
191 } 270 }
192 271
193 void erase (iterator i) 272 void erase (iterator i)
194 { 273 {
195 erase ((unsigned int )(i - this->begin ())); 274 erase ((unsigned int )(i - this->begin ()));
275 }
276};
277
278template<class T, int T::* index>
279struct object_vector : std::vector<T *, slice_allocator<T *> >
280{
281 void insert (T *obj)
282 {
283 assert (!(obj->*index));
284 push_back (obj);
285 obj->*index = this->size ();
286 }
287
288 void insert (T &obj)
289 {
290 insert (&obj);
291 }
292
293 void erase (T *obj)
294 {
295 assert (obj->*index);
296 int pos = obj->*index;
297 obj->*index = 0;
298
299 if (pos < this->size ())
300 {
301 (*this)[pos - 1] = (*this)[this->size () - 1];
302 (*this)[pos - 1]->*index = pos;
303 }
304
305 this->pop_back ();
306 }
307
308 void erase (T &obj)
309 {
310 errase (&obj);
196 } 311 }
197}; 312};
198 313
199template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 314template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
200template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 315template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
210inline void assign (char (&dst)[N], const char *src) 325inline void assign (char (&dst)[N], const char *src)
211{ 326{
212 assign ((char *)&dst, src, N); 327 assign ((char *)&dst, src, N);
213} 328}
214 329
330typedef double tstamp;
331
332// return current time as timestampe
333tstamp now ();
334
335int similar_direction (int a, int b);
336
215#endif 337#endif
216 338

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines