ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.46
Committed: Thu Jun 2 13:07:28 2011 UTC (12 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-9_14, rel-9_12
Changes since 1.45: +0 -1 lines
Log Message:
Remove unneeded includes.

File Contents

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