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.9 by root, Tue Sep 12 18:15:34 2006 UTC vs.
Revision 1.28 by root, Mon Jan 15 01:25:41 2007 UTC

5# define is_constant(c) __builtin_constant_p (c) 5# define is_constant(c) __builtin_constant_p (c)
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>
11#include <cmath>
12#include <new>
13#include <vector>
14
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
38// this is much faster than crossfires original algorithm
39// on modern cpus
40inline int
41isqrt (int n)
42{
43 return (int)sqrtf ((float)n);
44}
45
46// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
47#if 0
48// and has a max. error of 6 in the range -100..+100.
49#else
50// and has a max. error of 9 in the range -100..+100.
51#endif
52inline int
53idistance (int dx, int dy)
54{
55 unsigned int dx_ = abs (dx);
56 unsigned int dy_ = abs (dy);
57
58#if 0
59 return dx_ > dy_
60 ? (dx_ * 61685 + dy_ * 26870) >> 16
61 : (dy_ * 61685 + dx_ * 26870) >> 16;
62#else
63 return dx + dy - min (dx, dy) * 5 / 8;
64#endif
65}
66
67
10// makes dynamically allocated objects zero-initialised 68// makes dynamically allocated objects zero-initialised
11struct zero_initialised 69struct zero_initialised
12{ 70{
13 void *operator new (size_t s, void *); 71 void *operator new (size_t s, void *p)
72 {
73 memset (p, 0, s);
74 return p;
75 }
76
14 void *operator new (size_t s); 77 void *operator new (size_t s)
78 {
79 return g_slice_alloc0 (s);
80 }
81
15 void *operator new [] (size_t s); 82 void *operator new[] (size_t s)
83 {
84 return g_slice_alloc0 (s);
85 }
86
16 void operator delete (void *p, size_t s); 87 void operator delete (void *p, size_t s)
88 {
89 g_slice_free1 (s, p);
90 }
91
17 void operator delete [] (void *p, size_t s); 92 void operator delete[] (void *p, size_t s)
93 {
94 g_slice_free1 (s, p);
95 }
18}; 96};
19 97
20struct refcounted 98void *salloc_ (int n) throw (std::bad_alloc);
99void *salloc_ (int n, void *src) throw (std::bad_alloc);
100
101// strictly the same as g_slice_alloc, but never returns 0
102template<typename T>
103inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
104
105// also copies src into the new area, like "memdup"
106// if src is 0, clears the memory
107template<typename T>
108inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
109
110// clears the memory
111template<typename T>
112inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
113
114// for symmetry
115template<typename T>
116inline void sfree (T *ptr, int n = 1) throw ()
21{ 117{
22 mutable int refcnt; 118 g_slice_free1 (n * sizeof (T), (void *)ptr);
23 refcounted () : refcnt (0) { } 119}
24 void refcnt_inc () { ++refcnt; } 120
25 void refcnt_dec () { --refcnt; 121// a STL-compatible allocator that uses g_slice
26 if (refcnt < 0)abort();}//D 122// boy, this is verbose
123template<typename Tp>
124struct slice_allocator
125{
126 typedef size_t size_type;
127 typedef ptrdiff_t difference_type;
128 typedef Tp *pointer;
129 typedef const Tp *const_pointer;
130 typedef Tp &reference;
131 typedef const Tp &const_reference;
132 typedef Tp value_type;
133
134 template <class U>
135 struct rebind
136 {
137 typedef slice_allocator<U> other;
138 };
139
140 slice_allocator () throw () { }
141 slice_allocator (const slice_allocator &o) throw () { }
142 template<typename Tp2>
143 slice_allocator (const slice_allocator<Tp2> &) throw () { }
144
145 ~slice_allocator () { }
146
147 pointer address (reference x) const { return &x; }
148 const_pointer address (const_reference x) const { return &x; }
149
150 pointer allocate (size_type n, const_pointer = 0)
151 {
152 return salloc<Tp> (n);
153 }
154
155 void deallocate (pointer p, size_type n)
156 {
157 sfree<Tp> (p, n);
158 }
159
160 size_type max_size ()const throw ()
161 {
162 return size_t (-1) / sizeof (Tp);
163 }
164
165 void construct (pointer p, const Tp &val)
166 {
167 ::new (p) Tp (val);
168 }
169
170 void destroy (pointer p)
171 {
172 p->~Tp ();
173 }
27}; 174};
28 175
29template<class T> 176template<class T>
30struct refptr 177struct refptr
31{ 178{
54 T &operator * () const { return *p; } 201 T &operator * () const { return *p; }
55 T *operator ->() const { return p; } 202 T *operator ->() const { return p; }
56 203
57 operator T *() const { return p; } 204 operator T *() const { return p; }
58}; 205};
206
207typedef refptr<maptile> maptile_ptr;
208typedef refptr<object> object_ptr;
209typedef refptr<archetype> arch_ptr;
210typedef refptr<client> client_ptr;
211typedef refptr<player> player_ptr;
59 212
60struct str_hash 213struct str_hash
61{ 214{
62 std::size_t operator ()(const char *s) const 215 std::size_t operator ()(const char *s) const
63 { 216 {
89 { 242 {
90 return !strcmp (a, b); 243 return !strcmp (a, b);
91 } 244 }
92}; 245};
93 246
94#include <vector>
95
96template<class obj> 247template<class T>
97struct unordered_vector : std::vector<obj> 248struct unordered_vector : std::vector<T, slice_allocator<T> >
98{ 249{
99 typedef typename std::vector<obj>::iterator iterator; 250 typedef typename unordered_vector::iterator iterator;
100 251
101 void erase (unsigned int pos) 252 void erase (unsigned int pos)
102 { 253 {
103 if (pos < this->size () - 1) 254 if (pos < this->size () - 1)
104 (*this)[pos] = (*this)[this->size () - 1]; 255 (*this)[pos] = (*this)[this->size () - 1];
107 } 258 }
108 259
109 void erase (iterator i) 260 void erase (iterator i)
110 { 261 {
111 erase ((unsigned int )(i - this->begin ())); 262 erase ((unsigned int )(i - this->begin ()));
263 }
264};
265
266template<class T, int T::* index>
267struct object_vector : std::vector<T *, slice_allocator<T *> >
268{
269 void insert (T *obj)
270 {
271 assert (!(obj->*index));
272 push_back (obj);
273 obj->*index = this->size ();
274 }
275
276 void insert (T &obj)
277 {
278 insert (&obj);
279 }
280
281 void erase (T *obj)
282 {
283 assert (obj->*index);
284 int pos = obj->*index;
285 obj->*index = 0;
286
287 if (pos < this->size ())
288 {
289 (*this)[pos - 1] = (*this)[this->size () - 1];
290 (*this)[pos - 1]->*index = pos;
291 }
292
293 this->pop_back ();
294 }
295
296 void erase (T &obj)
297 {
298 errase (&obj);
112 } 299 }
113}; 300};
114 301
115template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 302template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
116template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 303template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
117template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } 304template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; }
118 305
119template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 306template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
120 307
121// basically does what strncpy should do 308// basically does what strncpy should do, but appends "..." to strings exceeding length
309void assign (char *dst, const char *src, int maxlen);
310
311// type-safe version of assign
122template<int N> 312template<int N>
123inline void assign (char (&dst)[N], const char *src) 313inline void assign (char (&dst)[N], const char *src)
124{ 314{
125 // should be optimised at some point, maybe should also add "..." 315 assign ((char *)&dst, src, N);
126 // when buffer is too small.
127 snprintf (dst, N, "%s", src);
128} 316}
129 317
318typedef double tstamp;
319
320// return current time as timestampe
321tstamp now ();
322
323int similar_direction (int a, int b);
324
130#endif 325#endif
131 326

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines