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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines