1 | #ifndef RXVT_UTIL_H |
1 | #ifndef RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
3 | |
3 | |
4 | extern class byteorder { |
4 | #include <new> |
5 | static unsigned int e; // at least 32 bits |
5 | #include <stdlib.h> |
6 | public: |
6 | #include <string.h> |
7 | byteorder (); |
7 | #include "ecb.h" |
|
|
8 | #include "estl.h" |
8 | |
9 | |
9 | static bool big_endian () { return e == 0x11223344; }; |
10 | // increases code size unless -fno-enforce-eh-specs |
10 | static bool network () { return e == 0x11223344; }; |
11 | #if __GNUC__ |
11 | static bool little_endian () { return e == 0x44332211; }; |
12 | # define NOTHROW |
12 | static bool vax () { return e == 0x44332211; }; |
13 | # define THROW(x) |
13 | } byteorder; |
14 | #else |
|
|
15 | # define NOTHROW throw() |
|
|
16 | # define THROW(x) throw x |
|
|
17 | #endif |
14 | |
18 | |
15 | template<typename T, typename U> |
19 | // various utility functions |
16 | static inline T min (T a, U b) { return a < b ? a : (T)b; } |
20 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
17 | template<typename T, typename U> |
21 | template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
18 | static inline T max (T a, U b) { return a > b ? a : (T)b; } |
|
|
19 | |
22 | |
20 | #include "simplevec.h" |
23 | 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; } |
|
|
24 | 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; } |
|
|
25 | |
|
|
26 | template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); } |
|
|
27 | |
|
|
28 | // linear interpolation |
|
|
29 | template<typename T, typename U, typename P> |
|
|
30 | static inline T |
|
|
31 | lerp (T a, U b, P p) |
|
|
32 | { |
|
|
33 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
|
|
34 | } |
|
|
35 | |
|
|
36 | // return a very temporary (and never deallocated) buffer. keep small. |
|
|
37 | void *rxvt_temp_buf (int len); |
21 | |
38 | |
22 | template<typename T> |
39 | template<typename T> |
|
|
40 | static inline T * |
|
|
41 | rxvt_temp_buf (int len) |
|
|
42 | { |
|
|
43 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
|
|
44 | } |
|
|
45 | |
|
|
46 | // in range including end |
|
|
47 | #define IN_RANGE_INC(val,beg,end) \ |
|
|
48 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
|
|
49 | |
|
|
50 | // in range excluding end |
|
|
51 | #define IN_RANGE_EXC(val,beg,end) \ |
|
|
52 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
53 | |
|
|
54 | // for m >= -n, ensure remainder lies between 0..n-1 |
|
|
55 | #define MOD(m,n) (((m) + (n)) % (n)) |
|
|
56 | |
|
|
57 | // makes dynamically allocated objects zero-initialised |
|
|
58 | struct zero_initialized |
|
|
59 | { |
|
|
60 | void *operator new (size_t s); |
|
|
61 | void operator delete (void *p, size_t s); |
|
|
62 | }; |
|
|
63 | |
23 | struct vector : simplevec<T> |
64 | struct stringvec : simplevec<char *> |
24 | { }; |
65 | { |
|
|
66 | ~stringvec () |
|
|
67 | { |
|
|
68 | for (char **c = begin (); c != end (); c++) |
|
|
69 | free (*c); |
|
|
70 | } |
|
|
71 | }; |
25 | |
72 | |
26 | #if 0 |
73 | #if 0 |
27 | template<typename T> |
74 | template<typename T> |
28 | struct rxvt_vec : simplevec<void *> { |
75 | struct rxvt_vec : simplevec<void *> |
|
|
76 | { |
29 | typedef T *iterator; |
77 | typedef T *iterator; |
30 | |
78 | |
31 | void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
79 | void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
32 | T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
80 | T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
33 | void erase (int i) { erase (begin () + i); } |
81 | void erase (int i) { erase (begin () + i); } |
… | |
… | |
37 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
85 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
38 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
86 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
39 | }; |
87 | }; |
40 | #endif |
88 | #endif |
41 | |
89 | |
42 | template <typename I, typename T> |
90 | inline void * |
43 | I find (I first, I last, const T& value) |
91 | operator new (size_t size) |
44 | { |
92 | { |
45 | while (first != last && *first != value) |
93 | // TODO: use rxvt_malloc |
46 | ++first; |
94 | return malloc (size); |
|
|
95 | } |
47 | |
96 | |
48 | return first; |
97 | inline void |
|
|
98 | operator delete (void *p) |
|
|
99 | { |
|
|
100 | free (p); |
49 | } |
101 | } |
50 | |
102 | |
51 | template<typename T> |
103 | template<typename T> |
52 | struct auto_ptr { |
104 | struct auto_ptr |
|
|
105 | { |
53 | T *p; |
106 | T *p; |
54 | |
107 | |
55 | auto_ptr () : p (0) { } |
108 | auto_ptr () : p (0) { } |
|
|
109 | |
|
|
110 | explicit |
56 | auto_ptr (T *a) : p (a) { } |
111 | auto_ptr (T *a) : p (a) { } |
57 | |
112 | |
58 | auto_ptr (auto_ptr<T> &a) |
113 | auto_ptr (auto_ptr<T> &a) |
59 | { |
114 | { |
60 | p = a.p; |
115 | p = a.p; |
… | |
… | |
71 | ~auto_ptr () |
126 | ~auto_ptr () |
72 | { |
127 | { |
73 | delete p; |
128 | delete p; |
74 | } |
129 | } |
75 | |
130 | |
76 | // void because it makes sense in our context |
131 | void reset (T *a) |
77 | void operator = (T *a) |
|
|
78 | { |
132 | { |
79 | delete p; |
133 | delete p; |
80 | p = a; |
134 | p = a; |
81 | } |
135 | } |
82 | |
136 | |
|
|
137 | // void because it makes sense in our context |
83 | void operator = (auto_ptr &a) |
138 | void operator =(auto_ptr &a) |
84 | { |
139 | { |
85 | *this = a.p; |
140 | *this = a.p; |
86 | a.p = 0; |
141 | a.p = 0; |
87 | } |
142 | } |
88 | |
143 | |
89 | template<typename A> |
144 | template<typename A> |
90 | void operator = (auto_ptr<A> &a) |
145 | void operator =(auto_ptr<A> &a) |
91 | { |
146 | { |
92 | *this = a.p; |
147 | *this = a.p; |
93 | a.p = 0; |
148 | a.p = 0; |
94 | } |
149 | } |
95 | |
150 | |
|
|
151 | T *operator ->() const { return p; } |
96 | operator T * () const { return p; } |
152 | T &operator *() const { return *p; } |
97 | |
153 | |
|
|
154 | operator T *() { return p; } |
98 | T *operator -> () const { return p; } |
155 | T *get () const { return p; } |
99 | T &operator * () const { return *p; } |
|
|
100 | |
156 | |
101 | T *get () |
157 | T *release() |
102 | { |
158 | { |
103 | T *r = p; |
159 | T *r = p; |
104 | p = 0; |
160 | p = 0; |
105 | return r; |
161 | return r; |
106 | } |
162 | } |
107 | }; |
163 | }; |
108 | |
164 | |
109 | typedef auto_ptr<char> auto_str; |
165 | typedef auto_ptr<char> auto_str; |
110 | |
166 | |
111 | struct stringvec : simplevec<char *> |
|
|
112 | { |
|
|
113 | ~stringvec () |
|
|
114 | { |
|
|
115 | for (char **c = begin (); c != end (); c++) |
|
|
116 | delete [] *c; |
|
|
117 | } |
|
|
118 | }; |
|
|
119 | |
|
|
120 | struct zero_initialized { |
|
|
121 | void *operator new (size_t s); |
|
|
122 | void operator delete (void *p, size_t s); |
|
|
123 | }; |
|
|
124 | |
|
|
125 | #endif |
167 | #endif |
126 | |
168 | |