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.27 by root, Mon Jan 15 00:40:49 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 <new>
12#include <vector>
13
14#include <glib.h>
15
16#include <shstr.h>
17#include <traits.h>
18
19// use a gcc extension for auto declarations until ISO C++ sanctifies them
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)
28
29// in range including end
30#define IN_RANGE_INC(val,beg,end) \
31 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
32
33// in range excluding end
34#define IN_RANGE_EXC(val,beg,end) \
35 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
36
10// makes dynamically allocated objects zero-initialised 37// makes dynamically allocated objects zero-initialised
11struct zero_initialised 38struct zero_initialised
12{ 39{
13 void *operator new (size_t s, void *); 40 void *operator new (size_t s, void *p)
41 {
42 memset (p, 0, s);
43 return p;
44 }
45
14 void *operator new (size_t s); 46 void *operator new (size_t s)
47 {
48 return g_slice_alloc0 (s);
49 }
50
15 void *operator new [] (size_t s); 51 void *operator new[] (size_t s)
52 {
53 return g_slice_alloc0 (s);
54 }
55
16 void operator delete (void *p, size_t s); 56 void operator delete (void *p, size_t s)
57 {
58 g_slice_free1 (s, p);
59 }
60
17 void operator delete [] (void *p, size_t s); 61 void operator delete[] (void *p, size_t s)
62 {
63 g_slice_free1 (s, p);
64 }
18}; 65};
19 66
20struct refcounted 67void *salloc_ (int n) throw (std::bad_alloc);
68void *salloc_ (int n, void *src) throw (std::bad_alloc);
69
70// strictly the same as g_slice_alloc, but never returns 0
71template<typename T>
72inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
73
74// also copies src into the new area, like "memdup"
75// if src is 0, clears the memory
76template<typename T>
77inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
78
79// clears the memory
80template<typename T>
81inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
82
83// for symmetry
84template<typename T>
85inline void sfree (T *ptr, int n = 1) throw ()
21{ 86{
22 mutable int refcnt; 87 g_slice_free1 (n * sizeof (T), (void *)ptr);
23 refcounted () : refcnt (0) { } 88}
24 void refcnt_inc () { ++refcnt; } 89
25 void refcnt_dec () { --refcnt; 90// a STL-compatible allocator that uses g_slice
26 if (refcnt < 0)abort();}//D 91// boy, this is verbose
92template<typename Tp>
93struct slice_allocator
94{
95 typedef size_t size_type;
96 typedef ptrdiff_t difference_type;
97 typedef Tp *pointer;
98 typedef const Tp *const_pointer;
99 typedef Tp &reference;
100 typedef const Tp &const_reference;
101 typedef Tp value_type;
102
103 template <class U>
104 struct rebind
105 {
106 typedef slice_allocator<U> other;
107 };
108
109 slice_allocator () throw () { }
110 slice_allocator (const slice_allocator &o) throw () { }
111 template<typename Tp2>
112 slice_allocator (const slice_allocator<Tp2> &) throw () { }
113
114 ~slice_allocator () { }
115
116 pointer address (reference x) const { return &x; }
117 const_pointer address (const_reference x) const { return &x; }
118
119 pointer allocate (size_type n, const_pointer = 0)
120 {
121 return salloc<Tp> (n);
122 }
123
124 void deallocate (pointer p, size_type n)
125 {
126 sfree<Tp> (p, n);
127 }
128
129 size_type max_size ()const throw ()
130 {
131 return size_t (-1) / sizeof (Tp);
132 }
133
134 void construct (pointer p, const Tp &val)
135 {
136 ::new (p) Tp (val);
137 }
138
139 void destroy (pointer p)
140 {
141 p->~Tp ();
142 }
27}; 143};
28 144
29template<class T> 145template<class T>
30struct refptr 146struct refptr
31{ 147{
54 T &operator * () const { return *p; } 170 T &operator * () const { return *p; }
55 T *operator ->() const { return p; } 171 T *operator ->() const { return p; }
56 172
57 operator T *() const { return p; } 173 operator T *() const { return p; }
58}; 174};
175
176typedef refptr<maptile> maptile_ptr;
177typedef refptr<object> object_ptr;
178typedef refptr<archetype> arch_ptr;
179typedef refptr<client> client_ptr;
180typedef refptr<player> player_ptr;
59 181
60struct str_hash 182struct str_hash
61{ 183{
62 std::size_t operator ()(const char *s) const 184 std::size_t operator ()(const char *s) const
63 { 185 {
89 { 211 {
90 return !strcmp (a, b); 212 return !strcmp (a, b);
91 } 213 }
92}; 214};
93 215
94#include <vector>
95
96template<class obj> 216template<class T>
97struct unordered_vector : std::vector<obj> 217struct unordered_vector : std::vector<T, slice_allocator<T> >
98{ 218{
99 typedef typename std::vector<obj>::iterator iterator; 219 typedef typename unordered_vector::iterator iterator;
100 220
101 void erase (unsigned int pos) 221 void erase (unsigned int pos)
102 { 222 {
103 if (pos < this->size () - 1) 223 if (pos < this->size () - 1)
104 (*this)[pos] = (*this)[this->size () - 1]; 224 (*this)[pos] = (*this)[this->size () - 1];
107 } 227 }
108 228
109 void erase (iterator i) 229 void erase (iterator i)
110 { 230 {
111 erase ((unsigned int )(i - this->begin ())); 231 erase ((unsigned int )(i - this->begin ()));
232 }
233};
234
235template<class T, int T::* index>
236struct object_vector : std::vector<T *, slice_allocator<T *> >
237{
238 void insert (T *obj)
239 {
240 assert (!(obj->*index));
241 push_back (obj);
242 obj->*index = this->size ();
243 }
244
245 void insert (T &obj)
246 {
247 insert (&obj);
248 }
249
250 void erase (T *obj)
251 {
252 assert (obj->*index);
253 int pos = obj->*index;
254 obj->*index = 0;
255
256 if (pos < this->size ())
257 {
258 (*this)[pos - 1] = (*this)[this->size () - 1];
259 (*this)[pos - 1]->*index = pos;
260 }
261
262 this->pop_back ();
263 }
264
265 void erase (T &obj)
266 {
267 errase (&obj);
112 } 268 }
113}; 269};
114 270
115template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 271template<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; } 272template<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; } 273template<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 274
119template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 275template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
120 276
121// basically does what strncpy should do 277// basically does what strncpy should do, but appends "..." to strings exceeding length
278void assign (char *dst, const char *src, int maxlen);
279
280// type-safe version of assign
122template<int N> 281template<int N>
123inline void assign (char (&dst)[N], const char *src) 282inline void assign (char (&dst)[N], const char *src)
124{ 283{
125 // should be optimised at some point, maybe should also add "..." 284 assign ((char *)&dst, src, N);
126 // when buffer is too small.
127 snprintf (dst, N, "%s", src);
128} 285}
129 286
287typedef double tstamp;
288
289// return current time as timestampe
290tstamp now ();
291
292int similar_direction (int a, int b);
293
130#endif 294#endif
131 295

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines