ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.13
Committed: Wed Dec 21 14:19:19 2005 UTC (18 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +8 -6 lines
Log Message:
*** empty log message ***

File Contents

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