ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.48
Committed: Thu Jan 19 17:10:51 2012 UTC (12 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rxvt-unicode-rel-9_15
Changes since 1.47: +0 -2 lines
Log Message:
*** empty log message ***

File Contents

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