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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines