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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines