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