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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines