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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines