ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
(Generate patch)

Comparing rxvt-unicode/src/rxvtutil.h (file contents):
Revision 1.21 by root, Sun Jan 29 20:51:28 2006 UTC vs.
Revision 1.57 by sf-exg, Fri May 25 18:49:59 2012 UTC

1#ifndef RXVT_UTIL_H 1#ifndef RXVT_UTIL_H
2#define RXVT_UTIL_H 2#define RXVT_UTIL_H
3 3
4#include <new>
4#include <cstdlib> 5#include <stdlib.h>
5#include <cstring> 6#include <string.h>
7#include "ecb.h"
8#include "estl.h"
6 9
7#define PP_CONCAT_(a, b) a ## b 10// increases code size unless -fno-enforce-eh-specs
8#define PP_CONCAT(a, b) PP_CONCAT_(a, b) 11#if __GNUC__
9#define PP_STRINGIFY_(a) #a 12# define NOTHROW
10#define PP_STRINGIFY(a) PP_STRINGIFY_(a) 13# define THROW(x)
11 14#else
12extern class byteorder { 15# define NOTHROW throw()
13 static unsigned int e; // at least 32 bits 16# define THROW(x) throw x
14public: 17#endif
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 18
23// various utility functions 19// various utility functions
24template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
25template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } 20template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
26template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
27template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } 21template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
28 22
29template<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; } 23template<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; }
30template<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; } 24template<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 25
32template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 26template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); }
33 27
34// linear interpolation 28// linear interpolation
35template<typename T, typename U, typename P> 29template<typename T, typename U, typename P>
36static inline 30static inline T
37T lerp (T a, U b, P p) 31lerp (T a, U b, P p)
38{ 32{
39 return (int(a) * int(p) + int(b) * int(100 - p)) / 100; 33 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
34}
35
36// return a very temporary (and never deallocated) buffer. keep small.
37void *rxvt_temp_buf (int len);
38
39template<typename T>
40static inline T *
41rxvt_temp_buf (int len)
42{
43 return (T *)rxvt_temp_buf (len * sizeof (T));
40} 44}
41 45
42// in range including end 46// in range including end
43#define IN_RANGE_INC(val,beg,end) \ 47#define IN_RANGE_INC(val,beg,end) \
44 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 48 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
45 49
46// in range excluding end 50// in range excluding end
47#define IN_RANGE_EXC(val,beg,end) \ 51#define IN_RANGE_EXC(val,beg,end) \
48 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 52 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
49 53
54// for m >= -n, ensure remainder lies between 0..n-1
55#define MOD(m,n) (((m) + (n)) % (n))
56
50// makes dynamically allocated objects zero-initialised 57// makes dynamically allocated objects zero-initialised
51struct zero_initialized { 58struct zero_initialized
59{
52 void *operator new (size_t s); 60 void *operator new (size_t s);
53 void operator delete (void *p, size_t s); 61 void operator delete (void *p, size_t s);
54}; 62};
55 63
56/* simplevec taken (and heavily modified), from: 64struct stringvec : simplevec<char *>
57 * 65{
58 * MICO --- a free CORBA implementation 66 ~stringvec ()
59 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
60 */
61template<class T>
62struct simplevec {
63 typedef T* iterator;
64 typedef const T* const_iterator;
65 typedef unsigned long size_type;
66
67private:
68 size_type _last, _size;
69 T *_buf;
70
71public:
72 const_iterator begin () const
73 { 67 {
74 return &_buf[0]; 68 for (char **c = begin (); c != end (); c++)
69 free (*c);
75 } 70 }
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
97private:
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
127public:
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}; 71};
288
289template<class T>
290bool 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
297template<class T>
298bool 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
311template<typename T>
312struct vector : simplevec<T>
313{ };
314 72
315#if 0 73#if 0
316template<typename T> 74template<typename T>
317struct rxvt_vec : simplevec<void *> { 75struct rxvt_vec : simplevec<void *>
76{
318 typedef T *iterator; 77 typedef T *iterator;
319 78
320 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 79 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
321 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 80 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
322 void erase (int i) { erase (begin () + i); } 81 void erase (int i) { erase (begin () + i); }
326 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 85 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])); } 86 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
328}; 87};
329#endif 88#endif
330 89
331template <typename I, typename T> 90inline void *
332I find (I first, I last, const T& value) 91operator new (size_t size)
333{ 92{
334 while (first != last && *first != value) 93 // TODO: use rxvt_malloc
335 ++first; 94 return malloc (size);
95}
336 96
337 return first; 97inline void
98operator delete (void *p)
99{
100 free (p);
338} 101}
339 102
340template<typename T> 103template<typename T>
341struct auto_ptr { 104struct auto_ptr
105{
342 T *p; 106 T *p;
343 107
344 auto_ptr () : p (0) { } 108 auto_ptr () : p (0) { }
109
110 explicit
345 auto_ptr (T *a) : p (a) { } 111 auto_ptr (T *a) : p (a) { }
346 112
347 auto_ptr (auto_ptr<T> &a) 113 auto_ptr (auto_ptr &a)
348 { 114 {
349 p = a.p; 115 p = a.p;
350 a.p = 0; 116 a.p = 0;
351 } 117 }
352 118
360 ~auto_ptr () 126 ~auto_ptr ()
361 { 127 {
362 delete p; 128 delete p;
363 } 129 }
364 130
365 // void because it makes sense in our context 131 void reset (T *a)
366 void operator = (T *a)
367 { 132 {
368 delete p; 133 delete p;
369 p = a; 134 p = a;
370 } 135 }
371 136
137 // void because it makes sense in our context
372 void operator = (auto_ptr &a) 138 void operator =(auto_ptr &a)
373 { 139 {
374 *this = a.p; 140 reset (a.release ());
375 a.p = 0;
376 } 141 }
377 142
378 template<typename A> 143 template<typename A>
379 void operator = (auto_ptr<A> &a) 144 void operator =(auto_ptr<A> &a)
380 { 145 {
381 *this = a.p; 146 reset (a.release ());
382 a.p = 0;
383 } 147 }
384 148
149 T *operator ->() const { return p; }
385 operator T * () const { return p; } 150 T &operator *() const { return *p; }
386 151
152 operator T *() { return p; }
387 T *operator -> () const { return p; } 153 T *get () const { return p; }
388 T &operator * () const { return *p; }
389 154
390 T *get () 155 T *release()
391 { 156 {
392 T *r = p; 157 T *r = p;
393 p = 0; 158 p = 0;
394 return r; 159 return r;
395 } 160 }
396}; 161};
397 162
398typedef auto_ptr<char> auto_str; 163typedef auto_ptr<char> auto_str;
399 164
400struct 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.
410void *rxvt_temp_buf (int len);
411
412template<typename T>
413inline T *
414rxvt_temp_buf (int len)
415{
416 return (T *)rxvt_temp_buf (len * sizeof (T));
417}
418
419#endif 165#endif
420 166

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines