ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.19
Committed: Sun Jan 22 00:36:59 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-7_2
Changes since 1.18: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #ifndef RXVT_UTIL_H
2 #define RXVT_UTIL_H
3
4 #include <cstdlib>
5 #include <cstring>
6
7 #define PP_CONCAT_(a, b) a ## b
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
12 extern class byteorder {
13 static unsigned int e; // at least 32 bits
14 public:
15 byteorder ();
16
17 static bool big_endian () { return e == 0x11223344; };
18 static bool network () { return e == 0x11223344; };
19 static bool little_endian () { return e == 0x44332211; };
20 static bool vax () { return e == 0x44332211; };
21 } byteorder;
22
23 // various utility functions
24 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
25 template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
26 template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
27 template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
28
29 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; }
30 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; }
31
32 template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
33
34 // in range including end
35 #define IN_RANGE_INC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
37
38 // in range excluding end
39 #define IN_RANGE_EXC(val,beg,end) \
40 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
41
42 // makes dynamically allocated objects zero-initialised
43 struct zero_initialized {
44 void *operator new (size_t s);
45 void operator delete (void *p, size_t s);
46 };
47
48 /* simplevec taken (and heavily modified), from:
49 *
50 * MICO --- a free CORBA implementation
51 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
52 */
53 template<class T>
54 struct simplevec {
55 typedef T* iterator;
56 typedef const T* const_iterator;
57 typedef unsigned long size_type;
58
59 private:
60 size_type _last, _size;
61 T *_buf;
62
63 public:
64 const_iterator begin () const
65 {
66 return &_buf[0];
67 }
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
89 private:
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
119 public:
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 };
280
281 template<class T>
282 bool 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
289 template<class T>
290 bool 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
303 template<typename T>
304 struct vector : simplevec<T>
305 { };
306
307 #if 0
308 template<typename T>
309 struct rxvt_vec : simplevec<void *> {
310 typedef T *iterator;
311
312 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
313 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
314 void erase (int i) { erase (begin () + i); }
315 void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
316 iterator begin () const { return (iterator)simplevec<void *>::begin (); }
317 iterator end () const { return (iterator)simplevec<void *>::end (); }
318 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])); }
320 };
321 #endif
322
323 template <typename I, typename T>
324 I find (I first, I last, const T& value)
325 {
326 while (first != last && *first != value)
327 ++first;
328
329 return first;
330 }
331
332 template<typename T>
333 struct auto_ptr {
334 T *p;
335
336 auto_ptr () : p (0) { }
337 auto_ptr (T *a) : p (a) { }
338
339 auto_ptr (auto_ptr<T> &a)
340 {
341 p = a.p;
342 a.p = 0;
343 }
344
345 template<typename A>
346 auto_ptr (auto_ptr<A> &a)
347 {
348 p = a.p;
349 a.p = 0;
350 }
351
352 ~auto_ptr ()
353 {
354 delete p;
355 }
356
357 // void because it makes sense in our context
358 void operator = (T *a)
359 {
360 delete p;
361 p = a;
362 }
363
364 void operator = (auto_ptr &a)
365 {
366 *this = a.p;
367 a.p = 0;
368 }
369
370 template<typename A>
371 void operator = (auto_ptr<A> &a)
372 {
373 *this = a.p;
374 a.p = 0;
375 }
376
377 operator T * () const { return p; }
378
379 T *operator -> () const { return p; }
380 T &operator * () const { return *p; }
381
382 T *get ()
383 {
384 T *r = p;
385 p = 0;
386 return r;
387 }
388 };
389
390 typedef auto_ptr<char> auto_str;
391
392 struct stringvec : simplevec<char *>
393 {
394 ~stringvec ()
395 {
396 for (char **c = begin (); c != end (); c++)
397 free (*c);
398 }
399 };
400
401 #endif
402