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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines