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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines