1 | #ifndef RXVT_UTIL_H |
1 | #ifndef RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
3 | |
3 | |
4 | #include <new> |
|
|
5 | #include <stdlib.h> |
4 | #include <stdlib.h> |
6 | #include <string.h> |
5 | #include <string.h> |
|
|
6 | |
|
|
7 | #define ECB_NO_THREADS 1 |
7 | #include "ecb.h" |
8 | #include "ecb.h" |
|
|
9 | |
8 | #include "estl.h" |
10 | #include "estl.h" |
9 | |
11 | |
10 | #include "emman.h" |
12 | #include "emman.h" |
11 | |
13 | |
12 | // increases code size unless -fno-enforce-eh-specs |
14 | // increases code size unless -fno-enforce-eh-specs |
… | |
… | |
22 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
24 | 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; } |
25 | template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
24 | |
26 | |
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; } |
27 | 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; } |
28 | 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 | template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); } |
|
|
29 | |
29 | |
30 | // linear interpolation |
30 | // linear interpolation |
31 | template<typename T, typename U, typename P> |
31 | template<typename T, typename U, typename P> |
32 | static inline T |
32 | static inline T |
33 | lerp (T a, U b, P p) |
33 | lerp (T a, U b, P p) |
… | |
… | |
87 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
87 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
88 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
88 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
89 | }; |
89 | }; |
90 | #endif |
90 | #endif |
91 | |
91 | |
92 | inline void * |
92 | typedef estl::scoped_array<char> auto_str; |
93 | operator new (size_t size) throw (std::bad_alloc) |
|
|
94 | { |
|
|
95 | // TODO: use rxvt_malloc |
|
|
96 | return malloc (size); |
|
|
97 | } |
|
|
98 | |
|
|
99 | inline void |
|
|
100 | operator delete (void *p) throw () |
|
|
101 | { |
|
|
102 | free (p); |
|
|
103 | } |
|
|
104 | |
|
|
105 | template<typename T> |
|
|
106 | struct auto_ptr |
|
|
107 | { |
|
|
108 | T *p; |
|
|
109 | |
|
|
110 | auto_ptr () : p (0) { } |
|
|
111 | |
|
|
112 | explicit |
|
|
113 | auto_ptr (T *a) : p (a) { } |
|
|
114 | |
|
|
115 | auto_ptr (auto_ptr &a) |
|
|
116 | { |
|
|
117 | p = a.p; |
|
|
118 | a.p = 0; |
|
|
119 | } |
|
|
120 | |
|
|
121 | template<typename A> |
|
|
122 | auto_ptr (auto_ptr<A> &a) |
|
|
123 | { |
|
|
124 | p = a.p; |
|
|
125 | a.p = 0; |
|
|
126 | } |
|
|
127 | |
|
|
128 | ~auto_ptr () |
|
|
129 | { |
|
|
130 | delete p; |
|
|
131 | } |
|
|
132 | |
|
|
133 | void reset (T *a) |
|
|
134 | { |
|
|
135 | delete p; |
|
|
136 | p = a; |
|
|
137 | } |
|
|
138 | |
|
|
139 | // void because it makes sense in our context |
|
|
140 | void operator =(auto_ptr &a) |
|
|
141 | { |
|
|
142 | reset (a.release ()); |
|
|
143 | } |
|
|
144 | |
|
|
145 | template<typename A> |
|
|
146 | void operator =(auto_ptr<A> &a) |
|
|
147 | { |
|
|
148 | reset (a.release ()); |
|
|
149 | } |
|
|
150 | |
|
|
151 | T *operator ->() const { return p; } |
|
|
152 | T &operator *() const { return *p; } |
|
|
153 | |
|
|
154 | operator T *() { return p; } |
|
|
155 | T *get () const { return p; } |
|
|
156 | |
|
|
157 | T *release() |
|
|
158 | { |
|
|
159 | T *r = p; |
|
|
160 | p = 0; |
|
|
161 | return r; |
|
|
162 | } |
|
|
163 | }; |
|
|
164 | |
|
|
165 | typedef auto_ptr<char> auto_str; |
|
|
166 | |
93 | |
167 | #endif |
94 | #endif |
168 | |
95 | |