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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines