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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines