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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines