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