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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines