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> |
7 | #include "ecb.h" |
6 | #include "ecb.h" |
8 | #include "estl.h" |
7 | #include "estl.h" |
|
|
8 | |
|
|
9 | #include "emman.h" |
9 | |
10 | |
10 | // increases code size unless -fno-enforce-eh-specs |
11 | // increases code size unless -fno-enforce-eh-specs |
11 | #if __GNUC__ |
12 | #if __GNUC__ |
12 | # define NOTHROW |
13 | # define NOTHROW |
13 | # define THROW(x) |
14 | # define THROW(x) |
… | |
… | |
20 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
21 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
21 | template<typename T, typename U> static inline void max_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; } |
22 | |
23 | |
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 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 | 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 | |
26 | |
28 | // linear interpolation |
27 | // linear interpolation |
29 | template<typename T, typename U, typename P> |
28 | template<typename T, typename U, typename P> |
30 | static inline T |
29 | static inline T |
31 | lerp (T a, U b, P p) |
30 | lerp (T a, U b, P p) |
… | |
… | |
85 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
84 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
86 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
85 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
87 | }; |
86 | }; |
88 | #endif |
87 | #endif |
89 | |
88 | |
90 | inline void * |
89 | typedef estl::scoped_array<char> auto_str; |
91 | operator new (size_t size) |
|
|
92 | { |
|
|
93 | // TODO: use rxvt_malloc |
|
|
94 | return malloc (size); |
|
|
95 | } |
|
|
96 | |
|
|
97 | inline void |
|
|
98 | operator delete (void *p) |
|
|
99 | { |
|
|
100 | free (p); |
|
|
101 | } |
|
|
102 | |
|
|
103 | template<typename T> |
|
|
104 | struct auto_ptr |
|
|
105 | { |
|
|
106 | T *p; |
|
|
107 | |
|
|
108 | auto_ptr () : p (0) { } |
|
|
109 | |
|
|
110 | explicit |
|
|
111 | auto_ptr (T *a) : p (a) { } |
|
|
112 | |
|
|
113 | auto_ptr (auto_ptr &a) |
|
|
114 | { |
|
|
115 | p = a.p; |
|
|
116 | a.p = 0; |
|
|
117 | } |
|
|
118 | |
|
|
119 | template<typename A> |
|
|
120 | auto_ptr (auto_ptr<A> &a) |
|
|
121 | { |
|
|
122 | p = a.p; |
|
|
123 | a.p = 0; |
|
|
124 | } |
|
|
125 | |
|
|
126 | ~auto_ptr () |
|
|
127 | { |
|
|
128 | delete p; |
|
|
129 | } |
|
|
130 | |
|
|
131 | void reset (T *a) |
|
|
132 | { |
|
|
133 | delete p; |
|
|
134 | p = a; |
|
|
135 | } |
|
|
136 | |
|
|
137 | // void because it makes sense in our context |
|
|
138 | void operator =(auto_ptr &a) |
|
|
139 | { |
|
|
140 | *this = a.p; |
|
|
141 | a.p = 0; |
|
|
142 | } |
|
|
143 | |
|
|
144 | template<typename A> |
|
|
145 | void operator =(auto_ptr<A> &a) |
|
|
146 | { |
|
|
147 | *this = a.p; |
|
|
148 | a.p = 0; |
|
|
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 | |
90 | |
167 | #endif |
91 | #endif |
168 | |
92 | |