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 | #include <inttypes.h> |
5 | |
7 | |
6 | extern class byteorder { |
8 | using namespace std; |
7 | static unsigned int e; // at least 32 bits |
|
|
8 | public: |
|
|
9 | byteorder (); |
|
|
10 | |
9 | |
|
|
10 | #define ARRAY_LENGTH(v) (sizeof (v) / sizeof ((v)[0])) |
|
|
11 | |
|
|
12 | #define PP_CONCAT_(a, b) a ## b |
|
|
13 | #define PP_CONCAT(a, b) PP_CONCAT_(a, b) |
|
|
14 | #define PP_STRINGIFY_(a) #a |
|
|
15 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
|
|
16 | |
|
|
17 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
|
|
18 | |
|
|
19 | #if __GNUC__ >= 4 |
|
|
20 | # define rxvt_attribute(x) __attribute__(x) |
|
|
21 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
22 | #else |
|
|
23 | # define rxvt_attribute(x) |
|
|
24 | # define expect(expr,value) (expr) |
|
|
25 | #endif |
|
|
26 | |
|
|
27 | // put into ifs if you are very sure that the expression |
|
|
28 | // is mostly true or mostly false. note that these return |
|
|
29 | // booleans, not the expression. |
|
|
30 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
31 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
32 | |
|
|
33 | #define NORETURN rxvt_attribute ((noreturn)) |
|
|
34 | #define UNUSED rxvt_attribute ((unused)) |
|
|
35 | #define CONST rxvt_attribute ((const)) |
|
|
36 | |
|
|
37 | // increases code size unless -fno-enforce-eh-specs |
|
|
38 | #if __GNUC__ |
|
|
39 | # define NOTHROW |
|
|
40 | # define THROW(x) |
|
|
41 | #else |
|
|
42 | # define NOTHROW throw() |
|
|
43 | # define THROW(x) throw x |
|
|
44 | #endif |
|
|
45 | |
|
|
46 | namespace byteorder { |
|
|
47 | static unsigned char e () |
|
|
48 | { |
|
|
49 | const uint32_t u = 0x11223344; |
|
|
50 | return *(unsigned char *)u; |
|
|
51 | } |
|
|
52 | |
11 | static bool big_endian () { return e == 0x11223344; }; |
53 | static bool big_endian () { return e () == 0x11; }; |
12 | static bool network () { return e == 0x11223344; }; |
54 | static bool network () { return big_endian (); }; |
13 | static bool little_endian () { return e == 0x44332211; }; |
55 | static bool little_endian () { return e () == 0x44; }; |
14 | static bool vax () { return e == 0x44332211; }; |
56 | static bool vax () { return little_endian (); }; |
15 | } byteorder; |
57 | }; |
16 | |
58 | |
|
|
59 | // various utility functions |
|
|
60 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
61 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
|
|
62 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
63 | template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
|
|
64 | |
|
|
65 | template<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; } |
|
|
66 | template<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; } |
|
|
67 | |
|
|
68 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
69 | |
|
|
70 | template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); } |
|
|
71 | |
|
|
72 | // linear interpolation |
|
|
73 | template<typename T, typename U, typename P> |
|
|
74 | static inline |
|
|
75 | T lerp (T a, U b, P p) |
|
|
76 | { |
|
|
77 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
|
|
78 | } |
|
|
79 | |
17 | template<typename T, typename U> |
80 | template <typename I, typename T> |
18 | static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
81 | I find (I first, I last, const T& value) |
19 | template<typename T, typename U> |
82 | { |
20 | static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
83 | while (first != last && *first != value) |
|
|
84 | ++first; |
|
|
85 | |
|
|
86 | return first; |
|
|
87 | } |
|
|
88 | |
|
|
89 | // return a very temporary (and never deallocated) buffer. keep small. |
|
|
90 | void *rxvt_temp_buf (int len); |
|
|
91 | |
21 | template<typename T> |
92 | template<typename T> |
22 | static inline void swap (T& a, T& b) { T t=a; a=b; b=t; } |
93 | static inline T * |
|
|
94 | rxvt_temp_buf (int len) |
|
|
95 | { |
|
|
96 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
|
|
97 | } |
23 | |
98 | |
|
|
99 | // some bit functions, xft fuck me plenty |
|
|
100 | #if HAVE_GCC_BUILTINS |
|
|
101 | /* netbsd stupidly defines popcount itself and puts it into string.h */ |
|
|
102 | static inline int rxvt_ctz (unsigned int x) { return __builtin_ctz (x); } |
|
|
103 | static inline int rxvt_popcount (unsigned int x) { return __builtin_popcount (x); } |
|
|
104 | #else |
|
|
105 | // count trailing zero bits and count # of one bits |
|
|
106 | int rxvt_ctz (unsigned int x) CONST; |
|
|
107 | int rxvt_popcount (unsigned int x) CONST; |
|
|
108 | #endif |
|
|
109 | |
|
|
110 | // in range including end |
24 | #define IN_RANGE(val,beg,end) \ |
111 | #define IN_RANGE_INC(val,beg,end) \ |
25 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
112 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
26 | |
113 | |
|
|
114 | // in range excluding end |
|
|
115 | #define IN_RANGE_EXC(val,beg,end) \ |
|
|
116 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
117 | |
|
|
118 | // for m >= -n, ensure remainder lies between 0..n-1 |
|
|
119 | #define MOD(m,n) (((m) + (n)) % (n)) |
|
|
120 | |
|
|
121 | // makes dynamically allocated objects zero-initialised |
27 | struct zero_initialized { |
122 | struct zero_initialized |
|
|
123 | { |
28 | void *operator new (size_t s); |
124 | void *operator new (size_t s); |
29 | void operator delete (void *p, size_t s); |
125 | void operator delete (void *p, size_t s); |
30 | }; |
126 | }; |
31 | |
127 | |
32 | /* simplevec taken (and heavily modified), from: |
128 | /* simplevec taken (and heavily modified), from: |
33 | * |
129 | * |
34 | * MICO --- a free CORBA implementation |
130 | * MICO --- a free CORBA implementation |
35 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
131 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
36 | */ |
132 | */ |
37 | template<class T> |
133 | template<class T> |
38 | struct simplevec { |
134 | struct simplevec |
|
|
135 | { |
39 | typedef T* iterator; |
136 | typedef T* iterator; |
40 | typedef const T* const_iterator; |
137 | typedef const T* const_iterator; |
41 | typedef unsigned long size_type; |
138 | typedef unsigned long size_type; |
42 | |
139 | |
43 | private: |
140 | private: |
… | |
… | |
241 | return pos; |
338 | return pos; |
242 | } |
339 | } |
243 | void erase (iterator first, iterator last) |
340 | void erase (iterator first, iterator last) |
244 | { |
341 | { |
245 | if (last != first) { |
342 | if (last != first) { |
246 | memmove (first, last, (end ()-last)*sizeof (T)); |
343 | memmove (first, last, (end () - last) * sizeof (T)); |
247 | _last -= last - first; |
344 | _last -= last - first; |
248 | } |
345 | } |
249 | } |
346 | } |
250 | void erase (iterator pos) |
347 | void erase (iterator pos) |
251 | { |
348 | { |
252 | if (pos != end ()) { |
349 | if (pos != end ()) { |
253 | memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T)); |
350 | memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T)); |
254 | --_last; |
351 | --_last; |
255 | } |
352 | } |
256 | } |
353 | } |
257 | void swap (simplevec<T> &t) |
354 | void swap (simplevec<T> &t) |
258 | { |
355 | { |
… | |
… | |
284 | } |
381 | } |
285 | |
382 | |
286 | |
383 | |
287 | template<typename T> |
384 | template<typename T> |
288 | struct vector : simplevec<T> |
385 | struct vector : simplevec<T> |
289 | { }; |
386 | { |
|
|
387 | }; |
|
|
388 | |
|
|
389 | struct stringvec : simplevec<char *> |
|
|
390 | { |
|
|
391 | ~stringvec () |
|
|
392 | { |
|
|
393 | for (char **c = begin (); c != end (); c++) |
|
|
394 | free (*c); |
|
|
395 | } |
|
|
396 | }; |
290 | |
397 | |
291 | #if 0 |
398 | #if 0 |
292 | template<typename T> |
399 | template<typename T> |
293 | struct rxvt_vec : simplevec<void *> { |
400 | struct rxvt_vec : simplevec<void *> |
|
|
401 | { |
294 | typedef T *iterator; |
402 | typedef T *iterator; |
295 | |
403 | |
296 | void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
404 | void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
297 | T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
405 | T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
298 | void erase (int i) { erase (begin () + i); } |
406 | void erase (int i) { erase (begin () + i); } |
… | |
… | |
302 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
410 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
303 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
411 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
304 | }; |
412 | }; |
305 | #endif |
413 | #endif |
306 | |
414 | |
307 | template <typename I, typename T> |
|
|
308 | I find (I first, I last, const T& value) |
|
|
309 | { |
|
|
310 | while (first != last && *first != value) |
|
|
311 | ++first; |
|
|
312 | |
|
|
313 | return first; |
|
|
314 | } |
|
|
315 | |
|
|
316 | template<typename T> |
415 | template<typename T> |
317 | struct auto_ptr { |
416 | struct auto_ptr |
|
|
417 | { |
318 | T *p; |
418 | T *p; |
319 | |
419 | |
320 | auto_ptr () : p (0) { } |
420 | auto_ptr () : p (0) { } |
321 | auto_ptr (T *a) : p (a) { } |
421 | auto_ptr (T *a) : p (a) { } |
322 | |
422 | |
… | |
… | |
371 | } |
471 | } |
372 | }; |
472 | }; |
373 | |
473 | |
374 | typedef auto_ptr<char> auto_str; |
474 | typedef auto_ptr<char> auto_str; |
375 | |
475 | |
376 | struct stringvec : simplevec<char *> |
|
|
377 | { |
|
|
378 | ~stringvec () |
|
|
379 | { |
|
|
380 | for (char **c = begin (); c != end (); c++) |
|
|
381 | delete [] *c; |
|
|
382 | } |
|
|
383 | }; |
|
|
384 | |
|
|
385 | #endif |
476 | #endif |
386 | |
477 | |