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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines