ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.45
Committed: Mon May 30 18:39:04 2011 UTC (12 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.44: +1 -51 lines
Log Message:
Migrate to libecb.

File Contents

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