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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines