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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines