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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines