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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines