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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines