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