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