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.13 by root, Thu Sep 14 00:07:15 2006 UTC vs.
Revision 1.26 by root, Sun Jan 7 02:39:14 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>
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)
13 28
14// makes dynamically allocated objects zero-initialised 29// makes dynamically allocated objects zero-initialised
15struct zero_initialised 30struct zero_initialised
16{ 31{
17 void *operator new (size_t s, void *p) 32 void *operator new (size_t s, void *p)
39 { 54 {
40 g_slice_free1 (s, p); 55 g_slice_free1 (s, p);
41 } 56 }
42}; 57};
43 58
59void *salloc_ (int n) throw (std::bad_alloc);
60void *salloc_ (int n, void *src) throw (std::bad_alloc);
61
44// strictly the same as g_slice_alloc, but never returns 0 62// strictly the same as g_slice_alloc, but never returns 0
45void *alloc (int s) throw (std::bad_alloc); 63template<typename T>
64inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
65
66// also copies src into the new area, like "memdup"
67// if src is 0, clears the memory
68template<typename T>
69inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
70
71// clears the memory
72template<typename T>
73inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
74
46// for symmetry 75// for symmetry
47inline void dealloc (void *p, int s) throw () 76template<typename T>
77inline void sfree (T *ptr, int n = 1) throw ()
48{ 78{
49 g_slice_free1 (s, p); 79 g_slice_free1 (n * sizeof (T), (void *)ptr);
50} 80}
51 81
52// a STL-compatible allocator that uses g_slice 82// a STL-compatible allocator that uses g_slice
53// boy, this is verbose 83// boy, this is verbose
54template<typename Tp> 84template<typename Tp>
78 pointer address (reference x) const { return &x; } 108 pointer address (reference x) const { return &x; }
79 const_pointer address (const_reference x) const { return &x; } 109 const_pointer address (const_reference x) const { return &x; }
80 110
81 pointer allocate (size_type n, const_pointer = 0) 111 pointer allocate (size_type n, const_pointer = 0)
82 { 112 {
83 return static_cast<pointer>(alloc (n * sizeof (Tp))); 113 return salloc<Tp> (n);
84 } 114 }
85 115
86 void deallocate (pointer p, size_type n) 116 void deallocate (pointer p, size_type n)
87 { 117 {
88 dealloc (static_cast<void *>(p), n * sizeof (Tp)); 118 sfree<Tp> (p, n);
89 } 119 }
90 120
91 size_type max_size ()const throw () 121 size_type max_size ()const throw ()
92 { 122 {
93 return size_t (-1) / sizeof (Tp); 123 return size_t (-1) / sizeof (Tp);
100 130
101 void destroy (pointer p) 131 void destroy (pointer p)
102 { 132 {
103 p->~Tp (); 133 p->~Tp ();
104 } 134 }
105};
106
107struct refcounted
108{
109 mutable int refcnt;
110 refcounted () : refcnt (0) { }
111 void refcnt_inc () { ++refcnt; }
112 void refcnt_dec () { --refcnt;
113 if (refcnt < 0)abort();}//D
114}; 135};
115 136
116template<class T> 137template<class T>
117struct refptr 138struct refptr
118{ 139{
141 T &operator * () const { return *p; } 162 T &operator * () const { return *p; }
142 T *operator ->() const { return p; } 163 T *operator ->() const { return p; }
143 164
144 operator T *() const { return p; } 165 operator T *() const { return p; }
145}; 166};
167
168typedef refptr<maptile> maptile_ptr;
169typedef refptr<object> object_ptr;
170typedef refptr<archetype> arch_ptr;
171typedef refptr<client> client_ptr;
172typedef refptr<player> player_ptr;
146 173
147struct str_hash 174struct str_hash
148{ 175{
149 std::size_t operator ()(const char *s) const 176 std::size_t operator ()(const char *s) const
150 { 177 {
176 { 203 {
177 return !strcmp (a, b); 204 return !strcmp (a, b);
178 } 205 }
179}; 206};
180 207
181#include <vector>
182
183template<class obj> 208template<class T>
184struct unordered_vector : std::vector<obj, slice_allocator<obj> > 209struct unordered_vector : std::vector<T, slice_allocator<T> >
185{ 210{
186 typedef typename unordered_vector::iterator iterator; 211 typedef typename unordered_vector::iterator iterator;
187 212
188 void erase (unsigned int pos) 213 void erase (unsigned int pos)
189 { 214 {
194 } 219 }
195 220
196 void erase (iterator i) 221 void erase (iterator i)
197 { 222 {
198 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);
199 } 260 }
200}; 261};
201 262
202template<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; }
203template<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; }
213inline void assign (char (&dst)[N], const char *src) 274inline void assign (char (&dst)[N], const char *src)
214{ 275{
215 assign ((char *)&dst, src, N); 276 assign ((char *)&dst, src, N);
216} 277}
217 278
279typedef double tstamp;
280
281// return current time as timestampe
282tstamp now ();
283
284int similar_direction (int a, int b);
285
218#endif 286#endif
219 287

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines