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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines