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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines