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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines