ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.23
Committed: Sun Jan 29 22:30:21 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.22: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #ifndef RXVT_UTIL_H
2 #define RXVT_UTIL_H
3
4 #include <cstdlib>
5 #include <cstring>
6
7 #define PP_CONCAT_(a, b) a ## b
8 #define PP_CONCAT(a, b) PP_CONCAT_(a, b)
9 #define PP_STRINGIFY_(a) #a
10 #define PP_STRINGIFY(a) PP_STRINGIFY_(a)
11
12 // actually, some gcc-3.x versions work, too
13 #define HAVE_GCC_BUILTINS (__GNUC__ >= 4)
14
15 extern class byteorder {
16 static unsigned int e; // at least 32 bits
17 public:
18 byteorder ();
19
20 static bool big_endian () { return e == 0x11223344; };
21 static bool network () { return e == 0x11223344; };
22 static bool little_endian () { return e == 0x44332211; };
23 static bool vax () { return e == 0x44332211; };
24 } byteorder;
25
26 // various utility functions
27 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
28 template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
29 template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
30 template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
31
32 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; }
33 template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; }
34
35 template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
36
37 // linear interpolation
38 template<typename T, typename U, typename P>
39 static inline
40 T lerp (T a, U b, P p)
41 {
42 return (int(a) * int(p) + int(b) * int(100 - p)) / 100;
43 }
44
45 // some bit functions, xft fuck me plenty
46 #if HAVE_GCC_BUILTINS
47 static inline int ctz (unsigned int x) { return __builtin_ctz (x); }
48 static inline int popcount (unsigned int x) { return __builtin_popcount (x); }
49 #else
50 // count trailing zero bits and count # of one bits
51 int ctz (unsigned int x);
52 int popcount (unsigned int x);
53 #endif
54
55 // in range including end
56 #define IN_RANGE_INC(val,beg,end) \
57 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
58
59 // in range excluding end
60 #define IN_RANGE_EXC(val,beg,end) \
61 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
62
63 // makes dynamically allocated objects zero-initialised
64 struct zero_initialized {
65 void *operator new (size_t s);
66 void operator delete (void *p, size_t s);
67 };
68
69 /* simplevec taken (and heavily modified), from:
70 *
71 * MICO --- a free CORBA implementation
72 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
73 */
74 template<class T>
75 struct simplevec {
76 typedef T* iterator;
77 typedef const T* const_iterator;
78 typedef unsigned long size_type;
79
80 private:
81 size_type _last, _size;
82 T *_buf;
83
84 public:
85 const_iterator begin () const
86 {
87 return &_buf[0];
88 }
89 iterator begin ()
90 {
91 return &_buf[0];
92 }
93 const_iterator end () const
94 {
95 return &_buf[_last];
96 }
97 iterator end ()
98 {
99 return &_buf[_last];
100 }
101 size_type capacity () const
102 {
103 return _size;
104 }
105 size_type size () const
106 {
107 return _last;
108 }
109
110 private:
111 static T *alloc (size_type n)
112 {
113 return (T *)::operator new ((size_t) (n * sizeof (T)));
114 }
115 static void dealloc (T *buf)
116 {
117 if (buf)
118 ::operator delete (buf);
119 }
120
121 void reserve (iterator where, size_type n)
122 {
123 if (_last + n <= _size) {
124 memmove (where+n, where, (end ()-where)*sizeof (T));
125 } else {
126 size_type sz = _last+n;
127 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
128 T *nbuf = alloc (sz);
129 if (_buf) {
130 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
131 memcpy (nbuf + (where-begin ()) + n, where,
132 (end ()-where)*sizeof (T));
133 dealloc (_buf);
134 }
135 _buf = nbuf;
136 _size = sz;
137 }
138 }
139
140 public:
141 void reserve (size_type sz)
142 {
143 if (_size < sz) {
144 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
145 T *nbuf = alloc (sz);
146 if (_buf) {
147 memcpy (nbuf, begin (), size ()*sizeof (T));
148 dealloc (_buf);
149 }
150 _buf = nbuf;
151 _size = sz;
152 }
153 }
154 simplevec ()
155 : _last(0), _size(0), _buf(0)
156 {
157 }
158 simplevec (size_type n, const T& t = T ())
159 : _last(0), _size(0), _buf(0)
160 {
161 insert (begin (), n, t);
162 }
163 simplevec (const_iterator first, const_iterator last)
164 : _last(0), _size(0), _buf(0)
165 {
166 insert (begin (), first, last);
167 }
168 simplevec (const simplevec<T> &v)
169 : _last(0), _size(0), _buf(0)
170 {
171 reserve (v._last);
172 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
173 _last = v._last;
174 }
175 simplevec<T> &operator= (const simplevec<T> &v)
176 {
177 if (this != &v) {
178 _last = 0;
179 reserve (v._last);
180 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
181 _last = v._last;
182 }
183 return *this;
184 }
185 ~simplevec ()
186 {
187 dealloc (_buf);
188 }
189 const T &front () const
190 {
191 //ministl_assert (size () > 0);
192 return _buf[0];
193 }
194 T &front ()
195 {
196 //ministl_assert (size () > 0);
197 return _buf[0];
198 }
199 const T &back () const
200 {
201 //ministl_assert (size () > 0);
202 return _buf[_last-1];
203 }
204 T &back ()
205 {
206 //ministl_assert (size () > 0);
207 return _buf[_last-1];
208 }
209 bool empty () const
210 {
211 return _last == 0;
212 }
213 void clear ()
214 {
215 _last = 0;
216 }
217 void push_back (const T &t)
218 {
219 reserve (_last+1);
220 *end () = t;
221 ++_last;
222 }
223 void push_back (T &t)
224 {
225 reserve (_last+1);
226 *end () = t;
227 ++_last;
228 }
229 void pop_back ()
230 {
231 //ministl_assert (size () > 0);
232 --_last;
233 }
234 const T &operator[] (size_type idx) const
235 {
236 //ministl_assert (idx < size ());
237 return _buf[idx];
238 }
239 T &operator[] (size_type idx)
240 {
241 //ministl_assert (idx < size ());
242 return _buf[idx];
243 }
244 iterator insert (iterator pos, const T &t)
245 {
246 //ministl_assert (pos <= end ());
247 long at = pos - begin ();
248 reserve (pos, 1);
249 pos = begin ()+at;
250 *pos = t;
251 ++_last;
252 return pos;
253 }
254 iterator insert (iterator pos, const_iterator first, const_iterator last)
255 {
256 //ministl_assert (pos <= end ());
257 long n = last - first;
258 long at = pos - begin ();
259 if (n > 0) {
260 reserve (pos, n);
261 pos = begin ()+at;
262 memcpy (pos, first, (last-first)*sizeof (T));
263 _last += n;
264 }
265 return pos;
266 }
267 iterator insert (iterator pos, size_type n, const T &t)
268 {
269 //ministl_assert (pos <= end ());
270 long at = pos - begin ();
271 if (n > 0) {
272 reserve (pos, n);
273 pos = begin ()+at;
274 for (int i = 0; i < n; ++i)
275 pos[i] = t;
276 _last += n;
277 }
278 return pos;
279 }
280 void erase (iterator first, iterator last)
281 {
282 if (last != first) {
283 memmove (first, last, (end () - last) * sizeof (T));
284 _last -= last - first;
285 }
286 }
287 void erase (iterator pos)
288 {
289 if (pos != end ()) {
290 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
291 --_last;
292 }
293 }
294 void swap (simplevec<T> &t)
295 {
296 ::swap(_last, t._last);
297 ::swap(_size, t._size);
298 ::swap(_buf, t._buf);
299 }
300 };
301
302 template<class T>
303 bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
304 {
305 if (v1.size () != v2.size ())
306 return false;
307 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
308 }
309
310 template<class T>
311 bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
312 {
313 unsigned long minlast = min (v1.size (), v2.size ());
314 for (unsigned long i = 0; i < minlast; ++i) {
315 if (v1[i] < v2[i])
316 return true;
317 if (v2[i] < v1[i])
318 return false;
319 }
320 return v1.size () < v2.size ();
321 }
322
323
324 template<typename T>
325 struct vector : simplevec<T>
326 { };
327
328 #if 0
329 template<typename T>
330 struct rxvt_vec : simplevec<void *> {
331 typedef T *iterator;
332
333 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
334 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
335 void erase (int i) { erase (begin () + i); }
336 void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
337 iterator begin () const { return (iterator)simplevec<void *>::begin (); }
338 iterator end () const { return (iterator)simplevec<void *>::end (); }
339 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
340 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
341 };
342 #endif
343
344 template <typename I, typename T>
345 I find (I first, I last, const T& value)
346 {
347 while (first != last && *first != value)
348 ++first;
349
350 return first;
351 }
352
353 template<typename T>
354 struct auto_ptr {
355 T *p;
356
357 auto_ptr () : p (0) { }
358 auto_ptr (T *a) : p (a) { }
359
360 auto_ptr (auto_ptr<T> &a)
361 {
362 p = a.p;
363 a.p = 0;
364 }
365
366 template<typename A>
367 auto_ptr (auto_ptr<A> &a)
368 {
369 p = a.p;
370 a.p = 0;
371 }
372
373 ~auto_ptr ()
374 {
375 delete p;
376 }
377
378 // void because it makes sense in our context
379 void operator = (T *a)
380 {
381 delete p;
382 p = a;
383 }
384
385 void operator = (auto_ptr &a)
386 {
387 *this = a.p;
388 a.p = 0;
389 }
390
391 template<typename A>
392 void operator = (auto_ptr<A> &a)
393 {
394 *this = a.p;
395 a.p = 0;
396 }
397
398 operator T * () const { return p; }
399
400 T *operator -> () const { return p; }
401 T &operator * () const { return *p; }
402
403 T *get ()
404 {
405 T *r = p;
406 p = 0;
407 return r;
408 }
409 };
410
411 typedef auto_ptr<char> auto_str;
412
413 struct stringvec : simplevec<char *>
414 {
415 ~stringvec ()
416 {
417 for (char **c = begin (); c != end (); c++)
418 free (*c);
419 }
420 };
421
422 // return a very temporary (and never deallocated) buffer. keep small.
423 void *rxvt_temp_buf (int len);
424
425 template<typename T>
426 inline T *
427 rxvt_temp_buf (int len)
428 {
429 return (T *)rxvt_temp_buf (len * sizeof (T));
430 }
431
432 #endif
433