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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines