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