ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
(Generate patch)

Comparing rxvt-unicode/src/rxvtutil.h (file contents):
Revision 1.19 by root, Sun Jan 22 00:36:59 2006 UTC vs.
Revision 1.59 by sf-exg, Thu Oct 23 22:39:40 2014 UTC

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>
4#include <cstdlib> 5#include <stdlib.h>
5#include <cstring> 6#include <string.h>
7#include "ecb.h"
8#include "estl.h"
6 9
7#define PP_CONCAT_(a, b) a ## b 10#include "emman.h"
8#define PP_CONCAT(a, b) PP_CONCAT_(a, b)
9#define PP_STRINGIFY_(a) #a
10#define PP_STRINGIFY(a) PP_STRINGIFY_(a)
11 11
12extern class byteorder { 12// increases code size unless -fno-enforce-eh-specs
13 static unsigned int e; // at least 32 bits 13#if __GNUC__
14public: 14# define NOTHROW
15 byteorder (); 15# define THROW(x)
16 16#else
17 static bool big_endian () { return e == 0x11223344; }; 17# define NOTHROW throw()
18 static bool network () { return e == 0x11223344; }; 18# define THROW(x) throw x
19 static bool little_endian () { return e == 0x44332211; }; 19#endif
20 static bool vax () { return e == 0x44332211; };
21} byteorder;
22 20
23// various utility functions 21// various utility functions
24template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
25template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } 22template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
26template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
27template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } 23template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
28 24
29template<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; } 25template<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; }
30template<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; } 26template<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; }
31 27
32template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 28template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); }
29
30// linear interpolation
31template<typename T, typename U, typename P>
32static inline T
33lerp (T a, U b, P p)
34{
35 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
36}
37
38// return a very temporary (and never deallocated) buffer. keep small.
39void *rxvt_temp_buf (int len);
40
41template<typename T>
42static inline T *
43rxvt_temp_buf (int len)
44{
45 return (T *)rxvt_temp_buf (len * sizeof (T));
46}
33 47
34// in range including end 48// in range including end
35#define IN_RANGE_INC(val,beg,end) \ 49#define IN_RANGE_INC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 50 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
37 51
38// in range excluding end 52// in range excluding end
39#define IN_RANGE_EXC(val,beg,end) \ 53#define IN_RANGE_EXC(val,beg,end) \
40 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 54 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
41 55
56// for m >= -n, ensure remainder lies between 0..n-1
57#define MOD(m,n) (((m) + (n)) % (n))
58
42// makes dynamically allocated objects zero-initialised 59// makes dynamically allocated objects zero-initialised
43struct zero_initialized { 60struct zero_initialized
61{
44 void *operator new (size_t s); 62 void *operator new (size_t s);
45 void operator delete (void *p, size_t s); 63 void operator delete (void *p, size_t s);
46}; 64};
47 65
48/* simplevec taken (and heavily modified), from: 66struct stringvec : simplevec<char *>
49 * 67{
50 * MICO --- a free CORBA implementation 68 ~stringvec ()
51 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
52 */
53template<class T>
54struct simplevec {
55 typedef T* iterator;
56 typedef const T* const_iterator;
57 typedef unsigned long size_type;
58
59private:
60 size_type _last, _size;
61 T *_buf;
62
63public:
64 const_iterator begin () const
65 { 69 {
66 return &_buf[0]; 70 for (char **c = begin (); c != end (); c++)
71 free (*c);
67 } 72 }
68 iterator begin ()
69 {
70 return &_buf[0];
71 }
72 const_iterator end () const
73 {
74 return &_buf[_last];
75 }
76 iterator end ()
77 {
78 return &_buf[_last];
79 }
80 size_type capacity () const
81 {
82 return _size;
83 }
84 size_type size () const
85 {
86 return _last;
87 }
88
89private:
90 static T *alloc (size_type n)
91 {
92 return (T *)::operator new ((size_t) (n * sizeof (T)));
93 }
94 static void dealloc (T *buf)
95 {
96 if (buf)
97 ::operator delete (buf);
98 }
99
100 void reserve (iterator where, size_type n)
101 {
102 if (_last + n <= _size) {
103 memmove (where+n, where, (end ()-where)*sizeof (T));
104 } else {
105 size_type sz = _last+n;
106 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
107 T *nbuf = alloc (sz);
108 if (_buf) {
109 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
110 memcpy (nbuf + (where-begin ()) + n, where,
111 (end ()-where)*sizeof (T));
112 dealloc (_buf);
113 }
114 _buf = nbuf;
115 _size = sz;
116 }
117 }
118
119public:
120 void reserve (size_type sz)
121 {
122 if (_size < sz) {
123 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
124 T *nbuf = alloc (sz);
125 if (_buf) {
126 memcpy (nbuf, begin (), size ()*sizeof (T));
127 dealloc (_buf);
128 }
129 _buf = nbuf;
130 _size = sz;
131 }
132 }
133 simplevec ()
134 : _last(0), _size(0), _buf(0)
135 {
136 }
137 simplevec (size_type n, const T& t = T ())
138 : _last(0), _size(0), _buf(0)
139 {
140 insert (begin (), n, t);
141 }
142 simplevec (const_iterator first, const_iterator last)
143 : _last(0), _size(0), _buf(0)
144 {
145 insert (begin (), first, last);
146 }
147 simplevec (const simplevec<T> &v)
148 : _last(0), _size(0), _buf(0)
149 {
150 reserve (v._last);
151 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
152 _last = v._last;
153 }
154 simplevec<T> &operator= (const simplevec<T> &v)
155 {
156 if (this != &v) {
157 _last = 0;
158 reserve (v._last);
159 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
160 _last = v._last;
161 }
162 return *this;
163 }
164 ~simplevec ()
165 {
166 dealloc (_buf);
167 }
168 const T &front () const
169 {
170 //ministl_assert (size () > 0);
171 return _buf[0];
172 }
173 T &front ()
174 {
175 //ministl_assert (size () > 0);
176 return _buf[0];
177 }
178 const T &back () const
179 {
180 //ministl_assert (size () > 0);
181 return _buf[_last-1];
182 }
183 T &back ()
184 {
185 //ministl_assert (size () > 0);
186 return _buf[_last-1];
187 }
188 bool empty () const
189 {
190 return _last == 0;
191 }
192 void clear ()
193 {
194 _last = 0;
195 }
196 void push_back (const T &t)
197 {
198 reserve (_last+1);
199 *end () = t;
200 ++_last;
201 }
202 void push_back (T &t)
203 {
204 reserve (_last+1);
205 *end () = t;
206 ++_last;
207 }
208 void pop_back ()
209 {
210 //ministl_assert (size () > 0);
211 --_last;
212 }
213 const T &operator[] (size_type idx) const
214 {
215 //ministl_assert (idx < size ());
216 return _buf[idx];
217 }
218 T &operator[] (size_type idx)
219 {
220 //ministl_assert (idx < size ());
221 return _buf[idx];
222 }
223 iterator insert (iterator pos, const T &t)
224 {
225 //ministl_assert (pos <= end ());
226 long at = pos - begin ();
227 reserve (pos, 1);
228 pos = begin ()+at;
229 *pos = t;
230 ++_last;
231 return pos;
232 }
233 iterator insert (iterator pos, const_iterator first, const_iterator last)
234 {
235 //ministl_assert (pos <= end ());
236 long n = last - first;
237 long at = pos - begin ();
238 if (n > 0) {
239 reserve (pos, n);
240 pos = begin ()+at;
241 memcpy (pos, first, (last-first)*sizeof (T));
242 _last += n;
243 }
244 return pos;
245 }
246 iterator insert (iterator pos, size_type n, const T &t)
247 {
248 //ministl_assert (pos <= end ());
249 long at = pos - begin ();
250 if (n > 0) {
251 reserve (pos, n);
252 pos = begin ()+at;
253 for (int i = 0; i < n; ++i)
254 pos[i] = t;
255 _last += n;
256 }
257 return pos;
258 }
259 void erase (iterator first, iterator last)
260 {
261 if (last != first) {
262 memmove (first, last, (end () - last) * sizeof (T));
263 _last -= last - first;
264 }
265 }
266 void erase (iterator pos)
267 {
268 if (pos != end ()) {
269 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
270 --_last;
271 }
272 }
273 void swap (simplevec<T> &t)
274 {
275 ::swap(_last, t._last);
276 ::swap(_size, t._size);
277 ::swap(_buf, t._buf);
278 }
279}; 73};
280
281template<class T>
282bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
283{
284 if (v1.size () != v2.size ())
285 return false;
286 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
287}
288
289template<class T>
290bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
291{
292 unsigned long minlast = min (v1.size (), v2.size ());
293 for (unsigned long i = 0; i < minlast; ++i) {
294 if (v1[i] < v2[i])
295 return true;
296 if (v2[i] < v1[i])
297 return false;
298 }
299 return v1.size () < v2.size ();
300}
301
302
303template<typename T>
304struct vector : simplevec<T>
305{ };
306 74
307#if 0 75#if 0
308template<typename T> 76template<typename T>
309struct rxvt_vec : simplevec<void *> { 77struct rxvt_vec : simplevec<void *>
78{
310 typedef T *iterator; 79 typedef T *iterator;
311 80
312 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 81 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
313 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 82 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
314 void erase (int i) { erase (begin () + i); } 83 void erase (int i) { erase (begin () + i); }
318 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 87 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
319 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])); }
320}; 89};
321#endif 90#endif
322 91
323template <typename I, typename T> 92inline void *
324I find (I first, I last, const T& value) 93operator new (size_t size) throw (std::bad_alloc)
325{ 94{
326 while (first != last && *first != value) 95 // TODO: use rxvt_malloc
327 ++first; 96 return malloc (size);
97}
328 98
329 return first; 99inline void
100operator delete (void *p) throw ()
101{
102 free (p);
330} 103}
331 104
332template<typename T> 105template<typename T>
333struct auto_ptr { 106struct auto_ptr
107{
334 T *p; 108 T *p;
335 109
336 auto_ptr () : p (0) { } 110 auto_ptr () : p (0) { }
111
112 explicit
337 auto_ptr (T *a) : p (a) { } 113 auto_ptr (T *a) : p (a) { }
338 114
339 auto_ptr (auto_ptr<T> &a) 115 auto_ptr (auto_ptr &a)
340 { 116 {
341 p = a.p; 117 p = a.p;
342 a.p = 0; 118 a.p = 0;
343 } 119 }
344 120
352 ~auto_ptr () 128 ~auto_ptr ()
353 { 129 {
354 delete p; 130 delete p;
355 } 131 }
356 132
357 // void because it makes sense in our context 133 void reset (T *a)
358 void operator = (T *a)
359 { 134 {
360 delete p; 135 delete p;
361 p = a; 136 p = a;
362 } 137 }
363 138
139 // void because it makes sense in our context
364 void operator = (auto_ptr &a) 140 void operator =(auto_ptr &a)
365 { 141 {
366 *this = a.p; 142 reset (a.release ());
367 a.p = 0;
368 } 143 }
369 144
370 template<typename A> 145 template<typename A>
371 void operator = (auto_ptr<A> &a) 146 void operator =(auto_ptr<A> &a)
372 { 147 {
373 *this = a.p; 148 reset (a.release ());
374 a.p = 0;
375 } 149 }
376 150
151 T *operator ->() const { return p; }
377 operator T * () const { return p; } 152 T &operator *() const { return *p; }
378 153
154 operator T *() { return p; }
379 T *operator -> () const { return p; } 155 T *get () const { return p; }
380 T &operator * () const { return *p; }
381 156
382 T *get () 157 T *release()
383 { 158 {
384 T *r = p; 159 T *r = p;
385 p = 0; 160 p = 0;
386 return r; 161 return r;
387 } 162 }
388}; 163};
389 164
390typedef auto_ptr<char> auto_str; 165typedef auto_ptr<char> auto_str;
391 166
392struct stringvec : simplevec<char *>
393{
394 ~stringvec ()
395 {
396 for (char **c = begin (); c != end (); c++)
397 free (*c);
398 }
399};
400
401#endif 167#endif
402 168

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines