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, 5 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

# User Rev Content
1 root 1.1 #ifndef RXVT_UTIL_H
2     #define RXVT_UTIL_H
3    
4 sf-exg 1.47 #include <stdlib.h>
5     #include <string.h>
6 sf-exg 1.45 #include "ecb.h"
7 root 1.4
8 root 1.24 // 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 root 1.16 // various utility functions
18 root 1.13 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 root 1.14 template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
27 root 1.8
28 root 1.25 template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); }
29    
30 root 1.21 // linear interpolation
31     template<typename T, typename U, typename P>
32     static inline
33     T lerp (T a, U b, P p)
34     {
35 root 1.26 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
36 root 1.21 }
37    
38 ayin 1.32 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 root 1.11 // in range including end
58     #define IN_RANGE_INC(val,beg,end) \
59 root 1.9 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
60 root 1.1
61 root 1.11 // 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 ayin 1.36 // for m >= -n, ensure remainder lies between 0..n-1
66     #define MOD(m,n) (((m) + (n)) % (n))
67    
68 root 1.16 // makes dynamically allocated objects zero-initialised
69 root 1.38 struct zero_initialized
70     {
71 root 1.4 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 ayin 1.29 *
77 root 1.4 * MICO --- a free CORBA implementation
78     * Copyright (C) 1997-98 Kay Roemer & Arno Puder
79     */
80     template<class T>
81 root 1.38 struct simplevec
82     {
83 root 1.4 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 root 1.8 return &_buf[0];
95 root 1.4 }
96     iterator begin ()
97     {
98 root 1.8 return &_buf[0];
99 root 1.4 }
100     const_iterator end () const
101     {
102 root 1.8 return &_buf[_last];
103 root 1.4 }
104     iterator end ()
105     {
106 root 1.8 return &_buf[_last];
107 root 1.4 }
108     size_type capacity () const
109     {
110 root 1.8 return _size;
111 root 1.4 }
112     size_type size () const
113     {
114 root 1.8 return _last;
115 root 1.4 }
116    
117     private:
118     static T *alloc (size_type n)
119     {
120 root 1.8 return (T *)::operator new ((size_t) (n * sizeof (T)));
121 root 1.4 }
122     static void dealloc (T *buf)
123     {
124 root 1.8 if (buf)
125     ::operator delete (buf);
126 root 1.4 }
127    
128     void reserve (iterator where, size_type n)
129     {
130 root 1.8 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 root 1.4 }
146    
147     public:
148     void reserve (size_type sz)
149     {
150 root 1.8 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 root 1.4 }
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 root 1.8 insert (begin (), n, t);
169 root 1.4 }
170     simplevec (const_iterator first, const_iterator last)
171     : _last(0), _size(0), _buf(0)
172     {
173 root 1.8 insert (begin (), first, last);
174 root 1.4 }
175     simplevec (const simplevec<T> &v)
176     : _last(0), _size(0), _buf(0)
177     {
178 root 1.8 reserve (v._last);
179     memcpy (_buf, v.begin (), v.size ()*sizeof (T));
180     _last = v._last;
181 root 1.4 }
182     simplevec<T> &operator= (const simplevec<T> &v)
183     {
184 root 1.8 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 root 1.4 return *this;
191     }
192     ~simplevec ()
193     {
194 root 1.8 dealloc (_buf);
195 root 1.4 }
196     const T &front () const
197     {
198 root 1.8 //ministl_assert (size () > 0);
199     return _buf[0];
200 root 1.4 }
201     T &front ()
202     {
203 root 1.8 //ministl_assert (size () > 0);
204     return _buf[0];
205 root 1.4 }
206     const T &back () const
207     {
208 root 1.8 //ministl_assert (size () > 0);
209     return _buf[_last-1];
210 root 1.4 }
211     T &back ()
212     {
213 root 1.8 //ministl_assert (size () > 0);
214     return _buf[_last-1];
215 root 1.4 }
216     bool empty () const
217     {
218 root 1.8 return _last == 0;
219 root 1.4 }
220     void clear ()
221     {
222 root 1.8 _last = 0;
223 root 1.4 }
224     void push_back (const T &t)
225     {
226 root 1.8 reserve (_last+1);
227     *end () = t;
228     ++_last;
229 root 1.4 }
230     void push_back (T &t)
231     {
232 root 1.8 reserve (_last+1);
233     *end () = t;
234     ++_last;
235 root 1.4 }
236     void pop_back ()
237     {
238 root 1.8 //ministl_assert (size () > 0);
239     --_last;
240 root 1.4 }
241     const T &operator[] (size_type idx) const
242     {
243 root 1.8 //ministl_assert (idx < size ());
244     return _buf[idx];
245 root 1.4 }
246     T &operator[] (size_type idx)
247     {
248 root 1.8 //ministl_assert (idx < size ());
249     return _buf[idx];
250 root 1.4 }
251     iterator insert (iterator pos, const T &t)
252     {
253 root 1.8 //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 root 1.4 }
261     iterator insert (iterator pos, const_iterator first, const_iterator last)
262     {
263     //ministl_assert (pos <= end ());
264 root 1.8 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 root 1.4 }
274     iterator insert (iterator pos, size_type n, const T &t)
275     {
276     //ministl_assert (pos <= end ());
277 root 1.8 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 root 1.4 }
287     void erase (iterator first, iterator last)
288     {
289 root 1.8 if (last != first) {
290 root 1.18 memmove (first, last, (end () - last) * sizeof (T));
291 root 1.8 _last -= last - first;
292     }
293 root 1.4 }
294     void erase (iterator pos)
295     {
296     if (pos != end ()) {
297 root 1.18 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
298 root 1.4 --_last;
299     }
300     }
301 root 1.8 void swap (simplevec<T> &t)
302     {
303     ::swap(_last, t._last);
304     ::swap(_size, t._size);
305     ::swap(_buf, t._buf);
306     }
307 root 1.4 };
308    
309     template<class T>
310     bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
311     {
312     if (v1.size () != v2.size ())
313 root 1.8 return false;
314 root 1.4 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 root 1.8 return true;
324     if (v2[i] < v1[i])
325     return false;
326 root 1.4 }
327     return v1.size () < v2.size ();
328     }
329    
330 root 1.1
331     template<typename T>
332     struct vector : simplevec<T>
333 root 1.35 {
334     };
335 root 1.1
336 ayin 1.32 struct stringvec : simplevec<char *>
337     {
338     ~stringvec ()
339     {
340     for (char **c = begin (); c != end (); c++)
341     free (*c);
342     }
343     };
344    
345 root 1.34 #if 0
346 root 1.1 template<typename T>
347 root 1.38 struct rxvt_vec : simplevec<void *>
348     {
349 root 1.1 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 root 1.34 #endif
361 root 1.1
362     template<typename T>
363 root 1.38 struct auto_ptr
364     {
365 root 1.1 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 root 1.20
423 root 1.1 #endif
424