1 | #ifndef RXVT_UTIL_H |
1 | #ifndef RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
3 | |
3 | |
4 | #include <stdlib.h> |
4 | #include <stdlib.h> |
5 | #include <string.h> |
5 | #include <string.h> |
|
|
6 | |
|
|
7 | #define ECB_NO_THREADS 1 |
6 | #include "ecb.h" |
8 | #include "ecb.h" |
|
|
9 | |
7 | #include "estl.h" |
10 | #include "estl.h" |
|
|
11 | |
|
|
12 | #include "emman.h" |
8 | |
13 | |
9 | // increases code size unless -fno-enforce-eh-specs |
14 | // increases code size unless -fno-enforce-eh-specs |
10 | #if __GNUC__ |
15 | #if __GNUC__ |
11 | # define NOTHROW |
16 | # define NOTHROW |
12 | # define THROW(x) |
17 | # define THROW(x) |
… | |
… | |
20 | 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; } |
21 | |
26 | |
22 | 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; } |
23 | 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; } |
24 | |
29 | |
25 | template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); } |
|
|
26 | |
|
|
27 | // linear interpolation |
30 | // linear interpolation |
28 | template<typename T, typename U, typename P> |
31 | template<typename T, typename U, typename P> |
29 | static inline |
32 | static inline T |
30 | T lerp (T a, U b, P p) |
33 | lerp (T a, U b, P p) |
31 | { |
34 | { |
32 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
35 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
33 | } |
36 | } |
34 | |
37 | |
35 | // return a very temporary (and never deallocated) buffer. keep small. |
38 | // return a very temporary (and never deallocated) buffer. keep small. |
… | |
… | |
84 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
87 | 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])); } |
88 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
86 | }; |
89 | }; |
87 | #endif |
90 | #endif |
88 | |
91 | |
89 | template<typename T> |
92 | typedef estl::scoped_array<char> auto_str; |
90 | struct auto_ptr |
|
|
91 | { |
|
|
92 | T *p; |
|
|
93 | |
|
|
94 | auto_ptr () : p (0) { } |
|
|
95 | auto_ptr (T *a) : p (a) { } |
|
|
96 | |
|
|
97 | auto_ptr (auto_ptr<T> &a) |
|
|
98 | { |
|
|
99 | p = a.p; |
|
|
100 | a.p = 0; |
|
|
101 | } |
|
|
102 | |
|
|
103 | template<typename A> |
|
|
104 | auto_ptr (auto_ptr<A> &a) |
|
|
105 | { |
|
|
106 | p = a.p; |
|
|
107 | a.p = 0; |
|
|
108 | } |
|
|
109 | |
|
|
110 | ~auto_ptr () |
|
|
111 | { |
|
|
112 | free (p); |
|
|
113 | } |
|
|
114 | |
|
|
115 | void reset (T *a) |
|
|
116 | { |
|
|
117 | free (p); |
|
|
118 | p = a; |
|
|
119 | } |
|
|
120 | |
|
|
121 | // void because it makes sense in our context |
|
|
122 | void operator = (auto_ptr &a) |
|
|
123 | { |
|
|
124 | *this = a.p; |
|
|
125 | a.p = 0; |
|
|
126 | } |
|
|
127 | |
|
|
128 | template<typename A> |
|
|
129 | void operator = (auto_ptr<A> &a) |
|
|
130 | { |
|
|
131 | *this = a.p; |
|
|
132 | a.p = 0; |
|
|
133 | } |
|
|
134 | |
|
|
135 | operator T * () const { return p; } |
|
|
136 | |
|
|
137 | T *operator -> () const { return p; } |
|
|
138 | T &operator * () const { return *p; } |
|
|
139 | |
|
|
140 | T *get () |
|
|
141 | { |
|
|
142 | T *r = p; |
|
|
143 | p = 0; |
|
|
144 | return r; |
|
|
145 | } |
|
|
146 | }; |
|
|
147 | |
|
|
148 | typedef auto_ptr<char> auto_str; |
|
|
149 | |
93 | |
150 | #endif |
94 | #endif |
151 | |
95 | |