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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines