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.17 by root, Thu Jan 12 00:35:39 2006 UTC vs.
Revision 1.51 by sf-exg, Fri Apr 13 11:46:37 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)
10 13#else
11extern class byteorder { 14# define NOTHROW throw()
12 static unsigned int e; // at least 32 bits 15# define THROW(x) throw x
13public: 16#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 17
22// various utility functions 18// 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; } 19template<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; } 20template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
27 21
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; } 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; }
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; } 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; }
30 24
31template<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}
32 44
33// in range including end 45// in range including end
34#define IN_RANGE_INC(val,beg,end) \ 46#define IN_RANGE_INC(val,beg,end) \
35 ((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))
36 48
37// in range excluding end 49// in range excluding end
38#define IN_RANGE_EXC(val,beg,end) \ 50#define IN_RANGE_EXC(val,beg,end) \
39 ((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))
40 52
53// for m >= -n, ensure remainder lies between 0..n-1
54#define MOD(m,n) (((m) + (n)) % (n))
55
41// makes dynamically allocated objects zero-initialised 56// makes dynamically allocated objects zero-initialised
42struct zero_initialized { 57struct zero_initialized
58{
43 void *operator new (size_t s); 59 void *operator new (size_t s);
44 void operator delete (void *p, size_t s); 60 void operator delete (void *p, size_t s);
45}; 61};
46 62
47/* simplevec taken (and heavily modified), from: 63struct stringvec : simplevec<char *>
48 * 64{
49 * MICO --- a free CORBA implementation 65 ~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 { 66 {
65 return &_buf[0]; 67 for (char **c = begin (); c != end (); c++)
68 free (*c);
66 } 69 }
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}; 70};
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 71
306#if 0 72#if 0
307template<typename T> 73template<typename T>
308struct rxvt_vec : simplevec<void *> { 74struct rxvt_vec : simplevec<void *>
75{
309 typedef T *iterator; 76 typedef T *iterator;
310 77
311 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 78 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
312 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 79 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
313 void erase (int i) { erase (begin () + i); } 80 void erase (int i) { erase (begin () + i); }
317 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 84 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])); } 85 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
319}; 86};
320#endif 87#endif
321 88
322template <typename I, typename T> 89template<typename T>
323I find (I first, I last, const T& value) 90struct auto_ptr
324{ 91{
325 while (first != last && *first != value)
326 ++first;
327
328 return first;
329}
330
331template<typename T>
332struct auto_ptr {
333 T *p; 92 T *p;
334 93
335 auto_ptr () : p (0) { } 94 auto_ptr () : p (0) { }
336 auto_ptr (T *a) : p (a) { } 95 auto_ptr (T *a) : p (a) { }
337 96
348 a.p = 0; 107 a.p = 0;
349 } 108 }
350 109
351 ~auto_ptr () 110 ~auto_ptr ()
352 { 111 {
353 delete p; 112 free (p);
354 } 113 }
355 114
356 // void because it makes sense in our context 115 // void because it makes sense in our context
357 void operator = (T *a) 116 void operator = (T *a)
358 { 117 {
359 delete p; 118 free (p);
360 p = a; 119 p = a;
361 } 120 }
362 121
363 void operator = (auto_ptr &a) 122 void operator = (auto_ptr &a)
364 { 123 {
386 } 145 }
387}; 146};
388 147
389typedef auto_ptr<char> auto_str; 148typedef auto_ptr<char> auto_str;
390 149
391struct stringvec : simplevec<char *>
392{
393 ~stringvec ()
394 {
395 for (char **c = begin (); c != end (); c++)
396 delete [] *c;
397 }
398};
399
400#endif 150#endif
401 151

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines