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.20 by root, Sun Dec 17 23:10:35 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>
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
17// makes dynamically allocated objects zero-initialised 29// makes dynamically allocated objects zero-initialised
18struct zero_initialised 30struct zero_initialised
19{ 31{
20 void *operator new (size_t s, void *p) 32 void *operator new (size_t s, void *p)
21 { 33 {
53 65
54// also copies src into the new area, like "memdup" 66// also copies src into the new area, like "memdup"
55// if src is 0, clears the memory 67// if src is 0, clears the memory
56template<typename T> 68template<typename T>
57inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } 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); }
58 74
59// for symmetry 75// for symmetry
60template<typename T> 76template<typename T>
61inline void sfree (T *ptr, int n = 1) throw () 77inline void sfree (T *ptr, int n = 1) throw ()
62{ 78{
116 { 132 {
117 p->~Tp (); 133 p->~Tp ();
118 } 134 }
119}; 135};
120 136
121struct refcounted
122{
123 refcounted () : refcnt (0) { }
124// virtual ~refcounted ();
125 void refcnt_inc () { ++refcnt; }
126 void refcnt_dec () { --refcnt; }
127 bool dead () { return refcnt == 0; }
128 mutable int refcnt;
129#if 0
130private:
131 static refcounted *rc_first;
132 refcounted *rc_next;
133#endif
134};
135
136template<class T> 137template<class T>
137struct refptr 138struct refptr
138{ 139{
139 T *p; 140 T *p;
140 141
161 T &operator * () const { return *p; } 162 T &operator * () const { return *p; }
162 T *operator ->() const { return p; } 163 T *operator ->() const { return p; }
163 164
164 operator T *() const { return p; } 165 operator T *() const { return p; }
165}; 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;
166 173
167struct str_hash 174struct str_hash
168{ 175{
169 std::size_t operator ()(const char *s) const 176 std::size_t operator ()(const char *s) const
170 { 177 {
196 { 203 {
197 return !strcmp (a, b); 204 return !strcmp (a, b);
198 } 205 }
199}; 206};
200 207
201#include <vector>
202
203template<class obj> 208template<class T>
204struct unordered_vector : std::vector<obj, slice_allocator<obj> > 209struct unordered_vector : std::vector<T, slice_allocator<T> >
205{ 210{
206 typedef typename unordered_vector::iterator iterator; 211 typedef typename unordered_vector::iterator iterator;
207 212
208 void erase (unsigned int pos) 213 void erase (unsigned int pos)
209 { 214 {
214 } 219 }
215 220
216 void erase (iterator i) 221 void erase (iterator i)
217 { 222 {
218 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);
219 } 260 }
220}; 261};
221 262
222template<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; }
223template<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; }
238typedef double tstamp; 279typedef double tstamp;
239 280
240// return current time as timestampe 281// return current time as timestampe
241tstamp now (); 282tstamp now ();
242 283
284int similar_direction (int a, int b);
285
243#endif 286#endif
244 287

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines