ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.38
Committed: Wed Nov 5 14:43:54 2008 UTC (15 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-9_06
Changes since 1.37: +8 -4 lines
Log Message:
minor overlay refactoring, syntax changes

File Contents

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