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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines