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.2 by root, Sun Aug 15 07:20:16 2004 UTC vs.
Revision 1.48 by root, Thu Jan 19 17:10:51 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines