ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
Revision: 1.12
Committed: Tue Sep 12 21:10:32 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +6 -3 lines
Log Message:
tuning

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