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.28 by ayin, Tue Jun 26 10:46:28 2007 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
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
5 37
6extern class byteorder { 38extern class byteorder {
7 static unsigned int e; // at least 32 bits 39 static unsigned int e; // at least 32 bits
8public: 40public:
9 byteorder (); 41 byteorder ();
12 static bool network () { return e == 0x11223344; }; 44 static bool network () { return e == 0x11223344; };
13 static bool little_endian () { return e == 0x44332211; }; 45 static bool little_endian () { return e == 0x44332211; };
14 static bool vax () { return e == 0x44332211; }; 46 static bool vax () { return e == 0x44332211; };
15} byteorder; 47} byteorder;
16 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
17template<typename T, typename U> 63template<typename T, typename U, typename P>
18static inline T min (T a, U b) { return a < b ? a : (T)b; } 64static inline
19template<typename T, typename U> 65T lerp (T a, U b, P p)
20static 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}
21 69
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
22struct zero_initialized { 89struct zero_initialized {
23 void *operator new (size_t s); 90 void *operator new (size_t s);
24 void operator delete (void *p, size_t s); 91 void operator delete (void *p, size_t s);
25}; 92};
26 93
40 T *_buf; 107 T *_buf;
41 108
42public: 109public:
43 const_iterator begin () const 110 const_iterator begin () const
44 { 111 {
45 return &_buf[0]; 112 return &_buf[0];
46 } 113 }
47 iterator begin () 114 iterator begin ()
48 { 115 {
49 return &_buf[0]; 116 return &_buf[0];
50 } 117 }
51 const_iterator end () const 118 const_iterator end () const
52 { 119 {
53 return &_buf[_last]; 120 return &_buf[_last];
54 } 121 }
55 iterator end () 122 iterator end ()
56 { 123 {
57 return &_buf[_last]; 124 return &_buf[_last];
58 } 125 }
59 size_type capacity () const 126 size_type capacity () const
60 { 127 {
61 return _size; 128 return _size;
62 } 129 }
63 size_type size () const 130 size_type size () const
64 { 131 {
65 return _last; 132 return _last;
66 } 133 }
67 134
68private: 135private:
69 static T *alloc (size_type n) 136 static T *alloc (size_type n)
70 { 137 {
71 return (T *)::operator new ((size_t) (n * sizeof (T))); 138 return (T *)::operator new ((size_t) (n * sizeof (T)));
72 } 139 }
73 static void dealloc (T *buf) 140 static void dealloc (T *buf)
74 { 141 {
75 if (buf) 142 if (buf)
76 ::operator delete (buf); 143 ::operator delete (buf);
77 } 144 }
78 145
79 void reserve (iterator where, size_type n) 146 void reserve (iterator where, size_type n)
80 { 147 {
81 if (_last + n <= _size) { 148 if (_last + n <= _size) {
82 memmove (where+n, where, (end ()-where)*sizeof (T)); 149 memmove (where+n, where, (end ()-where)*sizeof (T));
83 } else { 150 } else {
84 size_type sz = _last+n; 151 size_type sz = _last+n;
85 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); 152 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
86 T *nbuf = alloc (sz); 153 T *nbuf = alloc (sz);
87 if (_buf) { 154 if (_buf) {
88 memcpy (nbuf, begin (), (where-begin ())*sizeof (T)); 155 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
89 memcpy (nbuf + (where-begin ()) + n, where, 156 memcpy (nbuf + (where-begin ()) + n, where,
90 (end ()-where)*sizeof (T)); 157 (end ()-where)*sizeof (T));
91 dealloc (_buf); 158 dealloc (_buf);
92 } 159 }
93 _buf = nbuf; 160 _buf = nbuf;
94 _size = sz; 161 _size = sz;
95 } 162 }
96 } 163 }
97 164
98public: 165public:
99 void reserve (size_type sz) 166 void reserve (size_type sz)
100 { 167 {
101 if (_size < sz) { 168 if (_size < sz) {
102 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); 169 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
103 T *nbuf = alloc (sz); 170 T *nbuf = alloc (sz);
104 if (_buf) { 171 if (_buf) {
105 memcpy (nbuf, begin (), size ()*sizeof (T)); 172 memcpy (nbuf, begin (), size ()*sizeof (T));
106 dealloc (_buf); 173 dealloc (_buf);
107 } 174 }
108 _buf = nbuf; 175 _buf = nbuf;
109 _size = sz; 176 _size = sz;
110 } 177 }
111 } 178 }
112 simplevec () 179 simplevec ()
113 : _last(0), _size(0), _buf(0) 180 : _last(0), _size(0), _buf(0)
114 { 181 {
115 } 182 }
116 simplevec (size_type n, const T& t = T ()) 183 simplevec (size_type n, const T& t = T ())
117 : _last(0), _size(0), _buf(0) 184 : _last(0), _size(0), _buf(0)
118 { 185 {
119 insert (begin (), n, t); 186 insert (begin (), n, t);
120 } 187 }
121 simplevec (const_iterator first, const_iterator last) 188 simplevec (const_iterator first, const_iterator last)
122 : _last(0), _size(0), _buf(0) 189 : _last(0), _size(0), _buf(0)
123 { 190 {
124 insert (begin (), first, last); 191 insert (begin (), first, last);
125 } 192 }
126 simplevec (const simplevec<T> &v) 193 simplevec (const simplevec<T> &v)
127 : _last(0), _size(0), _buf(0) 194 : _last(0), _size(0), _buf(0)
128 { 195 {
129 reserve (v._last); 196 reserve (v._last);
130 memcpy (_buf, v.begin (), v.size ()*sizeof (T)); 197 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
131 _last = v._last; 198 _last = v._last;
132 } 199 }
133 simplevec<T> &operator= (const simplevec<T> &v) 200 simplevec<T> &operator= (const simplevec<T> &v)
134 { 201 {
135 if (this != &v) { 202 if (this != &v) {
136 _last = 0; 203 _last = 0;
137 reserve (v._last); 204 reserve (v._last);
138 memcpy (_buf, v.begin (), v.size ()*sizeof (T)); 205 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
139 _last = v._last; 206 _last = v._last;
140 } 207 }
141 return *this; 208 return *this;
142 } 209 }
143 ~simplevec () 210 ~simplevec ()
144 { 211 {
145 dealloc (_buf); 212 dealloc (_buf);
146 } 213 }
147 const T &front () const 214 const T &front () const
148 { 215 {
149 //ministl_assert (size () > 0); 216 //ministl_assert (size () > 0);
150 return _buf[0]; 217 return _buf[0];
151 } 218 }
152 T &front () 219 T &front ()
153 { 220 {
154 //ministl_assert (size () > 0); 221 //ministl_assert (size () > 0);
155 return _buf[0]; 222 return _buf[0];
156 } 223 }
157 const T &back () const 224 const T &back () const
158 { 225 {
159 //ministl_assert (size () > 0); 226 //ministl_assert (size () > 0);
160 return _buf[_last-1]; 227 return _buf[_last-1];
161 } 228 }
162 T &back () 229 T &back ()
163 { 230 {
164 //ministl_assert (size () > 0); 231 //ministl_assert (size () > 0);
165 return _buf[_last-1]; 232 return _buf[_last-1];
166 } 233 }
167 bool empty () const 234 bool empty () const
168 { 235 {
169 return _last == 0; 236 return _last == 0;
170 } 237 }
171 void clear () 238 void clear ()
172 { 239 {
173 _last = 0; 240 _last = 0;
174 } 241 }
175 void push_back (const T &t) 242 void push_back (const T &t)
176 { 243 {
177 reserve (_last+1); 244 reserve (_last+1);
178 *end () = t; 245 *end () = t;
179 ++_last; 246 ++_last;
180 } 247 }
181 void push_back (T &t) 248 void push_back (T &t)
182 { 249 {
183 reserve (_last+1); 250 reserve (_last+1);
184 *end () = t; 251 *end () = t;
185 ++_last; 252 ++_last;
186 } 253 }
187 void pop_back () 254 void pop_back ()
188 { 255 {
189 //ministl_assert (size () > 0); 256 //ministl_assert (size () > 0);
190 --_last; 257 --_last;
191 } 258 }
192 const T &operator[] (size_type idx) const 259 const T &operator[] (size_type idx) const
193 { 260 {
194 //ministl_assert (idx < size ()); 261 //ministl_assert (idx < size ());
195 return _buf[idx]; 262 return _buf[idx];
196 } 263 }
197 T &operator[] (size_type idx) 264 T &operator[] (size_type idx)
198 { 265 {
199 //ministl_assert (idx < size ()); 266 //ministl_assert (idx < size ());
200 return _buf[idx]; 267 return _buf[idx];
201 } 268 }
202 iterator insert (iterator pos, const T &t) 269 iterator insert (iterator pos, const T &t)
203 { 270 {
204 //ministl_assert (pos <= end ()); 271 //ministl_assert (pos <= end ());
205 long at = pos - begin (); 272 long at = pos - begin ();
206 reserve (pos, 1); 273 reserve (pos, 1);
207 pos = begin ()+at; 274 pos = begin ()+at;
208 *pos = t; 275 *pos = t;
209 ++_last; 276 ++_last;
210 return pos; 277 return pos;
211 } 278 }
212 iterator insert (iterator pos, const_iterator first, const_iterator last) 279 iterator insert (iterator pos, const_iterator first, const_iterator last)
213 { 280 {
214 //ministl_assert (pos <= end ()); 281 //ministl_assert (pos <= end ());
215 long n = last - first; 282 long n = last - first;
216 long at = pos - begin (); 283 long at = pos - begin ();
217 if (n > 0) { 284 if (n > 0) {
218 reserve (pos, n); 285 reserve (pos, n);
219 pos = begin ()+at; 286 pos = begin ()+at;
220 memcpy (pos, first, (last-first)*sizeof (T)); 287 memcpy (pos, first, (last-first)*sizeof (T));
221 _last += n; 288 _last += n;
222 } 289 }
223 return pos; 290 return pos;
224 } 291 }
225 iterator insert (iterator pos, size_type n, const T &t) 292 iterator insert (iterator pos, size_type n, const T &t)
226 { 293 {
227 //ministl_assert (pos <= end ()); 294 //ministl_assert (pos <= end ());
228 long at = pos - begin (); 295 long at = pos - begin ();
229 if (n > 0) { 296 if (n > 0) {
230 reserve (pos, n); 297 reserve (pos, n);
231 pos = begin ()+at; 298 pos = begin ()+at;
232 for (int i = 0; i < n; ++i) 299 for (int i = 0; i < n; ++i)
233 pos[i] = t; 300 pos[i] = t;
234 _last += n; 301 _last += n;
235 } 302 }
236 return pos; 303 return pos;
237 } 304 }
238 void erase (iterator first, iterator last) 305 void erase (iterator first, iterator last)
239 { 306 {
240 if (last != first) { 307 if (last != first) {
241 memmove (first, last, (end ()-last)*sizeof (T)); 308 memmove (first, last, (end () - last) * sizeof (T));
242 _last -= last - first; 309 _last -= last - first;
243 } 310 }
244 } 311 }
245 void erase (iterator pos) 312 void erase (iterator pos)
246 { 313 {
247 if (pos != end ()) { 314 if (pos != end ()) {
248 memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T)); 315 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
249 --_last; 316 --_last;
250 } 317 }
318 }
319 void swap (simplevec<T> &t)
320 {
321 ::swap(_last, t._last);
322 ::swap(_size, t._size);
323 ::swap(_buf, t._buf);
251 } 324 }
252}; 325};
253 326
254template<class T> 327template<class T>
255bool operator== (const simplevec<T> &v1, const simplevec<T> &v2) 328bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
256{ 329{
257 if (v1.size () != v2.size ()) 330 if (v1.size () != v2.size ())
258 return false; 331 return false;
259 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T)); 332 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
260} 333}
261 334
262template<class T> 335template<class T>
263bool operator< (const simplevec<T> &v1, const simplevec<T> &v2) 336bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
264{ 337{
265 unsigned long minlast = min (v1.size (), v2.size ()); 338 unsigned long minlast = min (v1.size (), v2.size ());
266 for (unsigned long i = 0; i < minlast; ++i) { 339 for (unsigned long i = 0; i < minlast; ++i) {
267 if (v1[i] < v2[i]) 340 if (v1[i] < v2[i])
268 return true; 341 return true;
269 if (v2[i] < v1[i]) 342 if (v2[i] < v1[i])
270 return false; 343 return false;
271 } 344 }
272 return v1.size () < v2.size (); 345 return v1.size () < v2.size ();
273} 346}
274 347
275 348
365struct stringvec : simplevec<char *> 438struct stringvec : simplevec<char *>
366{ 439{
367 ~stringvec () 440 ~stringvec ()
368 { 441 {
369 for (char **c = begin (); c != end (); c++) 442 for (char **c = begin (); c != end (); c++)
370 delete [] *c; 443 free (*c);
371 } 444 }
372}; 445};
373 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
374#endif 457#endif
375 458

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines