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.29 by ayin, Wed Oct 31 09:55:24 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines