ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
Revision: 1.26
Committed: Sun Jan 7 02:39:14 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.25: +45 -2 lines
Log Message:
""

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H__
2     #define UTIL_H__
3    
4 root 1.2 #if __GNUC__ >= 3
5     # define is_constant(c) __builtin_constant_p (c)
6     #else
7     # define is_constant(c) 0
8     #endif
9    
10 root 1.11 #include <cstddef>
11 root 1.25 #include <new>
12     #include <vector>
13 root 1.11
14     #include <glib.h>
15    
16 root 1.25 #include <shstr.h>
17     #include <traits.h>
18    
19 root 1.14 // use a gcc extension for auto declarations until ISO C++ sanctifies them
20     #define AUTODECL(var,expr) typeof(expr) var = (expr)
21    
22 root 1.26 // 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 root 1.1 // makes dynamically allocated objects zero-initialised
30     struct zero_initialised
31     {
32 root 1.11 void *operator new (size_t s, void *p)
33     {
34     memset (p, 0, s);
35     return p;
36     }
37    
38     void *operator new (size_t s)
39     {
40     return g_slice_alloc0 (s);
41     }
42    
43     void *operator new[] (size_t s)
44     {
45     return g_slice_alloc0 (s);
46     }
47    
48     void operator delete (void *p, size_t s)
49     {
50     g_slice_free1 (s, p);
51     }
52    
53     void operator delete[] (void *p, size_t s)
54     {
55     g_slice_free1 (s, p);
56     }
57     };
58    
59 root 1.20 void *salloc_ (int n) throw (std::bad_alloc);
60     void *salloc_ (int n, void *src) throw (std::bad_alloc);
61    
62 root 1.12 // strictly the same as g_slice_alloc, but never returns 0
63 root 1.20 template<typename T>
64     inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
65    
66 root 1.17 // also copies src into the new area, like "memdup"
67 root 1.18 // if src is 0, clears the memory
68     template<typename T>
69 root 1.20 inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
70 root 1.18
71 root 1.21 // clears the memory
72     template<typename T>
73     inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
74    
75 root 1.12 // for symmetry
76 root 1.18 template<typename T>
77 root 1.20 inline void sfree (T *ptr, int n = 1) throw ()
78 root 1.12 {
79 root 1.20 g_slice_free1 (n * sizeof (T), (void *)ptr);
80 root 1.12 }
81 root 1.11
82     // a STL-compatible allocator that uses g_slice
83     // boy, this is verbose
84     template<typename Tp>
85     struct slice_allocator
86     {
87     typedef size_t size_type;
88     typedef ptrdiff_t difference_type;
89     typedef Tp *pointer;
90     typedef const Tp *const_pointer;
91     typedef Tp &reference;
92     typedef const Tp &const_reference;
93     typedef Tp value_type;
94    
95     template <class U>
96     struct rebind
97     {
98     typedef slice_allocator<U> other;
99     };
100    
101     slice_allocator () throw () { }
102     slice_allocator (const slice_allocator &o) throw () { }
103     template<typename Tp2>
104     slice_allocator (const slice_allocator<Tp2> &) throw () { }
105    
106     ~slice_allocator () { }
107    
108     pointer address (reference x) const { return &x; }
109     const_pointer address (const_reference x) const { return &x; }
110    
111     pointer allocate (size_type n, const_pointer = 0)
112     {
113 root 1.18 return salloc<Tp> (n);
114 root 1.11 }
115    
116     void deallocate (pointer p, size_type n)
117     {
118 root 1.19 sfree<Tp> (p, n);
119 root 1.11 }
120    
121     size_type max_size ()const throw ()
122     {
123     return size_t (-1) / sizeof (Tp);
124     }
125    
126     void construct (pointer p, const Tp &val)
127     {
128     ::new (p) Tp (val);
129     }
130    
131     void destroy (pointer p)
132     {
133     p->~Tp ();
134     }
135 root 1.1 };
136    
137 root 1.7 template<class T>
138     struct refptr
139     {
140     T *p;
141    
142     refptr () : p(0) { }
143     refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); }
144     refptr (T *p) : p(p) { if (p) p->refcnt_inc (); }
145     ~refptr () { if (p) p->refcnt_dec (); }
146    
147     const refptr<T> &operator =(T *o)
148     {
149     if (p) p->refcnt_dec ();
150     p = o;
151     if (p) p->refcnt_inc ();
152    
153     return *this;
154     }
155    
156     const refptr<T> &operator =(const refptr<T> o)
157     {
158     *this = o.p;
159     return *this;
160     }
161    
162     T &operator * () const { return *p; }
163     T *operator ->() const { return p; }
164    
165     operator T *() const { return p; }
166     };
167    
168 root 1.24 typedef refptr<maptile> maptile_ptr;
169 root 1.22 typedef refptr<object> object_ptr;
170     typedef refptr<archetype> arch_ptr;
171 root 1.24 typedef refptr<client> client_ptr;
172     typedef refptr<player> player_ptr;
173 root 1.22
174 root 1.4 struct str_hash
175     {
176     std::size_t operator ()(const char *s) const
177     {
178     unsigned long hash = 0;
179    
180     /* use the one-at-a-time hash function, which supposedly is
181     * better than the djb2-like one used by perl5.005, but
182     * certainly is better then the bug used here before.
183     * see http://burtleburtle.net/bob/hash/doobs.html
184     */
185     while (*s)
186     {
187     hash += *s++;
188     hash += hash << 10;
189     hash ^= hash >> 6;
190     }
191    
192     hash += hash << 3;
193     hash ^= hash >> 11;
194     hash += hash << 15;
195    
196     return hash;
197     }
198     };
199    
200     struct str_equal
201     {
202     bool operator ()(const char *a, const char *b) const
203     {
204     return !strcmp (a, b);
205     }
206     };
207    
208 root 1.26 template<class T>
209     struct unordered_vector : std::vector<T, slice_allocator<T> >
210 root 1.6 {
211 root 1.11 typedef typename unordered_vector::iterator iterator;
212 root 1.6
213     void erase (unsigned int pos)
214     {
215     if (pos < this->size () - 1)
216     (*this)[pos] = (*this)[this->size () - 1];
217    
218     this->pop_back ();
219     }
220    
221     void erase (iterator i)
222     {
223     erase ((unsigned int )(i - this->begin ()));
224     }
225     };
226    
227 root 1.26 template<class T, int T::* index>
228     struct 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);
260     }
261     };
262    
263 root 1.8 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
264     template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
265     template<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; }
266    
267     template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
268    
269 root 1.10 // basically does what strncpy should do, but appends "..." to strings exceeding length
270     void assign (char *dst, const char *src, int maxlen);
271    
272     // type-safe version of assign
273 root 1.9 template<int N>
274     inline void assign (char (&dst)[N], const char *src)
275     {
276 root 1.10 assign ((char *)&dst, src, N);
277 root 1.9 }
278    
279 root 1.17 typedef double tstamp;
280    
281     // return current time as timestampe
282     tstamp now ();
283    
284 root 1.25 int similar_direction (int a, int b);
285    
286 root 1.1 #endif
287