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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines