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.17 by root, Sat Dec 16 03:08:26 2006 UTC vs.
Revision 1.27 by root, Mon Jan 15 00:40:49 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 <new>
12#include <vector>
11 13
12#include <glib.h> 14#include <glib.h>
15
16#include <shstr.h>
17#include <traits.h>
13 18
14// 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
15#define AUTODECL(var,expr) typeof(expr) var = (expr) 20#define AUTODECL(var,expr) typeof(expr) var = (expr)
16 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
17// makes dynamically allocated objects zero-initialised 37// makes dynamically allocated objects zero-initialised
18struct zero_initialised 38struct zero_initialised
19{ 39{
20 void *operator new (size_t s, void *p) 40 void *operator new (size_t s, void *p)
21 { 41 {
42 { 62 {
43 g_slice_free1 (s, p); 63 g_slice_free1 (s, p);
44 } 64 }
45}; 65};
46 66
67void *salloc_ (int n) throw (std::bad_alloc);
68void *salloc_ (int n, void *src) throw (std::bad_alloc);
69
47// strictly the same as g_slice_alloc, but never returns 0 70// strictly the same as g_slice_alloc, but never returns 0
48void *salloc (int size) throw (std::bad_alloc); 71template<typename T>
72inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
73
49// also copies src into the new area, like "memdup" 74// also copies src into the new area, like "memdup"
50void *salloc (int size, void *src) throw (std::bad_alloc); 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
51// for symmetry 83// for symmetry
84template<typename T>
52inline void sfree (void *ptr, int size) throw () 85inline void sfree (T *ptr, int n = 1) throw ()
53{ 86{
54 g_slice_free1 (size, ptr); 87 g_slice_free1 (n * sizeof (T), (void *)ptr);
55} 88}
56 89
57// a STL-compatible allocator that uses g_slice 90// a STL-compatible allocator that uses g_slice
58// boy, this is verbose 91// boy, this is verbose
59template<typename Tp> 92template<typename Tp>
83 pointer address (reference x) const { return &x; } 116 pointer address (reference x) const { return &x; }
84 const_pointer address (const_reference x) const { return &x; } 117 const_pointer address (const_reference x) const { return &x; }
85 118
86 pointer allocate (size_type n, const_pointer = 0) 119 pointer allocate (size_type n, const_pointer = 0)
87 { 120 {
88 return static_cast<pointer>(salloc (n * sizeof (Tp))); 121 return salloc<Tp> (n);
89 } 122 }
90 123
91 void deallocate (pointer p, size_type n) 124 void deallocate (pointer p, size_type n)
92 { 125 {
93 sfree (static_cast<void *>(p), n * sizeof (Tp)); 126 sfree<Tp> (p, n);
94 } 127 }
95 128
96 size_type max_size ()const throw () 129 size_type max_size ()const throw ()
97 { 130 {
98 return size_t (-1) / sizeof (Tp); 131 return size_t (-1) / sizeof (Tp);
105 138
106 void destroy (pointer p) 139 void destroy (pointer p)
107 { 140 {
108 p->~Tp (); 141 p->~Tp ();
109 } 142 }
110};
111
112struct refcounted
113{
114 refcounted () : refcnt (0) { }
115// virtual ~refcounted ();
116 void refcnt_inc () { ++refcnt; }
117 void refcnt_dec () { --refcnt; }
118 bool dead () { return refcnt == 0; }
119 mutable int refcnt;
120#if 0
121private:
122 static refcounted *rc_first;
123 refcounted *rc_next;
124#endif
125}; 143};
126 144
127template<class T> 145template<class T>
128struct refptr 146struct refptr
129{ 147{
152 T &operator * () const { return *p; } 170 T &operator * () const { return *p; }
153 T *operator ->() const { return p; } 171 T *operator ->() const { return p; }
154 172
155 operator T *() const { return p; } 173 operator T *() const { return p; }
156}; 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;
157 181
158struct str_hash 182struct str_hash
159{ 183{
160 std::size_t operator ()(const char *s) const 184 std::size_t operator ()(const char *s) const
161 { 185 {
187 { 211 {
188 return !strcmp (a, b); 212 return !strcmp (a, b);
189 } 213 }
190}; 214};
191 215
192#include <vector>
193
194template<class obj> 216template<class T>
195struct unordered_vector : std::vector<obj, slice_allocator<obj> > 217struct unordered_vector : std::vector<T, slice_allocator<T> >
196{ 218{
197 typedef typename unordered_vector::iterator iterator; 219 typedef typename unordered_vector::iterator iterator;
198 220
199 void erase (unsigned int pos) 221 void erase (unsigned int pos)
200 { 222 {
205 } 227 }
206 228
207 void erase (iterator i) 229 void erase (iterator i)
208 { 230 {
209 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);
210 } 268 }
211}; 269};
212 270
213template<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; }
214template<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; }
229typedef double tstamp; 287typedef double tstamp;
230 288
231// return current time as timestampe 289// return current time as timestampe
232tstamp now (); 290tstamp now ();
233 291
292int similar_direction (int a, int b);
293
234#endif 294#endif
235 295

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines