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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines