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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines