1 |
root |
1.1 |
#ifndef RXVT_UTIL_H |
2 |
|
|
#define RXVT_UTIL_H |
3 |
|
|
|
4 |
root |
1.55 |
#include <new> |
5 |
sf-exg |
1.47 |
#include <stdlib.h> |
6 |
|
|
#include <string.h> |
7 |
sf-exg |
1.45 |
#include "ecb.h" |
8 |
sf-exg |
1.49 |
#include "estl.h" |
9 |
root |
1.4 |
|
10 |
root |
1.58 |
#include "emman.h" |
11 |
|
|
|
12 |
root |
1.24 |
// increases code size unless -fno-enforce-eh-specs |
13 |
|
|
#if __GNUC__ |
14 |
|
|
# define NOTHROW |
15 |
|
|
# define THROW(x) |
16 |
|
|
#else |
17 |
|
|
# define NOTHROW throw() |
18 |
|
|
# define THROW(x) throw x |
19 |
|
|
#endif |
20 |
|
|
|
21 |
root |
1.16 |
// various utility functions |
22 |
sf-exg |
1.51 |
template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
23 |
|
|
template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
24 |
root |
1.13 |
|
25 |
|
|
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; } |
26 |
|
|
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; } |
27 |
|
|
|
28 |
sf-exg |
1.51 |
template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); } |
29 |
root |
1.25 |
|
30 |
root |
1.21 |
// linear interpolation |
31 |
|
|
template<typename T, typename U, typename P> |
32 |
root |
1.55 |
static inline T |
33 |
|
|
lerp (T a, U b, P p) |
34 |
root |
1.21 |
{ |
35 |
root |
1.26 |
return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
36 |
root |
1.21 |
} |
37 |
|
|
|
38 |
ayin |
1.32 |
// return a very temporary (and never deallocated) buffer. keep small. |
39 |
|
|
void *rxvt_temp_buf (int len); |
40 |
|
|
|
41 |
|
|
template<typename T> |
42 |
|
|
static inline T * |
43 |
|
|
rxvt_temp_buf (int len) |
44 |
|
|
{ |
45 |
|
|
return (T *)rxvt_temp_buf (len * sizeof (T)); |
46 |
|
|
} |
47 |
|
|
|
48 |
root |
1.11 |
// in range including end |
49 |
|
|
#define IN_RANGE_INC(val,beg,end) \ |
50 |
root |
1.9 |
((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
51 |
root |
1.1 |
|
52 |
root |
1.11 |
// in range excluding end |
53 |
|
|
#define IN_RANGE_EXC(val,beg,end) \ |
54 |
|
|
((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
55 |
|
|
|
56 |
ayin |
1.36 |
// for m >= -n, ensure remainder lies between 0..n-1 |
57 |
|
|
#define MOD(m,n) (((m) + (n)) % (n)) |
58 |
|
|
|
59 |
root |
1.16 |
// makes dynamically allocated objects zero-initialised |
60 |
root |
1.38 |
struct zero_initialized |
61 |
|
|
{ |
62 |
root |
1.4 |
void *operator new (size_t s); |
63 |
|
|
void operator delete (void *p, size_t s); |
64 |
|
|
}; |
65 |
|
|
|
66 |
sf-exg |
1.60 |
// alas new/delete cannot be specified as inline in C++11 (see 17.6.4.6) |
67 |
|
|
void *operator new (size_t s) throw (std::bad_alloc); |
68 |
|
|
void operator delete (void *p) throw (); |
69 |
|
|
|
70 |
ayin |
1.32 |
struct stringvec : simplevec<char *> |
71 |
|
|
{ |
72 |
|
|
~stringvec () |
73 |
|
|
{ |
74 |
|
|
for (char **c = begin (); c != end (); c++) |
75 |
|
|
free (*c); |
76 |
|
|
} |
77 |
|
|
}; |
78 |
|
|
|
79 |
root |
1.34 |
#if 0 |
80 |
root |
1.1 |
template<typename T> |
81 |
root |
1.38 |
struct rxvt_vec : simplevec<void *> |
82 |
|
|
{ |
83 |
root |
1.1 |
typedef T *iterator; |
84 |
|
|
|
85 |
|
|
void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
86 |
|
|
T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
87 |
|
|
void erase (int i) { erase (begin () + i); } |
88 |
|
|
void erase (iterator i) { simplevec<void *>::erase ((void **)i); } |
89 |
|
|
iterator begin () const { return (iterator)simplevec<void *>::begin (); } |
90 |
|
|
iterator end () const { return (iterator)simplevec<void *>::end (); } |
91 |
|
|
T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
92 |
|
|
const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
93 |
|
|
}; |
94 |
root |
1.34 |
#endif |
95 |
root |
1.1 |
|
96 |
sf-exg |
1.61 |
typedef estl::scoped_array<char> auto_str; |
97 |
root |
1.20 |
|
98 |
root |
1.1 |
#endif |
99 |
|
|
|