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.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 14# define NOTHROW
15extern class byteorder { 15# define THROW(x)
16 static unsigned int e; // at least 32 bits 16#else
17public: 17# define NOTHROW throw()
18 byteorder (); 18# define THROW(x) throw x
19 19#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 20
26// various utility functions 21// 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; } 22template<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; } 23template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
31 24
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; } 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; }
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; } 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; }
34 27
35template<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); }
36 29
37// linear interpolation 30// linear interpolation
38template<typename T, typename U, typename P> 31template<typename T, typename U, typename P>
39static inline 32static inline T
40T lerp (T a, U b, P p) 33lerp (T a, U b, P p)
41{ 34{
42 return (int(a) * int(p) + int(b) * int(100 - p)) / 100; 35 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
43} 36}
44 37
45// some bit functions, xft fuck me plenty 38// return a very temporary (and never deallocated) buffer. keep small.
46#if HAVE_GCC_BUILTINS 39void *rxvt_temp_buf (int len);
47static inline int ctz (unsigned int x) { return __builtin_ctz (x); } 40
48static inline int popcount (unsigned int x) { return __builtin_popcount (x); } 41template<typename T>
49#else 42static inline T *
50// count trailing zero bits and count # of one bits 43rxvt_temp_buf (int len)
51int ctz (unsigned int x); 44{
52int popcount (unsigned int x); 45 return (T *)rxvt_temp_buf (len * sizeof (T));
53#endif 46}
54 47
55// in range including end 48// in range including end
56#define IN_RANGE_INC(val,beg,end) \ 49#define IN_RANGE_INC(val,beg,end) \
57 ((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))
58 51
59// in range excluding end 52// in range excluding end
60#define IN_RANGE_EXC(val,beg,end) \ 53#define IN_RANGE_EXC(val,beg,end) \
61 ((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))
62 55
56// for m >= -n, ensure remainder lies between 0..n-1
57#define MOD(m,n) (((m) + (n)) % (n))
58
63// makes dynamically allocated objects zero-initialised 59// makes dynamically allocated objects zero-initialised
64struct zero_initialized { 60struct zero_initialized
61{
65 void *operator new (size_t s); 62 void *operator new (size_t s);
66 void operator delete (void *p, size_t s); 63 void operator delete (void *p, size_t s);
67}; 64};
68 65
69/* simplevec taken (and heavily modified), from: 66struct stringvec : simplevec<char *>
70 * 67{
71 * MICO --- a free CORBA implementation 68 ~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 { 69 {
87 return &_buf[0]; 70 for (char **c = begin (); c != end (); c++)
71 free (*c);
88 } 72 }
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}; 73};
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 74
328#if 0 75#if 0
329template<typename T> 76template<typename T>
330struct rxvt_vec : simplevec<void *> { 77struct rxvt_vec : simplevec<void *>
78{
331 typedef T *iterator; 79 typedef T *iterator;
332 80
333 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 81 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
334 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 82 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
335 void erase (int i) { erase (begin () + i); } 83 void erase (int i) { erase (begin () + i); }
339 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 87 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])); } 88 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
341}; 89};
342#endif 90#endif
343 91
344template <typename I, typename T> 92inline void *
345I find (I first, I last, const T& value) 93operator new (size_t size) throw (std::bad_alloc)
346{ 94{
347 while (first != last && *first != value) 95 // TODO: use rxvt_malloc
348 ++first; 96 return malloc (size);
97}
349 98
350 return first; 99inline void
100operator delete (void *p) throw ()
101{
102 free (p);
351} 103}
352 104
353template<typename T> 105template<typename T>
354struct auto_ptr { 106struct auto_ptr
107{
355 T *p; 108 T *p;
356 109
357 auto_ptr () : p (0) { } 110 auto_ptr () : p (0) { }
111
112 explicit
358 auto_ptr (T *a) : p (a) { } 113 auto_ptr (T *a) : p (a) { }
359 114
360 auto_ptr (auto_ptr<T> &a) 115 auto_ptr (auto_ptr &a)
361 { 116 {
362 p = a.p; 117 p = a.p;
363 a.p = 0; 118 a.p = 0;
364 } 119 }
365 120
373 ~auto_ptr () 128 ~auto_ptr ()
374 { 129 {
375 delete p; 130 delete p;
376 } 131 }
377 132
378 // void because it makes sense in our context 133 void reset (T *a)
379 void operator = (T *a)
380 { 134 {
381 delete p; 135 delete p;
382 p = a; 136 p = a;
383 } 137 }
384 138
139 // void because it makes sense in our context
385 void operator = (auto_ptr &a) 140 void operator =(auto_ptr &a)
386 { 141 {
387 *this = a.p; 142 reset (a.release ());
388 a.p = 0;
389 } 143 }
390 144
391 template<typename A> 145 template<typename A>
392 void operator = (auto_ptr<A> &a) 146 void operator =(auto_ptr<A> &a)
393 { 147 {
394 *this = a.p; 148 reset (a.release ());
395 a.p = 0;
396 } 149 }
397 150
151 T *operator ->() const { return p; }
398 operator T * () const { return p; } 152 T &operator *() const { return *p; }
399 153
154 operator T *() { return p; }
400 T *operator -> () const { return p; } 155 T *get () const { return p; }
401 T &operator * () const { return *p; }
402 156
403 T *get () 157 T *release()
404 { 158 {
405 T *r = p; 159 T *r = p;
406 p = 0; 160 p = 0;
407 return r; 161 return r;
408 } 162 }
409}; 163};
410 164
411typedef auto_ptr<char> auto_str; 165typedef auto_ptr<char> auto_str;
412 166
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
432#endif 167#endif
433 168

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines