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