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.2 by root, Sun Sep 3 23:33:01 2006 UTC vs.
Revision 1.25 by root, Sat Dec 30 10:16:10 2006 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
10// makes dynamically allocated objects zero-initialised 22// makes dynamically allocated objects zero-initialised
11struct zero_initialised 23struct zero_initialised
12{ 24{
25 void *operator new (size_t s, void *p)
26 {
27 memset (p, 0, s);
28 return p;
29 }
30
13 void *operator new (size_t s); 31 void *operator new (size_t s)
32 {
33 return g_slice_alloc0 (s);
34 }
35
36 void *operator new[] (size_t s)
37 {
38 return g_slice_alloc0 (s);
39 }
40
14 void operator delete (void *p, size_t s); 41 void operator delete (void *p, size_t s)
42 {
43 g_slice_free1 (s, p);
44 }
45
46 void operator delete[] (void *p, size_t s)
47 {
48 g_slice_free1 (s, p);
49 }
15}; 50};
51
52void *salloc_ (int n) throw (std::bad_alloc);
53void *salloc_ (int n, void *src) throw (std::bad_alloc);
54
55// strictly the same as g_slice_alloc, but never returns 0
56template<typename T>
57inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
58
59// also copies src into the new area, like "memdup"
60// if src is 0, clears the memory
61template<typename T>
62inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
63
64// clears the memory
65template<typename T>
66inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
67
68// for symmetry
69template<typename T>
70inline void sfree (T *ptr, int n = 1) throw ()
71{
72 g_slice_free1 (n * sizeof (T), (void *)ptr);
73}
74
75// a STL-compatible allocator that uses g_slice
76// boy, this is verbose
77template<typename Tp>
78struct slice_allocator
79{
80 typedef size_t size_type;
81 typedef ptrdiff_t difference_type;
82 typedef Tp *pointer;
83 typedef const Tp *const_pointer;
84 typedef Tp &reference;
85 typedef const Tp &const_reference;
86 typedef Tp value_type;
87
88 template <class U>
89 struct rebind
90 {
91 typedef slice_allocator<U> other;
92 };
93
94 slice_allocator () throw () { }
95 slice_allocator (const slice_allocator &o) throw () { }
96 template<typename Tp2>
97 slice_allocator (const slice_allocator<Tp2> &) throw () { }
98
99 ~slice_allocator () { }
100
101 pointer address (reference x) const { return &x; }
102 const_pointer address (const_reference x) const { return &x; }
103
104 pointer allocate (size_type n, const_pointer = 0)
105 {
106 return salloc<Tp> (n);
107 }
108
109 void deallocate (pointer p, size_type n)
110 {
111 sfree<Tp> (p, n);
112 }
113
114 size_type max_size ()const throw ()
115 {
116 return size_t (-1) / sizeof (Tp);
117 }
118
119 void construct (pointer p, const Tp &val)
120 {
121 ::new (p) Tp (val);
122 }
123
124 void destroy (pointer p)
125 {
126 p->~Tp ();
127 }
128};
129
130template<class T>
131struct refptr
132{
133 T *p;
134
135 refptr () : p(0) { }
136 refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); }
137 refptr (T *p) : p(p) { if (p) p->refcnt_inc (); }
138 ~refptr () { if (p) p->refcnt_dec (); }
139
140 const refptr<T> &operator =(T *o)
141 {
142 if (p) p->refcnt_dec ();
143 p = o;
144 if (p) p->refcnt_inc ();
145
146 return *this;
147 }
148
149 const refptr<T> &operator =(const refptr<T> o)
150 {
151 *this = o.p;
152 return *this;
153 }
154
155 T &operator * () const { return *p; }
156 T *operator ->() const { return p; }
157
158 operator T *() const { return p; }
159};
160
161typedef refptr<maptile> maptile_ptr;
162typedef refptr<object> object_ptr;
163typedef refptr<archetype> arch_ptr;
164typedef refptr<client> client_ptr;
165typedef refptr<player> player_ptr;
166
167struct str_hash
168{
169 std::size_t operator ()(const char *s) const
170 {
171 unsigned long hash = 0;
172
173 /* use the one-at-a-time hash function, which supposedly is
174 * better than the djb2-like one used by perl5.005, but
175 * certainly is better then the bug used here before.
176 * see http://burtleburtle.net/bob/hash/doobs.html
177 */
178 while (*s)
179 {
180 hash += *s++;
181 hash += hash << 10;
182 hash ^= hash >> 6;
183 }
184
185 hash += hash << 3;
186 hash ^= hash >> 11;
187 hash += hash << 15;
188
189 return hash;
190 }
191};
192
193struct str_equal
194{
195 bool operator ()(const char *a, const char *b) const
196 {
197 return !strcmp (a, b);
198 }
199};
200
201template<class obj>
202struct unordered_vector : std::vector<obj, slice_allocator<obj> >
203{
204 typedef typename unordered_vector::iterator iterator;
205
206 void erase (unsigned int pos)
207 {
208 if (pos < this->size () - 1)
209 (*this)[pos] = (*this)[this->size () - 1];
210
211 this->pop_back ();
212 }
213
214 void erase (iterator i)
215 {
216 erase ((unsigned int )(i - this->begin ()));
217 }
218};
219
220template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
221template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
222template<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; }
223
224template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
225
226// basically does what strncpy should do, but appends "..." to strings exceeding length
227void assign (char *dst, const char *src, int maxlen);
228
229// type-safe version of assign
230template<int N>
231inline void assign (char (&dst)[N], const char *src)
232{
233 assign ((char *)&dst, src, N);
234}
235
236typedef double tstamp;
237
238// return current time as timestampe
239tstamp now ();
240
241int similar_direction (int a, int b);
16 242
17#endif 243#endif
18 244

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines