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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines