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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines