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